cs1101x: programming methodology recitation 10 inheritance and polymorphism

16
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

Upload: erika-hunter

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X: Programming Methodology

Recitation 10 Inheritance and Polymorphism

Page 2: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 2

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); }}

Page 3: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 3

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); }}

+

Page 4: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 4

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);}

}

Page 5: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 5

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);}

Page 6: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 6

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));}

}

Page 7: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 7

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;

Page 8: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 8

Qn 2: Class Y (5/5)Answers:

+

Page 9: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 9

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));}

Page 10: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 10

Qn 3: Colored3DPoint (2/2)

Output:

+

Page 11: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 11

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?

Page 12: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 12

Qn 4: Sum of two elements (2/5)

Sample run:

Enter number of elements: 8Enter elements (in non-decreasing order):2 3 8 12 15 19 22 24Enter sum: 23Answer: 2, 4

Page 13: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 13

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); }

Page 14: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 14

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(); }

Page 15: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 15

Qn 4: Sum of two elements (5/5)

+

public static void search(int[] arr, int sum) {

}}

Page 16: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #10 16

End of Recitation #10