Refer to Example 1, motor car.
Write a class called MotorCar. Include:
a)Fields make, model, maximum speed, and tank capacity.
b)A constructor that initializes these fields.
c)Accessor methods that return the value of each variable.
d)Mutator methods - addPetrol that adds petrol in the tank, and drive that reduces the petrol by some amount.
Remember the conventions for naming the class, variables, and methods
Define a test class called TestMotorCar. Create two objects car1 and car2 with initial values shown in the table below
Make / Model / Maximum speed / Capacitycar1 / Ford / Mustang / 120 mph / 15 gallons
car2 / Toyota / Corolla / 150 mph / 12 gallons
- public class MotorCar
- {
- }
// Adding the variables
- public class MotorCar
- {
- private String make, model;
- private double engine, max_speed, tank;
- }
// Adding the constructor
- public class MotorCar
- {
- private String make, model;
- private double engine, max_speed, tank;
- public MotorCar(String mk, String mod, double spd, double tnk, double eng)
- {
- make = mk;
- model = mod;
- engine = eng;
- max_speed = spd;
- tank = tnk;
- }
- }
// Class MotorCar (Adding accessor method getMake())
- public class MotorCar
- {
- private String make, model;
- private double engine, max_speed, tank, hold;
- public MotorCar(String mk, String mod, double spd, double tnk, double eng)
- {
- make = mk;
- model = mod;
- engine = eng;
- max_speed = spd;
- tank = tnk;
- }
- public String getMake()
- {
- return make;
- }
- }
// Adding mutator method drive()
- public class MotorCar
- {
- private String make, model;
- private double engine, max_speed, tank;
- public MotorCar(String mk, String mod, double spd, double tnk, double eng)
- {
- make = mk;
- model = mod;
- engine = eng;
- max_speed = spd;
- tank = tnk;
- }
- public String getMake()
- {
- return make;
- }
- public void drive(double amount)
- {
- tank = tank - amount;
- }
- }
//Test Class TestMotorCar (version 1)
- class TestMotorcar
- {
- public static void main(String[] arg)
- {
- MotorCar car1 = new MotorCar("Ford", "Mustang", 120, 15, 3.5);
- MotorCar car2 = new MotorCar("Toyota", "Corolla", 150, 12, 2.2);
- System.out.println("Features of Cars");
- System.out.println("------");
- System.out.println("Name: " + car1.getMake() + " " + car1.getModel());
- System.out.println("Petrol tank capacity: " + car1.getTankCapacity() + " gallons");
- System.out.println("Maximum speed: " + car1.getMaximumSpeed() + " mph");
- System.out.println("------");
- System.out.println("Name: " + car2.getMake() + " " + car2.getModel());
- System.out.println("Petrol tank capacity: " + car2.getTankCapacity() + " gallons");
- System.out.println("Maximum speed: " + car2.getMaximumSpeed() + " mph");
- System.out.println("------");
- }
- }
//Test Class TestMotorCar (version 2 – make common code method)
- class TestMotorcar
- {
- public static void main(String[] arg)
- {
- MotorCar car1 = new MotorCar("Ford", "Mustang", 120, 15, 3.5);
- MotorCar car2 = new MotorCar("Toyota", "Corolla", 150, 12, 2.2);
- System.out.println("Features of Cars");
- report(car1);
- report(car2);
- System.out.println("------");
- }
- public static void report( MotorCar m)
- {
- System.out.println("------");
- System.out.println("Name: " + m.getMake() + " " + m.getModel());
- System.out.println("Petrol tank capacity: " + m.getTankCapacity() + " gallons");
- System.out.println("Maximum speed: " + m.getMaximumSpeed() + " mph");
- }
- }