In: Computer Science
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 every
constructor, and instead call one constructor from another one when
needed to prevent code
repetition.
3- Provide required accessor (getter) and mutator (setter) methods.
Especially, accelerate, decelerate,
turnOn, turnoff, stop, etc.
4- Test the class by creating a tested class. Inside the tester
class, create couple of Car’s instance objects
and change their statuses by calling corresponding methods, and
compare the actual and expected
outputs.
5- Provide standard Java documentation for the Car class and create
the html for the documentation.
6- Modify the Car class by adding properties, fuel efficiency,
measured in liters/km, and a certain
amount of fuel in the gas tank. Provide a constructor to initialize
a specific fuel efficiency and also
the initial fuel level as 0. Supply a method drive that simulates
driving the car for a certain distance,
reducing the amount of gasoline in the fuel tank. Also, supply
methods getGasInTank, returning the
current amount of gasoline in the fuel tank, and addGas, to add
gasoline to the fuel tank.
You may assume that the drive method is never called with a
distance that consumes more than the
available gas. After implementation the class, test it again in the
CarTester class that tests all the new
methods.
public class Car {
private String make;
private String model;
private int year;
private int price;
private double speed;
private double maxspeed;
public Car(String make, String model, int year, double speed, double maxspeed) {
this.make = make;
this.model = model;
this.year = year;
this.speed = speed;
this.maxspeed = maxspeed;
}
public void printInfo(){
System.out.println("Print information about the car:");
System.out.println("Make: " + this.make);
System.out.println("Model: " + this.model);
System.out.println("Year: " + this.year);
System.out.println("speed: " + this.speed);
System.out.println("maxspeed: " + this.maxspeed);
System.out.println();
}
public void isOn(){
System.out.println("Car is on.");
}
public void isMoving() {
System.out.println("Car is moving.");
}
public String getmake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public double getspeed() {
return speed;
}
public double getmaxspeed() {
return maxspeed;
}
public void setspeed(int maxspeed) {
this.maxspeed = maxspeed;
}
public void accelerate(){
speed += 5;
//if (speed >= 0) && (speed <= maxspeed)
}
public void brake(){
speed -= 5;
}
}