Casual Discussion, Warm-up Questions, and Lecture Demo Exercises  

advertisement
Lecture Exercise 06
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Casual Discussion, Warm-up Questions, and Lecture Demo Exercises
Q1. (a) Explain: "reuse" and "redefine" found in Lab05_Q4 program: Answer: (1) Reuse: The ____________ method defined in the ______ class is reused in the _______ class. (2) Redefine: The _______________ methods defined in the _______ class are redefined in the _______ class. (b) Read the report() method in PersonnelOffice.java and note the statement: tot+=e.getSalary(); If some employees e are managers, will the bonus be counted? [Ref: Polymorphism and Dynamic Binding in Page 7 of Topic 04 Inheritance] Q2. The RandomNumber class below is designed to give random numbers. Sample usage: new RandomNumber() ‐ give a number in 0 .. 99 new RandomNumber(n) ‐ give a number 0 .. n‐1 However it has a compile‐time error: The constructor RandomNumber() is undefined Your tasks: Explain why there exists such an error and give a proper solution. (4 marks) public class RandomNumber { private int r=(int)(Math.random()*100); //0..99 public RandomNumber(int n) { r=(int)(Math.random()*n); } public String toString() {return ""+r;} public static void main(String[] args) { System.out.println(new RandomNumber(1000)); //Expected: 0..999 [OK] System.out.println(new RandomNumber()); //Expected: 0..99 [ERROR!] } }
* Since a constructor is written by ourselves, JAVA does not generate the no‐argument constructor for us. Last modified: 26‐Feb‐2016 1
Lecture Exercise 06
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Q3. Inheritance and Casting What's wrong with the following sayings? (i) Given a variable declared as A x; (where A is a class; which has a subclass, class B) * (B)x means to change the type of x to class B. * If x actually refers to a class B object and both class A and class B have a non‐static method of the same name, f1, then due to dynamic binding, x.f1() will run the method defined in class B. (ii) Dynamic Binding – The compiler automatically selects the appropriate non‐static method at runtime Q4. (i) What is the output of the following program? (ii) Rewrite the for‐loops as for‐each loops Output: public static void main(String[] args)
{
Integer[] arr = new Integer[3]; //Simply treat Integer as int (See Topic 04)
arr[0]= 100;
arr[1]= 101;
arr[2]= 109;
ArrayList<Integer> arrlist = new ArrayList<Integer>();
arrlist.add(100);arrlist.add(101);arrlist.add(109);
for (int i=0;i<arr.length;i++)
System.out.println(arr[i]);
for (int i=0;i<arrlist.size();i++)
System.out.println(arrlist.get(i));
}
Rewrite as for‐each loops:
for( System.out.println( for( System.out.println( )
); ) ); Q5 Read the code below. Which line(s) contain invalid code? Why? (2 marks) Integer i=3; //line 1 String s; //line 2 s = (String)i; //line 3 Object o; //line 4 o = i; //line 5 i = (Integer)o; //line 6 * String and Integer are not pair of super_class / sub_class. Last modified: 26‐Feb‐2016 2
Lecture Exercise 06
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Q6. this and super (a) this ‐ Read the following program. It can compile and run to give 5 lines of output. Your tasks: (i) Insert "this." to the code as many as possible, so that the code still works in the same way. (ii) Complete the blanks in the comments to show all outputs. class A { public String tellMe() {return "a";} public void alg() {System.out.println(tellMe() + "*");} } class B extends A { public String tellMe() {return "b";} public void alg() {System.out.println(tellMe() + "#");} } class C extends A { public String tellMe() {return "c";} } public static void main(String [] args) { A x1 = new B(); x1.alg(); //b# B x2 = new B(); x2.alg(); //b# A x0 = new A(); x0.alg(); //______________ A x3 = new C(); x3.alg(); //______________ C x4 = new C(); x4.alg(); //______________ }
(b) super ‐ Now, "tellMe()" is changed to "toString()", and "super." Is added into the code. What will be the outputs? class A { public String toString() {return "a";} public void alg() {System.out.println(super.toString() + "*");} } class B extends A { public String toString() {return "b";} public void alg() {System.out.println(super.toString() + "#");} } class C extends A { public String tellMe() {return "c";} } public static void main(String [] args) { A x1 = new B(); x1.alg(); //a# B x2 = new B(); x2.alg(); //a# A x0 = new A(); x0.alg(); //A@2a139a55* ("A@2a139a55" is obtained from .toString() of the Object class)
A x3 = new C(); x3.alg(); //______________ C x4 = new C(); x4.alg(); //______________ }
Last modified: 26‐Feb‐2016 3
Lecture Exercise 06
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Q7. Understanding two common runtime problems: stack overflow and out of heap space The following illustration explains the usage of memory in JVM: When the memory usage exceeds the allocated amount, runtime errors occur: 2 kinds of runtime errors: 1. stack overflow: java.lang.StackOverflowError 2. out of heap space: java.lang.OutOfMemoryError Your task: identify the kind of error caused by the following programs. //Program 1 public static void main(String[] args) { Object[] arr1 = new Object[10000000]; Object[] arr = arr1; for (int i=0;i<10000;i++) { arr[0]=new Object[10000000]; arr=(Object[])arr[0]; } }
Last modified: 26‐Feb‐2016 //Program 2
private static int factorial(int n) { if (n==1) return 1; else return n*factorial(n+1); } public static void main(String[] args) { int f = factorial(4); System.out.println(f); }
4
Download