Back to index

plt-scheme  4.2.1
intermediateTest.java
Go to the documentation of this file.
00001 // 14 checks; 2 failures
00002 // 6 tests; no failures
00003 // Order of calling testMethods crucial for test success
00004 
00005 interface Automobile {
00006   int milesTraveled();
00007   void travel( int miles );
00008 }
00009 
00010 abstract class Auto implements Automobile { 
00011   int miles;
00012   int milesTraveled() { return miles; }
00013   
00014   void travel(int miles) {
00015     this.miles = this.miles + miles;
00016   }
00017 }
00018 
00019 class Car extends Auto {
00020   
00021   double basePrice;
00022   
00023   Car(int miles, double basePrice) {
00024     this.miles = miles;
00025     this.basePrice = basePrice;
00026   }  
00027   
00028   double price(int year) {
00029     if ((2006 - year) == 0) {
00030       return this.basePrice;
00031     } else {
00032       if ((2006 - year) > 0) {
00033         return this.basePrice - (this.basePrice / (2006 - year));
00034       } else {
00035         return this.basePrice + (this.basePrice / (year - 2006));
00036       }
00037     }
00038   }
00039   
00040 }
00041 
00042 class CarExamples {
00043   
00044   Car myCar = new Car(100000, 16000.00);
00045   Car momCar = new Car(10000, 32000.00);
00046   
00047   boolean test1 = check this.myCar expect this.momCar;
00048   boolean test2 = check this.myCar.milesTraveled() expect 100000;
00049   
00050   boolean testTravel() {
00051     myCar.travel(10);
00052     return (check this.myCar expect new Car(100010, 16000.00));
00053   }
00054   
00055   boolean testTravel2() {
00056     myCar.travel(10);
00057     return (check this.myCar expect new Car(100020, 16000.00));
00058   }
00059   
00060   boolean testPrice() {
00061     return (check this.myCar.price(2006) expect 16000.00 within .01) &&
00062     (check this.myCar.price(1991) expect 14933.33 within .01) &&
00063     (check this.myCar.price(2007) expect 32000.00 within .01);
00064   }
00065   
00066 }
00067 
00068 class Truck extends Auto {
00069   String make;
00070   int numDoors;
00071   boolean extendedBed;
00072   double basePrice;
00073   
00074   Truck( String make, int miles, int numDoors, boolean bed, double basePrice) {
00075     this.make = make;
00076     this.miles = miles;
00077     this.numDoors = numDoors;
00078     this.extendedBed = bed;
00079     this.basePrice = basePrice;
00080   }
00081   
00082   String makeAndModel() {
00083     if (this.extendedBed) {
00084       return this.make.concat("Extended");
00085     } else {
00086       return this.make.concat(String.valueOf(this.numDoors));
00087     }
00088   }
00089   double price( int year ) {
00090     // Uncomment to test runtime error behavior
00091     //return this.basePrice - (2 * (this.basePrice / (2006 -year)));
00092     if (year == 2006) {
00093       return this.basePrice;
00094     } else {
00095       return this.basePrice - (2 * (this.basePrice / (2006 - year)));
00096     }
00097   }
00098   
00099 }
00100 
00101 class TruckExamples {
00102   Truck oneTruck = new Truck("Toyota",10000, 2,false,20000.00);
00103   Truck twoTruck = new Truck("Ford",100000,2,true,35000.00);
00104   
00105   boolean test1 = check this.oneTruck.milesTraveled() expect 10000;
00106   boolean test2 = check this.oneTruck expect this.twoTruck;
00107   
00108   TruckExamples() { }
00109   
00110   boolean testPrice() {
00111     return (check this.oneTruck.price(2006) expect 20000.00 within .01) &&
00112            (check this.oneTruck.price(1996) expect 16000.00 within .01);
00113   }
00114   
00115   boolean testTravel() {
00116     oneTruck.travel(1000);
00117     return check this.oneTruck expect new Truck("Toyota",11000,2,false,20000.00);
00118   }
00119   
00120   boolean testMakeAndModel() {
00121     return (check this.oneTruck.makeAndModel() expect "Toyota2") &&
00122            (check this.twoTruck.makeAndModel() expect "FordExtended");
00123   }
00124   
00125 }