160 likes | 275 Vues
This document explores essential concepts of inheritance and polymorphism in Java through various classes and methods. It begins with class F that demonstrates basic inheritance, followed by class G which extends F, showcasing the implications of constructor initialization. The discussion then transitions to class Y, emphasizing the usage of instanceof and casting in Java. Additionally, it includes practical examples and outputs to clarify these concepts. Finally, it presents the algorithmic challenge of finding two distinct elements in a sorted list that sum up to a specified value, complete with code implementation.
E N D
CS1101X: Programming MethodologyRecitation 10 Inheritance and Polymorphism
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); } }
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); } } Made F: 11 Made F: 28 +
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); } }
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); }
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)); } }
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 . . . } • x = y; • x = (X) y; • y = x; • y = (Y) x;
Qn 2: Class Y (5/5) Answers: a) x = y; This state is legal. It assigns a subclass object to a superclass variable. x is an Object: true x is an X: true x is a Y: true b) x = (X) y; This state is legal. It casts a subclass object to an object of its superclass, then assigns that value to a superclass variable. y is an Object: true y is an X: true y is a Y: true c) y = x; This statement is illegal. It attempts to assign a superclass object to a subclass variables, which is not permitted. d) y = (Y) x; This statement is illegal. It attempts to cast a superclass object to an object of its subclass, which is not permitted. +
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)); }
Qn 3: Colored3DPoint (2/2) Output: class Colored3DPoint[0, 0, 0, java.awt.Color[r=0,g=0,b=255]] class Colored3DPoint[1, 2, 3, java.awt.Color[r=0,g=0,b=0]] class Colored3DPoint[1, 2, 3, java.awt.Color[r=0,g=0,b=0]] false true +
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?
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
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); }
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(); }
Qn 4: Sum of two elements (5/5) public static void search(int[] arr, int sum) { if (arr.length >= 2) { int left = 0; int right = arr.length - 1; while (left < right) { if (arr[left] + arr[right] == sum) { System.out.println("Answer: " + left + ", " + right); return; } else if (arr[left] + arr[right] < sum) left++; else // arr[left] + arr[right] > sum right--; } } System.out.println("No solution."); } } +