Question

In: Computer Science

For today's lab we will be using the Car Object Class which we built in Lab...

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:

  • An Interface called GreenHouseGasser
    • Requires a method called CO2() which returns how much carbon dioxide the Object produces.
    • Make sure to update Car so that Car implements GreenHouseGasser. Cars should produce 1.5 units of CO2 for every mph the car is traveling.
  • A Car sub-class called SportsCar:
    • Overrides the accelerate() method. SportsCars accelerate twice as fast as regular Cars
    • Overrides the CO2() method. SportsCars should be using a factor of 3.25 units of CO2 for every mph
  • A Car sub-class called EcoCar
    • Overrides the accelerate() method. EcoCars accelerate 25% as fast as regular Cars
    • Overrides the CO2() method. EcoCars should be using a factor of .33 units of CO2 for every mph

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:

  • Driver tests all new methods and objects -- 25%
  • Interface created correctly and Car updated to implement the Interface -- 25%
  • SportsCar correctly created -- 20%
  • EcoCar correctly created -- 20%
  • JavaDoc comments! -- 10%

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();
   }
}

Solutions

Expert Solution

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)


Related Solutions

We started creating a Java class for Car. In this lab we are going to complete...
We started creating a Java class for Car. In this lab we are going to complete it in the following steps: 1- First create the Car class with some required instance variables, such make, model, year, price, speed, maxSpeed, isOn, isMoving, and any other properties you like to add. 2- Provide couple of constructors for initializing different set of important properties, such as make, model, year, and price. Make sure that you do not repeat initialization of instance variables in...
In the previous lab you created a Car class and a Dealership class. Now, in this...
In the previous lab you created a Car class and a Dealership class. Now, in this activity change the design of the Dealership class, in which the list of the cars inside a dealership will be stored in an ArrayList. Then, provide required getter and setter methods to keep the records of all its cars. In your tester class, test your class and also printout the list of all cars of a given dealership object. // Java program import java.util.ArrayList;...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
In the Force lab, we use a small lab car with mass m=5 Kg, the experiment...
In the Force lab, we use a small lab car with mass m=5 Kg, the experiment is on a horizontal table, on the car is acting a pulling force F= 20 N and the kinetic friction force Fr is acting as well. The car initially is at rest, and take 6.0 seconds to reach the velocity of 8.5 m/s. a)       Calculate the magnitude of the friction force Fr acting on the car? b)      We remove the pulling force when the...
Write a program to have a Car class which is comparable. Make the Car class comparable...
Write a program to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling a static...
6. As a class, we own and operate four car washes in Tucson. We notice that...
6. As a class, we own and operate four car washes in Tucson. We notice that fewer people are out driving right now, so we might have to make the difficult decision to down-size our operation by closing a location. We gather the following information about each location. Location Washes Oracle 213 Tangerine 170 Kolb 201 Speedway 216 At the 0.05 significance level, is there a difference in the number of full-service washes purchased at each location? (A) Write out...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
Which of this method of class String is used to obtain a length of String object?...
Which of this method of class String is used to obtain a length of String object? What is the output of the below Java program with WHILE, BREAK and CONTINUE? int cnt=0; while(true) { if(cnt > 4)    break;    if(cnt==0) {     cnt++; continue; }   System.out.print(cnt + ",");   cnt++; } 1,2,3,4 Compiler error 0,1,2,3,4, 1,2,3,4,
create a project and in it a class with a main. We will be using the...
create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?"); int w = scan.nextInt();...
// Creates a Breakfast class // and instantiates an object // Displays Breakfast special information using...
// Creates a Breakfast class // and instantiates an object // Displays Breakfast special information using System; using static System.Console; using System.Globalization; class DebugNine2 {    static void Main()    {       Breakfast special = new Breakfast("French toast", 4.99);       //Display the info about breakfast       WriteLine(special.INFO);       // then display today's special       WriteLine("Today we are having {0} for {1}",          special.Name, special.Price.ToString("C2", CultureInfo.GetCultureInfo("en-US")));   } } class Breakfast {    public string INFO =       "Breakfast is the most important meal of the day.";       // Breakfast constructor requires...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT