Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java111 example final LAB : 1. Write a Java program that reads an integer x, and a character c; then calls a method draw that prints a triangle of c in x lines. 2. Write a program that reads an integer then calls a method to check whether or not the integer is perfect. The program should print a message accordingly. An integer is said to be perfect if its factors including 1 but excluding the integer itself sum to the number. Example: 6 is perfect because 1 + 2 + 3 = 6. 3. Write a program that reads three integers: a, b and n. the programs calls a method that returns the nth Fibonacci number with a and b being the first and second. Main then calls another method to print the result. Self Study: 4. Write a program that reads an integer and then calls a method that receives an integer and returns the integer with its digits reversed. Main prints the resulting integer. 5. Write a program that reads two integers and then calls a method that receives the two integers and returns their greatest common divisor, which is printed in main. TUTORIAL: 1. Trace the following program: import java.util.*; public class M{ static Scanner console = new Scanner(System.in); static int i = 0; public static void main (String[] args) { int a; double b; i++; System.out.println (i); a = console.nextInt(); b = console.nextDouble(); double x = test(a, b); System.out.println("a = " + a + ", b = ", b + “x= “ + x +", test(x, b)= ", test((int)x, b)); if (isEven(a) ) System.out.println(a+ “is even” ); i++; System.out.println(i); } public static double test(int x, double y) { i++; System.out.println(i); return y * 3 + 2 * x; } public static Boolean isEven(int m) { i++; System.out.println(i); return (m%2 == 0 ); } 2. find the errors: class err{ public static void main(String args[]){ int x , y; String s = “test”; m1(int x , s); y = m2(); } public void m1(int d ){ … } public static m2( ) { if ( true) return true; } } 3. write method headers for the following: a. a method that receives three strings and returns nothing. b. A method that receives an integer and a character and returns a string. c. A method that returns a Boolean and receives nothing. 4. Write a method that receives a character and an integer and returns a string composed of the character repeated as many times as the integer. 5. import java.util.* ; class sheet12_exer1 { static Scanner consol = new Scanner(System.in) ; public static void main(String args[]) { System.out.println(" enter No. of lines to draw trangel"); int line = consol.nextInt(); System.out.println(" enter char to draw : "); char ch = consol.next().charAt(0) ; (line , ch) ; }// end main static void draw(int line , char ch) { int blank = 30 ; for (int i=0 ; i<line ; i++) { for (int k=0 ;k<blank;k++) System.out.print(" "); for (int j=0 ; j<i ; j++) System.out.print(ch+" "); System.out.println(); blank--; }//end for } } -----------------6. // import java.util.*; class sheet12_exer2 { static Scanner consol = new Scanner(System.in); public static void main(String[] args) { int no; boolean bol; System.out.println("Enter a number"); no=consol.nextInt(); bol=isperfect(no); if (bol) System.out.println(" The No. ( "+no+" ) is perfect."); else System.out.println(" The No. ( "+no+" ) is Nonperfect."); }// end main public static boolean isperfect(int No) { int sum = 0; for( int i=1 ; i<No; i++) { if (No%i == 0) sum = sum + i; } if (sum == No) return true; return false; }// end method } --------------------------7. import java.util.* ; class sheet12_exer3 { static Scanner consol = new Scanner(System.in) ; public static void main(String args[]) { int a , b , fi ; System.out.println(" enter No. 1 , No 2 a = consol.nextInt(); b = consol.nextInt(); fi = consol.nextInt(); and int result = fib(a , b , fi) ; printResult(result); }// end main static int fib(int no1 , int no2 , int fi) { int result=0 , count ; if(fi == 1) return no1 ; else if(fi == 2) return no2 ; else { count = 3 ; while( count <= fi) { result = no1 + no2 ; no1 = no2 ; no2 = result ; count++ ; } return result ; } }// end fib static void printResult(int n) { System.out.println(" the result is : "+ n) ; } }// end class 8. -----------------------9. import java.util.* ; No. of fibbonach: "); class sheet12_exer4 { static Scanner consol = new Scanner(System.in) ; public static void main(String args[]) { int num ; System.out.println(" enter your number to reverse it ."); num = consol.nextInt(); int num2 = revers(num) ; System.out.println("number after reverse it = " + num2); }// end main public static int revers(int n ) { int no2 =0; while(n != 0) { no2 = n% 10 + no2*10 ; n = n /10 ; } return no2 ; } }// end class 10. ------------------------11. import java.util.* ; class sheet12_exer5 { static Scanner consol = new Scanner(System.in) ; public static void main(String args[]) { int num1 , num2 ; .out.println(" enter no1 ."); num1 = consol.nextInt(); System.out.println(" enter no2 ."); num2 = consol.nextInt(); int div = divisor(num1 , num2 ) ; System.out.println("the common divesor = " + div); }// end main public static int divisor(int n1 , int n2 ) { int div = 1 , i ; for(i = 1 ; i <= n1 && i <= n2 ; i++) if( n1 % i == 0 && n2 % i == 0 ) div = i ; return div ; } }// end class 12. -------------------------------------13. Lap final 14. 15. import java.util.*; class vowel { static Scanner consol = new Scanner(System.in); public static void main(String s[]) { String str ; char ch ; int vowel = 0 , letter = 0 , spaces = 0 ; System.out.println(" enter String "); str = consol.nextLine(); for(int i = 0 ; i < str.length() ; i++) { ch = str.charAt(i); switch (ch) { case 'a': case 'o' : case 'e' : case 'i': case 'u' : vowel ++ ; break ; }// end seitch if( isLetter(ch) ) letter++ ; if(ch == ' ' ) spaces++; }// end for System.out.println(" vowels = "+ vowel); System.out.println(" letters = "+ letter); System.out.println(" spaces = "+ spaces); }// end main }//end class ============================================ Lab 1. Write a program that reads the elements of a one dimensional array and then call a method that sorts the array in ascending sequence. 2. Write a program that reads the elements of a 5x7 two dimensional array, and then calls a method that forms a 7x5 2 dimensional array that is the reverse of the first array. 3. Write a program that reads information for a list of students composed of: name, score. Your program should sort the names alphabetically, then print the sorted list with the grade (A, B, C …). 4. Write a program that reads a list of n integers between 0 and 10, then print the number of occurrences of each integer read. Do not print 0 occurrences. Tutorial 1. Write statements that set the elements of the diagonal of a 5 by 5 array of characters to '%'. 2. Write statements that increment the elements of a 5 by 5 array of integers by 10. 3. Write statements that generate a 10 by 10 integer array where the elements are random integers between 0 and 100 inclusive. 4. Write a method that receives a variable length parameter list of doubles and prints the total number of numbers less than 0, equal to 0 and greater than 0. Use a foreach loop. /*1. Write a program that reads the elements of a one dimensional array and then call a method that sorts the array in ascending sequence.*/ import java.util.*; class sheet12_exer1 { static Scanner consol = new Scanner(System.in); public static void main(String[] args) { int[] list = new int[9]; System.out.println(" enter 9 NO. :"); for(int i=0 ;i<list.length ; i++) list[i]=consol.nextInt(); sort(list); System.out.println("After sort :"); for(int i=0 ;i<list.length ; i++) System.out.print(list[i]+" - "); }// end main public static void sort(int[] list) { int temp ; for(int i=0 ;i<list.length ; i++) for(int j=i+1;j<list.length ; j++) if(list[i]>list[j]) { temp = list[i]; list[i] = list[j] ; list[j]= temp ; } } }// end class ----------------------/* 2-Write a program that reads the elements of a 5x7 two dimensional array, and then calls a method that forms a 7x5 2 dimensional array that is the reverse of the first array.*/ import java.util.*; class sheet12_exer2 { static Scanner consol = new Scanner(System.in); public static void main(String[] args) { int[][] list = new int[5][7] ; for(int i=0 ; i<list.length ; i++) { System.out.println(" enter 7 No. to row " + (i+1)); for(int j=0 ; j<list[i].length ; j++) list[i][j]= consol.nextInt(); } System.out.println("array After fill : "); for(int i=0 ; i<list.length ; i++) { for(int j=0 ; j<list[i].length ; j++) System.out.printf("%6d",list[i][j]); System.out.println(); } int[][] list2 = new int[7][5]; list2= revers(list); System.out.println("array After revers : "); // line 34 for(int i=0 ; i<list2.length ; i++) { for(int j=0 ; j<list2[i].length ; j++) System.out.printf("%6d",list2[i][j]); System.out.println(); } }// end main public static int[][] revers(int[][] list) { int[][] int row int col // line list2 = new int[7][5]; = list.length -1 ; = list[0].length -1 ; 50 for(int i=0 ; i<list2.length ; i++) { row =list.length -1 ; for(int j=0 ; j<list2[i].length ; j++) { list2[i][j] = list[row][col]; row-- ; } col-- ; }// end first for loop return list2; } }// end class ----------------------/*3- Write a program that reads information for a list of students composed of: name, score. Your program should sort the names alphabetically, then print the sorted list with the grade (A, B, C …).*/ // sheet 12 exer 3 import java.util.*; public class list_Student_sheet12 { static Scanner consol = new Scanner(System.in); public static void main(String[] args) { int len = 3 ; String[] names=new String[len]; double[] scor = new double[len]; for(int i=0 ; i<names.length ; i++) { System.out.println("enter student name then score" +" for student("+(i+1)+") :"); names[i] = consol.next(); scor[i] = consol.nextInt(); }// end for sort(names , scor); print(names , scor); }// end main public static void print(String[] std , double[] scor) { char grade; int i ; for (i=0; i< std.length ; i++) { if(scor[i]>=90) grade='A' ; else if(scor[i]>=80) grade='B' ; else if(scor[i]>=70) grade='C' ; else if(scor[i]>=60) grade='D' ; else grade='F' ; System.out.println(std[i] +" "+grade); }// end for }// end method public static void sort(String[] std , double[] scor) { String temp ; double temp_double ; for(int i=0 ; i<std.length-1 ; i++) { for(int j= i+1 ; j<std.length ; j++) { if(std[i].compareTo(std[j])>0) { temp= std[i]; std[i]=std[j]; std[j]=temp; // another array temp_double= scor[i]; scor[i]=scor[j]; scor[j]=temp_double; } // end if } // end for j }// end for i }// end method }// end class ------------------------------/* 4- Write a program that reads a list of n integers between 0 and 10, then print the number of occurrences of each integer read. Do not print 0 occurrences.*/ import java.util.*; class sheet12_exer4 { static Scanner consol = new Scanner(System.in); public static void main(String[] args) { int n ; int[] list = new int[11]; System.out.println("how many n = consol.nextInt(); Number you want to enter :"); for (int i=0 ; i<n ; i++) { System.out.println("enter Number :"); list[consol.nextInt()]++ ; } for (int i=1 ; i<11; i++) System.out.println("NO ("+i+") = "+ list[i]); }//end main }// end class =============================== 1. Write a program that reads an integer n, then declares an array of n elements. The program then calls a method that fills the array with the first n Fibonacci numbers. Then main reads a list of numbers less than n that denote the position of a number and calls another method that uses the array to return the Fibonacci number in that position. Main stops when a negative number is read. 2. Write a program that reads a string and prints the number of occurrences of each alphabetical letter in the string. Your program should call a method to create an array for the counters of the letters, and use another overloaded method that receives a character and updates the corresponding counter and finally call a third method to print the counts. 3. Write a program that reads names of students in order, then their grades. The program then calls a method that sorts the names in alphabetical order, the method calls another method that moves the grades to the appropriate place accordingly. Main then calls a third method that prints a list of the students with their grades. TUTORIAL: 1. Trace the following segment: int[] a , b, c ; int m; a = new int[6]; b = new int[6]; c =new int[12]; for (m = 0 ;m<a.length;m++) a[m] = m*3; for (m = 0; m<b.length ; m++) b[m] = m ; for ( m = 0; m<c.length ; m++) if (m%2 == 0) c[m] = a[m/2]; else c[m] = b[m/2]; for (m = 0 ;m<a.length;m++) System.out.print(a[m]+ " , " ); System.out.println(); for (m = 0; m<b.length ; m++) System.out.print(b[m]+ " , " ); System.out.println(); for ( m = 0; m<c.length ; m++) System.out.print(c[m]+ " , " ); System.out.println(); 2. Find the errors in the following code: public class ArrayError{ public static void main( String args[]) { char i=0; char[] c = {'a' , 'b' , 'c' , 'd' , 'e' }; g(c[],6); for (; i<=4; i++ ) System.out.print( c[i] ); } public static void g(char[] arr, int x) { char[] arr; for ( int i=0 ; i<=x ; i++ ) arr[i]++; } 3. Write a method that receives an integer array, the number of elements in the array and an integer x. The method should search the array for the first occurrence of x and return the location of the element. If no element is found, the method returns -1. 4. Write a method that receives an array of characters and returns a string that is composed of the characters in the array. 5. Write a method that receives an array of integers and returns another array that is the reverse of the received array. 6. Write a method that receives and sorts a one dimensional array of integers. /* 1- Write a program that reads an integer n, then declares an array of n elements. The program then calls a method that fills the array with the first n Fibonacci numbers. Then main reads a list of numbers less than n that denote the position of a number and calls another method that uses the array to return the Fibonacci number in that position. Main stops when a negative number is read.*/ import java.util.*; class sheet11_exer1 { static Scanner consol = new Scanner(System.in); public static void main(String s[]) { int n , postion ; System.out.println("Enter the number to calculat the fibonacce No. : "); n = consol.nextInt(); int[] array = new int[n]; fillArray(array , n);// method call do { System.out.println(" Enter the number less than" + n); postion = consol.nextInt(); while(postion > n ) { System.out.println(" the numbes should be less than "+n+ " and begger than zero "); System.out.println("enter another No."); postion = consol.nextInt(); } if(postion <0 ) break ; else { System.out.println(" The value of this position is : "+ returnValu(array,postion)); } // end do }while(postion>= 0); }// end main public static void fillArray( int list[],int no1) { int no2 = no1; list[0] = no1; list[1] = no2; for ( int i=2 ; i<list.length ; i++) { list[i]=no1 + no2; no1 = no2; no2 = list[i]; } }// end method public static int returnValu(int list[] , int position) { return list[position]; }//end method }// end class ------------------------- /* 2-Write a program that reads a string and prints the number of occurrences of each alphabetical letter in the string. Your program should call a method to create an array for the counters of the letters, and use another overloaded method that receives a character and updates the corresponding counter and finally call a third method to print the counts*/ import java.util.*; import static java.lang.Character.*; class sheet11_exer2 { static Scanner consol = new Scanner(System.in); public static void main(String[] args) { String str; System.out.println(" Enter a string to count the occurrences of each alphabeticalletter: "); str = consol.nextLine(); countLetter(str);// send str to take all char alon from it }//end main public static void countLetter(String str) { char chr ; int[] countLetter = new int[26];//26 because this is NO. letter in languag for (int i =0 ; i<str.length() ; i++) { chr= str.charAt(i); countLetter(chr,countLetter);// send a char to test it }// end for print_count_letter(countLetter);// to print a count of each letter } //end method public static void countLetter(char ch , int[] countLetter) { int position; ch = toUpperCase(ch); // to easy count them we will make all letter capital position =(int) ch - 65; // convert the No. of char in askii code to No. from 0 to 25 if ( position >=0 && position<26) countLetter[position]++; // the No. of char will increas }//end method public static void print_count_letter(int letter[]) { // we will convert the No. to char then print it for (int i=0; i<26 ; i++) System.out.println( (char)(i+65) + " occor = " + letter[i]); }//end method }//end class -------------------------------/*3-Write a program that reads names of students in order, then their grades. The program then calls a method that sorts the names in alphabetical order, the method calls another method that moves the grades to the appropriate place accordingly. Main then calls a third method that prints a list of the students with their grades. */ import java.util.*; class sheet11_exer3 { static Scanner consol = new Scanner(System.in); public static void main(String[] args) { String[] names ; int[] grades ; int No ; System.out.println(" Enter how many student you have "); No = consol.nextInt(); names = new String[No]; grades = new int[No]; for (int i = 0 ; i< No ; i++) { System.out.print(" Enter student name NO. " + (i+1) + " : "); names[i] = consol.next(); System.out.println(); System.out.print(" Enter student grade NO. " + (i+1) + " : "); grades[i] = consol.nextInt(); System.out.println(); }// end for order(names , grades); print_std(names , grades); } // end main public static void order(String[] name , int[] grade) { String temp; int j; for ( int i=0 ; i<name.length -1 ; i++) { int small =i; for ( j=i+1 ; j<name.length ; j++) if (name[j].compareTo(name[small]) <0 ) small = j; temp=name[small]; name[small] = name[i]; name[i] = temp; order_grade(small ,i , grade); }// end first for }// end method public static void order_grade(int sml, int j, int[] grad) { int temp; temp=grad[sml];[sml] = grad[j]; grad[j] = temp; }// end method public static void print_std(String[] names , int[] grades) { System.out.printf("%n%-30s %-6s %n"," names","grades" ) ; System.out.println("-----------------------------------------------"); for(int i=0 ; i<names.length ; i++) System.out.printf(" %-30s %-4d %n",names[i],grades[i] ) ; }// end method print_std }// end class ---------------------- مصفوفة و يعمل معهم على التوازي يعني2 هنا البرنامج اللي حكيتلكم عنه اللي يعرف Parallel array /*3- Write a program that reads information for a list of students composed of: name, score. Your program should sort the names alphabetically, then print the sorted list with the grade (A, B, C …).*/ // sheet 12 exer 3 import java.util.*; public class list_Student_sheet12 { static Scanner consol = new Scanner(System.in); public static void main(String[] args) { int len = 3 ; String[] names=new String[len]; double[] scor = new double[len]; for(int i=0 ; i<names.length ; i++) { System.out.println("enter student name then score" +" for student("+(i+1)+") :"); names[i] = consol.next(); scor[i] = consol.nextInt(); }// end for sort(names , scor); print(names , scor); }// end main public static void print(String[] std , double[] scor) { char grade; int i ; for (i=0; i< std.length ; i++) { if(scor[i]>=90) grade='A' ; else if(scor[i]>=80) grade='B' ; else if(scor[i]>=70) grade='C' ; else if(scor[i]>=60) grade='D' ; else grade='F' ; System.out.println(std[i] +" "+grade); }// end for }// end method public static void sort(String[] std , double[] scor) { String temp ; double temp_double ; for(int i=0 ; i<std.length-1 ; i++) { for(int j= i+1 ; j<std.length ; j++) { if(std[i].compareTo(std[j])>0) { temp= std[i]; std[i]=std[j]; std[j]=temp; // another array temp_double= scor[i]; scor[i]=scor[j]; scor[j]=temp_double; } // end if } // end for j }// end for i }// end method }// end class ------------------------------/* 4- Write a program that reads a list of n integers between 0 and 10, then print the number of occurrences of each integer read. Do not print 0 occurrences.*/ import java.util.*; class sheet12_exer4 { static Scanner consol = new Scanner(System.in); public static void main(String[] args) { int n ; int[] list = new int[11]; System.out.println("how many n = consol.nextInt(); Number you want to enter :"); for (int i=0 ; i<n ; i++) { System.out.println("enter Number :"); list[consol.nextInt()]++ ; } for (int i=1 ; i<11; i++) System.out.println("NO ("+i+") = "+ list[i]); }//end main }// end class