Modified:
Do Question 8 first, it should help in the following.
public int highestPrice() {
int highest = tmA[0].getPrice();
for (int i=0; i<tmA.length; i++)
if (
tmA[i].getPrice() > highest )
highest = tmA[i].getPrice();
return highest;
}
|
|
|
A a7 = new A("red"); 3-6
a7.print(); 7-10
B b7 = new B(15.0, "white"); 14-16, 3-6, 17-18
b7.print(); 19-21, 7-10, 22-23
Seven seven7 = new Seven(); 26-28
seven7.printA( a7 ); 29-31, 7-10, 32
seven7.printB( b7 ); 33-35, 19-21, 7-10, 22-23, 36
seven7.printA( b7 ); 29-31, 19-21, 7-10, 22-23, 32
seven7.printB( a7 ); Invalid, cannot pass a super-type to a subtype.
A j = b7; Valid, no lines executed.
B k = a7; Invalid, cannot assign a super-type to a subtype.
B k = (B) a7; Valid, no lines executed.
seven7.printA( j ); 29-31, 19-21, 7-10, 22-23, 32
seven7.printB( k );
Run time error, k references a super-type a7 from Question l.
(16) What is the result of the following fragments?
- for (int i=0; i<3; i++) i = 0 i = 1 i = 2
{
System.out.print(" i =");
System.out.print( i );
}
- for (int i=0; i<3; i++) compile error because i is not defined outside for-statement
System.out.print(" i =");
System.out.print( i );
- public boolean test() { false
int x = 3;
if (x == 4)
return true;
return false;
}
- public boolean test() { false
int x = 3;
return x == 4;
}
- int [] a = new int[ 4 ];
a[0] = 30;
a[1] = 40;
a[2] = 25;
a[3] = 16;
int x = a[0];
for (int i=0; i<a.length; i++)
if ( a[i] > x )
x = a[ i ];
System.out.println( x ); 40
- TicketMachine [] a = new TicketMachine[ 4 ];
a[0] = new TicketMachine(30);
a[1] = new TicketMachine(40);
a[2] = new TicketMachine(25);
a[3] = new TicketMachine(16);
TicketMachine x = a[ 0 ];
for (int i=0; i<a.length; i++)
if ( a[i].getPrice() > x.getPrice() )
x = a[ i ];
System.out.println( x.getPrice() ); 40
- TicketMachine [] a = new TicketMachine[ 4 ];
a[0] = new TicketMachine(30);
a[1] = new TicketMachine(40);
a[2] = new TicketMachine(25);
a[3] = new TicketMachine(16);
int x = a[ 0 ].getPrice();
for (int i=0; i<a.length; i++)
if ( a[i].getPrice() > x )
x = a[ i ].getPrice();
System.out.println( x ); 40
- ArrayList <TicketMachine> a = new ArrayList<TicketMachine>();
a.add( new TicketMachine(30));
a.add( new TicketMachine(40));
a.add( new TicketMachine(25));
a.add( new TicketMachine(16));
int x = a.get(0).getPrice();
for (int i=0; i<a.size(); i++)
if ( a.get( i ).getPrice() > x )
x = a.get( i ).getPrice();
System.out.println( x ); 40