Back to index

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