1 / 49

INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014

INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014. Lecture 14 Title: MidTerm Analysis & Comments (Extra) Reference: Malik,Farrell,Liang. Lecture Contents:. Analysis and solutions of mid term exam Mid Term Exam, version 20a, Tuesday, March 18. 2014 Student name:

Download Presentation

INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014 Lecture 14 Title: MidTerm Analysis & Comments (Extra) Reference: Malik,Farrell,Liang

  2. Lecture Contents: Analysis and solutions of mid term exam Mid Term Exam, version 20a, Tuesday, March 18. 2014 Student name: Rules for evaluation the students’ results on mid term exam: 10 tasks * 4 = 40 1 task (No 11) = 20 1 task (No 12) = 40 --------------------------------------------

  3. Lecture Contents: Analysis and solutions of mid term exam Mid Term Exam, version 20b, Tuesday, March 18. 2014 Student name: Rules for evaluation the students’ results on mid term exam: 10 tasks * 4 = 40 1 task (No 11) = 20 1 task (No 12) = 40 --------------------------------------------

  4. Task 1a • What is wrong with the declarative statements below? Correct them if possible! • int xx = 1; • byte yy = xx;

  5. Task 1a What is wrong with the declarative statements below? Correct them if possible! int xx = 1; byte yy = (byte)xx;

  6. Task 1b • What is wrong with the declarative statements below? Correct them if possible! • long xx = 1; • int yy = xx;

  7. Task 1b What is wrong with the declarative statements below? Correct them if possible! long xx = 1; int yy = (int)xx;

  8. Task 2a • Show the output of the following statements • System.out.println(“1” + 1); • System.out.println(‘1’ + 1);

  9. Task 2a • Show the output of the following statements • System.out.println(“1” + 1); 11 • System.out.println(‘1’ + 1); 50

  10. Task 2b • Show the output of the following statements • System.out.println(“1” + 1 + 1); 111 • System.out.println(‘1’ + 1 + 1); 51

  11. Task 3a • Given input stream configured to include: “Welcome to Java” followed by CR/Enter • Given • Scanner cin = new Scanner(System.in); • String s1 = cin.next(); • What data is to be saved in s1?

  12. Task 3a • Given input stream configured to include: “Welcome to Java” followed by CR/Enter • Given • Scanner cin = new Scanner(System.in); • String s1 = cin.next(); • What data is to be saved in s1? Welcome

  13. Task 3b • Given input stream configured to include: “Welcome to Java” followed by CR/Enter • Given • Scanner cin = new Scanner(System.in); • String s1 = cin.nextLine(); • What data is to be saved in s1?

  14. Task 3b • Given input stream configured to include: “Welcome to Java” followed by CR/Enter • Given • Scanner cin = new Scanner(System.in); • String s1 = cin.nextLine(); • What data is to be saved in s1? Welcome to Java

  15. Task 4a • . The assignment operator is ……………… associative operator.

  16. Task 4a • . The assignment operator is …right…… associative operator.

  17. Task 4b • All arithmetic operators are ………….. associative operators.

  18. Task 4b • All arithmetic operators in Java are ……left…….. associative operators.

  19. Task 5a • Declare array reference variable and create one-dimensional double data type array with size typed by the user as input value at run time

  20. Task 5a int arraysize; System.out.print("Enter array size:"); arraysize = cin.nextInt(); double[] b = new double[arraysize]; // array size defined at run time for (i=0; i<b.length; i++) // array populate & display { b[i]=i*10; System.out.print(" " + b[i]);} // iteration based on data structures for(double id:b) {System.out.print(" "+id);}

  21. Task 5b • Declare array reference variable and create one-dimensional int data type array with size typed by the user as input value at run time

  22. Task 5b int arraysize; System.out.print("Enter array size:"); arraysize = cin.nextInt(); int[] b = new int[arraysize]; // array - size defined at run time for (i=0; i<b.length; i++) // array populate & display { b[i]=i*10; System.out.print(" " + b[i]);} // iteration based on data structures for(int id:b) {System.out.print(" "+id);}

  23. Task 6a • Given a two-dimensional int array with 5 rows x by 6 columns. Write statements to display the array row by row on a separate line.

  24. Task 6a int[][] mas = new int[5][6]; int i, j; for(i=0; i< mas.length; i++ ) { for (j=0; j<mas[2].length; j++) { System.out.print(" " + mas[i][j]); } System.out.println(); }

  25. Task 6b • Given a two-dimensional float array with 3 rows x by 5 columns. Write statements to display the array column by column on a separate line.

  26. Task 6b float[][] mas = { {1,2,3,4,5},{11,12,13,14,15},{21,22,23,24,25}}; int j, i; for (j=0; j<mas[2].length; j++) { for(i=0; i< mas.length; i++ ) { System.out.print(" " + mas[i][j]); } System.out.println(); }

  27. Task 7a • Write an example to illustrate a ragged array on your choice

  28. Task 7a • int[][] matrix3 = new int[3][]; // 2D array - jagged array • matrix3[0] = new int[] {0, 2, 4, 6, 8, 10}; • matrix3[1] = new int[] {1, 3, 5, 7 }; • matrix3[2] = new int[] {11, 22}; • for (i=0; i<matrix3.length; i++) { // display jagged array • System.out.println(); • for (j = 0; j < matrix3[i].length; j++) { • System.out.print(" " + matrix3[i][j]); • } • }

  29. Task 7b • Write an example to illustrate a ragged array on your choice

  30. Task 7b int[][] matrix4 = { {0, 2, 4, 6, 8, 10}, {1, 3, 5, 7 }, {11, 22}}; // display jagged array for (int i=0; i<matrix4.length; i++) { System.out.println(); for (int j = 0; j < matrix4[i].length; j++) { System.out.print(" " + matrix4[i][j]); } }

  31. Task 8a • Overloading methods. What is it? Give an example.

  32. Task 8b • Overriding methods. What is it? Give an example.

  33. Task 9a • Created type and actual type of a reference variable. • Write an example of a reference variable with the same created type and actual type. • Write an example of a reference variable with different created type and actual type.

  34. Task 9b • Created type and actual type of a reference variable. • Write an example of a reference variable with the same created type and actual type. • Write an example of a reference variable with different created type and actual type.

  35. Task 10a • Given a while loop: • int I=100; • while(I>=0) { • System.out.println(“AUBG”); • I-=20; • } • Convert it to equivalent for loop statement

  36. Task 10a • The converted loop: • for(int I=100; I>=0; I-=20) { • System.out.println(“AUBG”); • }

  37. Task 10b • Given a for loop: • for(int I=0; I<200; I=I-20) • System.out.println(“AUBG”); • Convert it to equivalent while loop statement

  38. Task 10b • The converted loop: • int I=0; • while (I<200) { • System.out.println(“AUBG”); • I=I-20; • }

  39. Practical Task 11a What is the printout of running class D in the Java source text below: class A { public A() { System.out.println("A's no-arg constructor invoked"); } } class B extends A { public B() { } } class C extends B { public C() { } } public class D { public static void main(String[] args) { C c = new C(); B b = new B(); new C(); new B(); } }

  40. Practical Task 11a • A's no-arg constructor invoked • A's no-arg constructor invoked • A's no-arg constructor invoked • A's no-arg constructor invoked

  41. Practical Task 11b class A { public A() { System.out.println("A's no-arg constructor invoked"); } } class B extends A { public B() { super(); System.out.println("B's no-arg constructor invoked"); } } class C extends B { public C() { System.out.println("C's no-arg constructor invoked"); } } public class D { public static void main(String[] args) { new C(); new C(); } }

  42. Practical Task 11b • A's no-arg constructor invoked • B's no-arg constructor invoked • C's no-arg constructor invoked • A's no-arg constructor invoked • B's no-arg constructor invoked • C's no-arg constructor invoked

  43. Practical Task 12a • Develop your user defined class Calculate1 to include a method computeResult() that returns the computed value of the following formula. • sum = 1 + ½ + ¼ + 1/8 + 1/16 + 1/32 + 1/64 + … • Hint: Develop one more class TestCalculate1 with main() method to test the functionality of the computeResult() method. You have to display three result values after processing the first 4, 6 and 8 operands of the infinite sum in the formula

  44. Practical Task 12b • Develop your user defined class Calculate2 to include a method computePI() that returns the computed value of π. • Hint 1: Approximate value of PI can be computed using the formula • π/4 = 1 – 1/3 + 1/5 – 1/7 + 1/9 – 1/11 + … • Hint 2: Develop one more class TestCalculate2 with main() method to test the functionality of the computePI() method. You have to display three result values after processing the first 8, 10 and 12 operands of the infinite sum in the formula

  45. Thank You For Your Attention! Any Questions?

  46. Practical Task 12a package sbmidtermExam20a; class Calculate1 { static double computeResult( int n) { // sum = 1 + ½ + ¼ + 1/8 + 1/16 + 1/32 + 1/64 + … double sum= 1.; for (int i=1; i<=n; i++) { sum = sum + 1/Math.pow(2. , i); } return sum; } } public class SBMidTermExam20a { public static void main(String[] args) { Calculate1 c = new Calculate1(); int i; for(i=1; i<100; i+=10) { System.out.print(" " + i + " " + Calculate1.computeResult(i)); System.out.println(" " + c.computeResult(i)); } } }

  47. Practical Task 12a run: 1 1.5 1.5 11 1.99951171875 1.99951171875 21 1.9999995231628418 1.9999995231628418 31 1.9999999995343387 1.9999999995343387 41 1.9999999999995453 1.9999999999995453 51 1.9999999999999996 1.9999999999999996 61 2.0 2.0 71 2.0 2.0 81 2.0 2.0 91 2.0 2.0 BUILD SUCCESSFUL (total time: 1 second)

  48. Practical Task 12b package sbmidtermExam20b; class Calculate2 { static double computePI( int n) { // π/4 = 1 – 1/3 + 1/5 – 1/7 + 1/9 – 1/11 + … double sum= 1.; for (int i=1; i<=n; i+=4) { sum = sum - 1./(i+2) + 1./(i+4); } return sum*4; } } public class SBMidTermExam20b { public static void main(String[] args) { Calculate2 cc = new Calculate2(); for(int j=1; j<=5000; j+=500) { System.out.print(" " + j + " " + Calculate2.computePI(j)); System.out.println(" " + cc.computePI(j)); } } }

  49. Practical Task 12b run: 1 3.466666666666667 3.466666666666667 501 3.1455452073225016 3.1455452073225016 1001 3.1435807231959534 3.1435807231959534 1501 3.142920674252598 3.142920674252598 2001 3.1425896623151117 3.1425896623151117 2501 3.1423907380596776 3.1423907380596776 3001 3.142257989510838 3.142257989510838 3501 3.142163104199401 3.142163104199401 4001 3.1420919046819966 3.1420919046819966 4501 3.1420365062088567 3.1420365062088567 BUILD SUCCESSFUL(total time: 1 second)

More Related