In: Computer Science
For today's lab we will be using the Car Object Class which we built in Lab 1. I have attached my solution if you would prefer to use my solution instead of your own solution.
We will working on remembering how to build sub-classes and user interfaces. So we will be creating new Java files for:
As your are building/updating your objects, remember to test. You'll need update the Driver to test your new methods and Objects!
Submit all of your .java files in one jar or zip file.
Grading Breakdown:
Here is the Car Object pre-made:
public class Car
{
private String name;
private int currentSpeed;
public Car(String inName)
{
name = inName;
}
public void accelerate()
{
currentSpeed += 10;
}
public void park()
{
currentSpeed = 0;
}
public void printCurrentSpeed()
{
System.out.println("Current Speed is: " + currentSpeed);
}
}
Here is the driver that is supposed to be used:
public class Driver {
public static void main(String[] args) {
// create new Audi car
Car audi = new Car("Audi");
// create new Nissan car
Car nissan = new
Car("Nissan");
// print current speed of Audi - it
is 0
audi.printCurrentSpeed();
// call the accelerate method twice
on Audi
audi.accelerate();
audi.accelerate();
// call the accelerate method once
on Nissan
nissan.accelerate();
// print current speed of Audi - it
is now 20 mpH
audi.printCurrentSpeed();
// print current speed of Nissan -
it is 10 mpH
nissan.printCurrentSpeed();
// now park the Audi car
audi.park();
// print current speed of Audi - it
is now 0, because the car is parked
audi.printCurrentSpeed();
}
}
car.java
package car;
class Car implements GreenHouseGasser{
private String name;
private int currentSpeed; /// speed is in milege per hour
private double distance; /// distance travelled by Car in
miles
public Car(String inName)
{
name = inName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCurrentSpeed() {
return currentSpeed;
}
public void setCurrentSpeed(int currentSpeed) {
this.currentSpeed = currentSpeed;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
/// method to accelerate by 10 mph
public void accelerate()
{
currentSpeed += 10;
}
/// method to increment distance when Car runs for given
time
public void incrementDistance(double time){
///distance=speed*time
distance=distance+(this.getCurrentSpeed()*time);
}
public void park()
{
currentSpeed = 0;
}
public void printCurrentSpeed()
{
System.out.println("Current Speed is: " + currentSpeed);
}
/// method to produce 1.5 Co2
public double CO2(){
return this.distance*1.5;
}
}
/// GreenHouseGasser.java
package car;
public interface GreenHouseGasser {
double CO2();
}
///Driver.java
package car;
class SportsCar extends Car{
public SportsCar(String inName) {
super(inName);
}
/// overidng Co2 method of Car as sports car releases 3.25 units
co2 per mile
@Override
public double CO2() {
return 3.25*this.getDistance();
}
/// overidng accelerate method of Car as SportsCar accelerated
speed by 20% of actual speed
@Override
public void accelerate() {
// super.accelerate(); //*A* /// increase normal cars speed by
10
//this.setCurrentSpeed( (int)(this.getCurrentSpeed()*1.25)); ///
*B* increment speed by 2 times car speed
this.setCurrentSpeed( (int)(this.getCurrentSpeed()+ 20));
}
}//SportCar
class EcoCar extends Car{
public EcoCar(String inName) {
super(inName);
}
/// overidng Co2 method of EcoCar as eco car releases 0.33 units
co2 per mile
@Override
public double CO2() {
return 0.33*this.getDistance();
}
/// overidng accelerate method of Car as Eco car accelerated speed
by 25% of actual speed
@Override
public void accelerate() {
// super.accelerate(); //*A* /// increase normal cars speed by
10
this.setCurrentSpeed( (int)(this.getCurrentSpeed()+12.5));
}
}//EcoCar
public class Driver {
public static void main(String[] args) {
// create new Audi car
Car audi = new Car("Audi");
SportsCar nissan = new SportsCar("Nissan");
EcoCar ecoCar=new EcoCar("ecocar");
System.out.println("Audi current spead = ");
audi.printCurrentSpeed();
System.out.println("Nissan current spead = ");
nissan.printCurrentSpeed();
System.out.println("Ecocar current spead = ");
ecoCar.printCurrentSpeed();
audi.accelerate();
nissan.accelerate();
ecoCar.accelerate();
System.out.println("\nAudi current spead = ");
audi.printCurrentSpeed();
System.out.println("Nissan current spead = ");
nissan.printCurrentSpeed();
System.out.println("Ecocar current spead = ");
ecoCar.printCurrentSpeed();
/// lets assume all three cars run for time = 1 hr
//hence all cars calls incrementDistance for 1 hr
audi.incrementDistance(1);
nissan.incrementDistance(1);
ecoCar.incrementDistance(1);
System.out.println("Audi current distance travelled =
"+audi.getDistance());
System.out.println("Nissan current distance travelled =
"+nissan.getDistance());
System.out.println("Ecocar current distance travelled =
"+ecoCar.getDistance());
System.out.println("Audi co2 produced = "+audi.CO2());
System.out.println("Nissan co2 produced = "+nissan.CO2());
System.out.println("Ecocar co2 produced = "+ecoCar.CO2());
audi.accelerate();
nissan.accelerate();
ecoCar.accelerate();
System.out.println("\nAudi current spead = ");
audi.printCurrentSpeed();
System.out.println("Nissan current spead = ");
nissan.printCurrentSpeed();
System.out.println("Ecocar current spead = ");
ecoCar.printCurrentSpeed();
/// lets assume all three cars run for time = 2 hr
//hence all cars calls incrementDistance for 2 hr
audi.incrementDistance(2);
nissan.incrementDistance(2);
ecoCar.incrementDistance(2);
System.out.println("Audi current distance travelled =
"+audi.getDistance());
System.out.println("Nissan current distance travelled =
"+nissan.getDistance());
System.out.println("Ecocar current distance travelled =
"+ecoCar.getDistance());
System.out.println("Audi co2 produced = "+audi.CO2());
System.out.println("Nissan co2 produced = "+nissan.CO2());
System.out.println("Ecocar co2 produced = "+ecoCar.CO2());
audi.accelerate();
nissan.accelerate();
ecoCar.accelerate();
System.out.println("\nAudi current spead = ");
audi.printCurrentSpeed();
System.out.println("Nissan current spead = ");
nissan.printCurrentSpeed();
System.out.println("Ecocar current spead = ");
ecoCar.printCurrentSpeed();
/// lets assume all three cars run for time = 1.5 hr
//hence all cars calls incrementDistance for 1.5 hr
audi.incrementDistance(1.5);
nissan.incrementDistance(1.5);
ecoCar.incrementDistance(1.5);
System.out.println("Audi current distance travelled =
"+audi.getDistance());
System.out.println("Nissan current distance travelled =
"+nissan.getDistance());
System.out.println("Ecocar current distance travelled =
"+ecoCar.getDistance());
System.out.println("Audi co2 produced = "+audi.CO2());
System.out.println("Nissan co2 produced = "+nissan.CO2());
System.out.println("Ecocar co2 produced = "+ecoCar.CO2());
// now park the Audi car
audi.park();
nissan.park();
ecoCar.park();
System.out.println("\nAudi current spead = ");
audi.printCurrentSpeed();
System.out.println("Nissan current spead = ");
nissan.printCurrentSpeed();
System.out.println("Ecocar current spead = ");
ecoCar.printCurrentSpeed();
}
}
/// output:
run:
Audi current spead =
Current Speed is: 0
Nissan current spead =
Current Speed is: 0
Ecocar current spead =
Current Speed is: 0
Audi current spead =
Current Speed is: 10
Nissan current spead =
Current Speed is: 20
Ecocar current spead =
Current Speed is: 12
Audi current distance travelled = 10.0
Nissan current distance travelled = 20.0
Ecocar current distance travelled = 12.0
Audi co2 produced = 15.0
Nissan co2 produced = 65.0
Ecocar co2 produced = 3.96
Audi current spead =
Current Speed is: 20
Nissan current spead =
Current Speed is: 40
Ecocar current spead =
Current Speed is: 24
Audi current distance travelled = 50.0
Nissan current distance travelled = 100.0
Ecocar current distance travelled = 60.0
Audi co2 produced = 75.0
Nissan co2 produced = 325.0
Ecocar co2 produced = 19.8
Audi current spead =
Current Speed is: 30
Nissan current spead =
Current Speed is: 60
Ecocar current spead =
Current Speed is: 36
Audi current distance travelled = 95.0
Nissan current distance travelled = 190.0
Ecocar current distance travelled = 114.0
Audi co2 produced = 142.5
Nissan co2 produced = 617.5
Ecocar co2 produced = 37.620000000000005
Audi current spead =
Current Speed is: 0
Nissan current spead =
Current Speed is: 0
Ecocar current spead =
Current Speed is: 0
BUILD SUCCESSFUL (total time: 0 seconds)