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 / Capacity
car1 / Ford / Mustang / 120 mph / 15 gallons
car2 / Toyota / Corolla / 150 mph / 12 gallons

  1. public class MotorCar
  2. {
  3. }

// Adding the variables

  1. public class MotorCar
  2. {
  3. private String make, model;
  4. private double engine, max_speed, tank;
  5. }

// Adding the constructor

  1. public class MotorCar
  2. {
  3. private String make, model;
  4. private double engine, max_speed, tank;
  5. public MotorCar(String mk, String mod, double spd, double tnk, double eng)
  6. {
  1. make = mk;
  2. model = mod;
  3. engine = eng;
  4. max_speed = spd;
  5. tank = tnk;
  6. }
  7. }

// Class MotorCar (Adding accessor method getMake())

  1. public class MotorCar
  2. {
  3. private String make, model;
  4. private double engine, max_speed, tank, hold;
  5. public MotorCar(String mk, String mod, double spd, double tnk, double eng)
  6. {
  7. make = mk;
  8. model = mod;
  9. engine = eng;
  10. max_speed = spd;
  11. tank = tnk;
  12. }
  13. public String getMake()
  14. {
  15. return make;
  16. }
  17. }

// Adding mutator method drive()

  1. public class MotorCar
  2. {
  3. private String make, model;
  4. private double engine, max_speed, tank;
  5. public MotorCar(String mk, String mod, double spd, double tnk, double eng)
  6. {
  7. make = mk;
  8. model = mod;
  9. engine = eng;
  10. max_speed = spd;
  11. tank = tnk;
  12. }
  13. public String getMake()
  14. {
  15. return make;
  16. }
  17. public void drive(double amount)
  18. {
  19. tank = tank - amount;
  20. }
  21. }

//Test Class TestMotorCar (version 1)

  1. class TestMotorcar
  2. {
  3. public static void main(String[] arg)
  4. {
  5. MotorCar car1 = new MotorCar("Ford", "Mustang", 120, 15, 3.5);
  6. MotorCar car2 = new MotorCar("Toyota", "Corolla", 150, 12, 2.2);
  7. System.out.println("Features of Cars");
  8. System.out.println("------");
  9. System.out.println("Name: " + car1.getMake() + " " + car1.getModel());
  10. System.out.println("Petrol tank capacity: " + car1.getTankCapacity() + " gallons");
  11. System.out.println("Maximum speed: " + car1.getMaximumSpeed() + " mph");
  12. System.out.println("------");
  13. System.out.println("Name: " + car2.getMake() + " " + car2.getModel());
  14. System.out.println("Petrol tank capacity: " + car2.getTankCapacity() + " gallons");
  15. System.out.println("Maximum speed: " + car2.getMaximumSpeed() + " mph");
  16. System.out.println("------");
  17. }
  18. }

//Test Class TestMotorCar (version 2 – make common code method)

  1. class TestMotorcar
  2. {
  3. public static void main(String[] arg)
  4. {
  5. MotorCar car1 = new MotorCar("Ford", "Mustang", 120, 15, 3.5);
  6. MotorCar car2 = new MotorCar("Toyota", "Corolla", 150, 12, 2.2);
  7. System.out.println("Features of Cars");
  8. report(car1);
  9. report(car2);
  10. System.out.println("------");
  11. }
  12. public static void report( MotorCar m)
  13. {
  14. System.out.println("------");
  15. System.out.println("Name: " + m.getMake() + " " + m.getModel());
  16. System.out.println("Petrol tank capacity: " + m.getTankCapacity() + " gallons");
  17. System.out.println("Maximum speed: " + m.getMaximumSpeed() + " mph");
  18. }
  19. }