Question
Modify the Car class to include: -equals -interface Write the main program to create an array of size 5 of type Car. Create 5 car
Modify the Car class to include:
-equals
-interface
Write the main program to create an array of size 5 of type Car. Create 5 car objects having each location of the array to refer to one of the cars. Test the pumpGas, goFast, equals method on the array items. Write an enhanced loop to print all the car values (using a toString written last time).
package cartest;
public class Car {
private int tank;
private int speed;
public Car(){
tank = 0;
speed = 0;
}
public int pumpGas(int gas) {
int extra;
if (gas + tank > 20) {
extra = 20-tank;}
else {
extra = gas;
}
tank += gas;
return 4 * gas;
}
public void goFast() {
speed += 5;
}
public String toString() {
return "Car tank is " + tank + ", speed is " + speed + "";
}
}
package cartest;
public class CarTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Car car = new Car();
System.out.println(car.pumpGas(15));
car.goFast();
System.out.println(car);
System.out.println(car.pumpGas(12));
car.goFast();
System.out.println(car);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started