Cars Design: You will design and implement classes for cars. There are three typ
ID: 3539149 • Letter: C
Question
Cars Design:
You will design and implement classes for cars. There are three types of cars: Sedan, Compact, and Sports. Each car contains an integer amount of gas. All cars respond to the gas feature, which increments the amount of gas by 1 up to a maximum of 50, and prints out "Gas!" to the standard output. Except the Compact car, which after printing "Gas!" also prints "Well, I have to work hard again". All cars respond to the accelerate feature, which decreases the amount of the gas by 1 and increases the speed of a car by 1. A car prints out "Faster!" when it receives the accelerate feature. However, Sports cars get so excited that they prints "It really feels good!" which costs the Sports car two more units of gas, but increases the speed by another one. All
the cars respond to the brake feature, which decreases the amount of the gas, as well as speed, by 1. The speed is represented by an integer between 0 and 200, except for a Sports car, which can reach 300. When the speed is greater than (3 gas %uDBC0%uDC00 50), a car responds by printing "Speeding!" A Sedan car will further complain "Why hurry?" The exertion of printing the extra words costs Sedan an additional unit of gas.
Also, implement your classes in Eiel with suitable contracts and invariants. Your design should be general enough to easily add new types of cars.
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Car {
String Make;
String Model;
int Year;
public Car (String MakeCar, String CarModel, int YearCar) {
Make=MakeCar;
Model=CarModel;
Year=YearCar;
}
public String getMake () {
return Make;
}
public String getModel () {
return Model;
}
public int getYear() {
return Year;
}
}
While this is from "CarTest"
import java.util.Scanner;
public class CarTest
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
System.out.println ("Please enter the Make of the car: ");
Make = scan.nextLine();
System.out.println ("Please enter the Model of the car: ");
Model = scan.nextInt();
System.out.println("Please enter the Year of the car: ");
}
}