1. (7) Rewrite the following class
definition to remove duplication.
|
public
class One public One (String theS, int theI) public void a () |
public
class One public One (String theS, int theI) public void ab ()
|
2. (8) Eliminate the explicit
coupling between the A2 and B2 classes.
|
public
class A2 { private String A; private B2 ab; public A2() { ab = new B2(); ab.B = "white"; A = ab.B; } } |
public
class A2 { |
|
public
class B2 { public String B; public B2() { B = "red"; } } |
public
class B2 { return B;
} |

3. (5) Give the inheritance hierarchy diagram for
the following:
class A extends Object { }
class B extends A { }
class C extends B { }
class D extends A { }
class E extends C { }
class F extends B { }
class G extends D { }
class H extends E { }
4. (10) Use inheritance to remove duplication.
The resulting classes A and B should still behave equivalently.
|
A |
B |
|
public
class A public A (String aS, int aI) public void ap
() |
public
class B public B (String theS, int theI) public void bp
() |
|
A |
B |
|
public
class A public A (String aS, int aI) public void ap
() public int
getI() |
public
class B extends A public B (String theS, int theI) public void bp
() |
OR
| C |
A |
B |
|
public
class C public C (String aS, int aI) public void
cp
() public int
getI() public int
getS()
|
public
class A extends C public void ap
() |
public
class B extends C public void bp
() |
5. (20) Indicate syntactically invalid
statements (i.e. errors detected by the compiler). Trace the line numbers
executed for valid statements.
a. TicketMachine tm = new TicketMachine( 50 ); _____6-11________
b. BusTM btm = new BusTM( 50, “A”, “X Y Z”
); ____26-28, 6-11, 29-31____
c.
tm.insertMoney( 100 ); _____12-15______
d. btm.insertMoney( 100 ); _____12-15______
e. tm.printStops( ); ______invalid ____
f.
btm.printStops( ); _____32-36 _____
g. tm.printTicket( ); _____16-21______
h. btm.printTicket( ); _____37-40, 16-21, 41_
i.
tm = btm; _____valid_____
j.
btm = tm; _____invalid____
6.
(12) True or False. Indicate syntactically valid
statements. For valid statements, indicate if an error occurs when executed.
public class Six
{
private ArrayList
<TicketMachine> aL;
private TicketMachine
tm;
private BusTM
btm;
public Six ()
{
aL = new ArrayList<TicketMachine>();
aL.add(new
TicketMachine( 50 ) );
aL.add(new
BusTM( 50, “A”, “X Y Z” ) );
Valid Execution
Syntax Error
a.
tm = aL.get( 0 ); _T__ _No__
b.
btm = aL.get(
1 ); _T__ _No__
c.
tm = aL.get( 0 ); _T__ _No__
d.
tm = aL.get( 1 ); _T__ _No__
e.
btm = aL.get( 0 ); _T__ _Yes__
f.
btm = aL.get( 1 ); _T__ _No__
7.
(15)
Give the output produced, if any, when the following is executed.
public class Seven
{
private TicketMachine
tm;
private BusTM
btm;
public Seven ()
{
tm
= new TicketMachine( 50 ) ;
btm = new BusTM( 50, “A”, “X Y Z”
) ;
a.
tm.printTicket(); _________# 50_________________
b.
btm.printTicket(); _________# Bus A
________ # 50______
c.
btm.printStops(); ________ # A
________#
Stops X Y Z_____
d.
tm = btm; ________________________________
e.
tm.printTicket(); _________# Bus
A
________ # 50______
8.
(23) Complete the definitions for the BusTerminal class.
a. Define an ArrayList field of type BusTM.
b. Define a BusTerminal
constructor of no parameters. The constructor initializes the ArrayList field and adds 3 BusTM objects to the ArrayList field. Chose appropriate parameters for constructing
BusTM
objects.
c.
Define a method that calls
the printStops() method on all BusTM objects of the ArrayList.
d. Define a method getAllNames() that returns the concatenation of the name field of all BusTM objects in the ArrayList.
e. Define a method lowestPrice() that returns the lowest value of the price field of all BusTM objects in the ArrayList.
public class BusTerminal {
// a.
private ArrayList <BusTM> al;
// b.
public BusTerminal() {
al = new ArrayList <BusTM>();
al.add( new BusTerminal(100, "Red Line", "34th Elm Stadium") );
al.add( new BusTerminal(200, "Blue Line", "28th Oak IUS") );
al.add( new BusTerminal(150, "Pink Line", "6th YMCA Bono") );
}
// c.
public void printAllStops() {
for( int i = 0; i<al.size(); i++)
al.get(i).printStops();
}
// d.
public String getAllNames() {
String result = "";
for( int i=0; i<al.size(); i++)
result = result + al.get(i).getName();
return result;
}
// e.
public int lowestPrice() {
int lowest = al.get(0).getPrice();
for( int i=0; i<al.size(); i++)
if( al.get( i ).getPrice() < lowest )
lowest = al.get( i ).getPrince();
}
}
|
1. public class TicketMachine 2. { 3. private int price; 4. private int balance; 5. private int total; 6. public TicketMachine(int ticketCost) 7. { 8. price
= ticketCost; 9. balance
= 0; 10. total
= 0; 11. } 12. public void insertMoney(int amount) 13. { 14. balance
+= amount; 15. } 16. public void printTicket() 17. { 18. System.out.println(“# “ + price ); 19. total
+= balance; 20. balance
= 0; 21. } 22. public int getPrice() 23. { 24. return
price; 25. } 26. } |
23. public class BusTM
extends TicketMachine
24. { 25. private
String name, stops; 26. public BusTM( int theCost,
String theName, 27. { 28.
super( theCost
); 29.
name = theName; 30.
stops = theStops; 31. } 32. public
void printStops() 33. { 34. System.out.println("# Bus " + name ); 35. System.out.println("# Stops " + stops ); 36. }
37. public
void printTicket() 38. { 39. System.out.println("# Bus " + name ); 40. super.printTicket(); 41. } 42. public String getName()
43. { 44. return
name; 45. } 46. } |