Title: CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism
1CS1101X Programming MethodologyRecitation 10
Inheritance and Polymorphism
2Qn 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)
3Qn 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
4Qn 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)
5Qn 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)
6Qn 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))
7Qn 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
8Qn 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.
9Qn 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))
10Qn 3 Colored3DPoint (2/2)
Output
class Colored3DPoint0, 0, 0, java.awt.Colorr0,
g0,b255 class Colored3DPoint1, 2, 3,
java.awt.Colorr0,g0,b0 class
Colored3DPoint1, 2, 3, java.awt.Colorr0,g0,b0
false true
11Qn 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?
12Qn 4 Sum of two elements (2/5)
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
13Qn 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)
14Qn 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 intn for (int i 0 i lt
n i) arri scan.nextInt()
return arr public static void
printArray(int arr) for (int i 0 i lt
arr.length i) System.out.print(arri
" " ) System.out.println()
15Qn 4 Sum of two elements (5/5)
public static void search(int arr, int sum)
if (arr.length gt 2) int left
0 int right arr.length - 1
while (left lt right) if
(arrleft arrright sum)
System.out.println("Answer " left ", "
right) return
else if (arrleft arrright lt sum)
left else //
arrleft arrright gt sum
right--
System.out.println("No solution.")
16End of Recitation 10