Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.

Similar presentations


Presentation on theme: "CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism."— Presentation transcript:

1 CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

2 CS1101X Recitation #102 Qn 1: Class F (1/2) Suppose class F has the following definition: public class F { private int value; public F(int x) { value = x; System.out.println("Made F: " + value); }

3 CS1101X Recitation #103 Qn 1: Class F (2/2) What is the output of the following program? public class G extends F { private F value; public G(int x, int y) { super(x); value = new F(y); } public static void main(String[] args) { G g = new G(11, 28); } +

4 CS1101X Recitation #104 Qn 2: Class Y (1/5) Given this class X: public class X { // default constructor public X() { // no body needed } // isX(): class method public static boolean isX(Object v) { return (v instanceof X); } // isObject(): class method public static boolean isObject(X v) { return (v instanceof Object); }

5 CS1101X Recitation #105 Qn 2: Class Y (2/5) And this class Y: public class Y extends X { // Y(): default constructor public Y() { // no body needed } // isY(): class method public static boolean isY(Object v) { return (v instanceof Y); }

6 CS1101X Recitation #106 Qn 2: Class Y (3/5) Class Y (continued): public static void main(String[] args) { X x = new X(); Y y = new Y(); X z = y; System.out.println("x is an Object: " X.isObject(x)); System.out.println("x is an X: " + X.isX(x)); System.out.println("x is a Y: " + Y.isY(x)); System.out.println(); System.out.println("y is an Object: " + X.isObject(y)); System.out.println("y is an X: " + X.isX(y)); System.out.println("y is a Y: " + Y.isY(y)); System.out.println(); System.out.println("z is an Object: " + X.isObject(z)); System.out.println("z is an X: " + X.isX(z)); System.out.println("z is a Y: " + Y.isY(z)); }

7 CS1101X Recitation #107 Qn 2: Class Y (4/5) Which of the following statements could be the fourth statement in method main() ? What would be the output? Explain. public static void main(String[] args) { X x = new X(); Y y = new Y(); X z = y; // fourth statement here... } a)x = y; b)x = (X) y; c)y = x; d)y = (Y) x;

8 CS1101X Recitation #108 Qn 2: Class Y (5/5) Answers: +

9 CS1101X Recitation #109 Qn 3: Colored3DPoint (1/2) Suppose the following method main() was added to class Colored3DPoint. What would the output be? public static void main(String[] args) { Colored3DPoint c = new Colored3DPoint(); Colored3DPoint d = new Colored3DPoint(1, 2, 3, color.BLACK); Colored3DPoint e = (Colored3DPoint) d.clone(); System.out.println(c); System.out.println(d); System.out.println(e); System.out.println(d.equals(c)); System.out.println(d.equals(e)); }

10 CS1101X Recitation #1010 Qn 3: Colored3DPoint (2/2) Output: +

11 CS1101X Recitation #1011 Qn 4: Sum of two elements (1/5) Given this problem: A sorted list of integers list and a value is given. Write an algorithm to find the subscripts of (any) two distinct elements in the list whose sum is equal to the given value. Example: list: 2, 3, 8, 12, 15, 19, 22, 24 value: 23 answer: elements 8 (at subscript 2) and 15 (at subscript 4) Write an efficient code for this problem. What is the running-time of your algorithm?

12 CS1101X Recitation #1012 Qn 4: Sum of two elements (2/5) Sample run: Enter number of elements: 8 Enter elements (in non-decreasing order): 2 3 8 12 15 19 22 24 Enter sum: 23 Answer: 2, 4

13 CS1101X Recitation #1013 Qn 4: Sum of two elements (3/5) import java.util.*; class SumTwoElements { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] intArray = createArray(scanner); // printArray(intArray); // for checking System.out.print("Enter sum: "); int sum = scanner.nextInt(); search(intArray, sum); }

14 CS1101X Recitation #1014 Qn 4: Sum of two elements (4/5) public static int[] createArray(Scanner scan) { System.out.print("Enter number of elements: "); int n = scan.nextInt(); System.out.println( "Enter elements (in non-decreasing order): "); int arr[] = new int[n]; for (int i = 0; i < n; ++i) { arr[i] = scan.nextInt(); } return arr; } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; ++i) System.out.print(arr[i] + " " ); System.out.println(); }

15 CS1101X Recitation #1015 Qn 4: Sum of two elements (5/5) + public static void search(int[] arr, int sum) { }

16 CS1101X Recitation #1016 End of Recitation #10


Download ppt "CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism."

Similar presentations


Ads by Google