2005 Tickets
<< 2006Customer | APQuestionsTrailIndex | 2005Average >>
Ticket
public abstract class Ticket {
private int serialNumber;
public Ticket()
{ serialNumber= getNextSerialNumber(); }
public abstract double getPrice();
public String toString()
{ return "\nNumber: " + serialNumber + "\nPrice: " + getPrice();}
private static int getNextSerialNumber()
{ return (int)(Math.random()*10000); }
}
Advance
public class Advance extends Ticket{
int daysBeforeShow;
public Advance(int d)
{ super();
daysBeforeShow = d;
}
public double getPrice(){
if (daysBeforeShow >9)
return 30.00;
return 40.00;
}
}
Note: In class we had something that didn't quite work... can you see the subtle difference and what is wrong with this?
//INCORRECT VERSION:
public class Advance extends Ticket{
int daysBeforeShow;
public Advance(int d)
{ super();
int daysBeforeShow = d;
}
public double getPrice(){
if (daysBeforeShow >9)
return 30.00;
return 40.00;
}
}
StudentAdvance
public class StudentAdvance extends Advance {
public StudentAdvance(int d) { super(d); }
public double getPrice()
{
return 0.5*super.getPrice();
}
public String toString()
{
return super.toString()+"\n(Student ID Required)";
}
}
A class to test out the Ticket Stuff
public class TicketTester
{
public static void main(String[] args)
{
Advance late=new Advance(2);
Advance ok=new Advance(10);
StudentAdvance lateStudent=new StudentAdvance(2);
StudentAdvance okStudent=new StudentAdvance(10);
System.out.println("Late price (should be 40): "+late);
System.out.println("adv price (should be 30): "+ok);
System.out.println("Late Student price (should be 20): "+lateStudent);
System.out.println("Student advance price (should be 15): "+okStudent);
}
}
