Chapter-9

Chapter-9 - Exercise 1 public class Circle cfw_ private int...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University public class Circle { private int radius; public Circle( int r ) { radius = r; } public String toString() { return “Radius: “ + radius ; } } Exercise 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Exercise 1 (contd.) import java.util.ArrayList; public class Foo { public static void main( String[] args ) { ArrayList<Circle> circles = new ArrayList<Circle>(); circles .add( new Circle(5) ); for( Circle c : circles ) System.out.println( c ); } } output ??
Background image of page 2
3 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Fruit +getColor(): String Grape +Grape(r: String) +getColor() : String - region: String public String getColor() { return “Green”; } public String getColor() { if( region.equals(“Florida”)) return “Red”; else return super.getColor (); } Exercise 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Exercise 2 (contd.) public class TestMethodOverriding { public static void main( String[] args ) { Grape g1 = new Grape( “Ottawa” ); Grape g2 = new Grape( “Florida” ); System.out.println( g1.getColor() ); System.out.println( g2.getColor() ); } } Output ??
Background image of page 4
5 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University Exercise 3 True or False ?? • A Car has an “is-a” relationship with Wheel and  Brake class. • Constructors are not inherited by subclasses. • A superclass’s  private members can be accessed  in the subclass declaration. Answer: false Answer: true Answer: false
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University public class A1 { public int x; private int y; protected int z; } public class A2 extends A1 { protected int a; private int b; } Exercise 4 public class A3 extends A2 { private int q; } Q) Which of the following is true ? y of class A1 is : a) accessible in A1, A2 and A3 b) accessible in A1 and A2 c) accessible only in A1 d) accessible only in A3 e) not accessible to any of the three classes Answer: (c)
Background image of page 6
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University public class A1 { public int x; private int y; protected int z; } public class A2 extends A1 { protected int a; private int b; } Exercise 5 public class A3 extends A2 { private int q; } Q) Which of the following lists of instance variables are accessible in class A2? a) x, y, z, a, b
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 30

Chapter-9 - Exercise 1 public class Circle cfw_ private int...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online