Question: Java Foundations Create the following classes. Each class should have getters and setters, toString, and a constructor. Be sure to set them up in the
Java Foundations
Create the following classes. Each class should have getters and setters, toString, and a constructor. Be sure to set them up in the proper OOP format.
Person: This class should have fields for name and at least 2 other fields.
MotorVehicle: This should be an abstract class. Fields: odometer, make, model, year. Methods: purchase (to get a new owner), use (to travel a certain number of miles but only if you have a non-null owner).
Car: child class of MotorVehicle. Field: isFourWheelDrive, owner (of type Person). Have the purchase method change the owner field, not the usual setter method.
Plane: child class of MotorVehicle. Field: wingspan, owner (of type Person). Have the purchase method change the owner field, not the usual setter method.
MotorVehicleException: an exception to be used in MotorVehicle or its children. At least one method in one of your other classes must generate this exception, e.g. trying to use a vehicle without an owner.
Main: should only have main(). You should instantiate multiple Persons and multiple Cars and Planes. Use the methods to make the Persons and Cars and Planes interact.
Car.java
public class Car extends MotorVehicle { //data fields private boolean isFourWheelDrive; private Person owner; //getters and setters public boolean isFourWheelDrive() { return isFourWheelDrive; }
public void setFourWheelDrive(boolean isFourWheelDrive) { this.isFourWheelDrive = isFourWheelDrive; }
public Person getOwner() { return owner; }
// constructor public Car(int year, String make, String model, boolean fwd) { this.odometer = 0; this.owner = null; this.year = year; this.make = make; this.model = model; this.isFourWheelDrive = fwd; } @Override public void purchase(Person newOwner) { this.owner = newOwner; // TODO Auto-generated method stub
}
@Override public void use(double miles) throws MotorVehicleException { // TODO Auto-generated method stub
} @Override public String toString() { if(owner == null) return "" + year + " " + make " " + model + " has no owner. " + "fwd" + isFourWheelDrive; } else }
}
Main.java
public class Main {
public static void main(String[] args) { // TODO Auto-generated method stub Person p1 = new Person("Dean", 30, "the open road!", "male"); System.out.println(p1); }
}
MotorVehicle.java
public abstract class MotorVehicle {
// data fields protected double odometer; // miles traveled protected String make; // brand name String protected String model; // type of vehicle protected int year; // when it was manufactured // getters and setters public double getOdometer() { return odometer; } public void setOdometer(double odometer) { this.odometer = odometer; } public String getMake() { return make; } public void setMake(String make) { this.make = make; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public abstract void purchase(Person newOwner); public abstract void use(double miles) throws MotorVehicleException;{ }
}
MotorVehicleException.java
public class MotorVehicleException extends Exception { public MotorVehicleException(String message) { super(message); }
}
Person.java
public class Person { // data fields a.k.a. instance variable private String name; private int age; // measured in years private String address; private String gender; // getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } // constructor public Person(String name, int age, String address, String gender) { this.name = name; this.age = age; this.address = address; this.gender = gender; } @Override public String toString() { return name+ " (age " + age + ") " + "is gender " + gender + " with address: " + address; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
