In: Computer Science
It's Java; the code should be the same as the output sample below; please, thank you.
Note: create a single-file solution that has multiple class definitions. Also, note that the static main() method should be a member of the Vehicle class.
Create a class called Vehicle that has the manufacturers name
(type String), number of cylinders in the engine (type int), and
owner (type Person given next). Then, create a class called Truck
that is derived from Vehicle and has the following additional
properties: the load capacity in tons (type double since it may
contain a fractional part) and towing capacity in pounds (type
int). Be sure your class has a reasonable complement of
constructors, accessor and mutator methods, and suitably defined
equals and toString methods. Write a program to test all your
methods (see sample run).
The definition of the class Person follows. Completing the
definitions of the methods is part of this programming
project.
class Person{
private String name;
public Person()
{...}
public Person(String theName)
{...}
public Person(Person theObject)
{...}
public String getName()
{...}
public void setName(String theName)
{...}
public String toString()
{...}
public boolean equals(Object other)
{...}
}
Output sample: interactive session
Enter·manufacturar·name:Chevrolet↵ Enter·number·of·cylinders:8↵ Enter·owner·name:David↵ Enter·load:2800.0↵ Enter·towing·capacity:1000↵ Enter·1·to·change·vehicle·properties,·2·to·print·and·10·to·quit:2↵ main↵ Manufacturer:·Chevrolet·Cylinders:·8·Name:·David·Load·Capacity:·2800.0·lbs·Tow·Capacity:·1000·pounds↵ Enter·1·to·change·vehicle·properties,·2·to·print·and·10·to·quit:1↵ Enter·1·to·change·manufacturar,·2·to·change·cylinders,·3·to·change·owner,·4·to·change·load,·5·to·change·towing·capacity,·or·any·other·number·to·go·back·to·main·menu:4↵ Enter·load:50000↵ Enter·1·to·change·vehicle·properties,·2·to·print·and·10·to·quit:1↵ Enter·1·to·change·manufacturar,·2·to·change·cylinders,·3·to·change·owner,·4·to·change·load,·5·to·change·towing·capacity,·or·any·other·number·to·go·back·to·main·menu:5↵ Enter·towing·capacity:1000↵ Enter·1·to·change·vehicle·properties,·2·to·print·and·10·to·quit:2↵ main↵ Manufacturer:·Chevrolet·Cylinders:·8·Name:·David·Load·Capacity:·50000.0·lbs·Tow·Capacity:·1000·pounds↵ Enter·1·to·change·vehicle·properties,·2·to·print·and·10·to·quit:10↵ ↵
Note: Here whole code and class is done as per your request, but if you have any doubt or issue regarding this you can ask me or post the issue, i will change code according to your need.
Alos include try{...} catch() {...} exception for better run the code.
person.java
public class Person {
private String name;
private Person obj;
public Person() {
}
public Person(String theName) {
this.name = theName;
}
public Person(Person theObject) {
this.obj = theObject;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public String toString() {
return "Owner = " + name + "\n";
}
}
Truck.java
public class Truck extends Vehicle {
private double loadCapacity;
private int towingCapacity;
public Truck() {
}
public Truck(String manufactureName, int numOfCylinder, Person owner, double loadCapacity, int towingCapacity) {
super(manufactureName, numOfCylinder, owner);
this.loadCapacity = loadCapacity;
this.towingCapacity = towingCapacity;
}
@Override
public String toString() {
return super.toString() + "loadCapacity = " + loadCapacity + "\ntowingCapacity = " + towingCapacity;
}
public double getLoadCapacity() {
return loadCapacity;
}
public void setLoadCapacity(double loadCapacity) {
this.loadCapacity = loadCapacity;
}
public int getTowingCapacity() {
return towingCapacity;
}
public void setTowingCapacity(int towingCapacity) {
this.towingCapacity = towingCapacity;
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}
Vehicle.java
import java.util.Scanner;
public class Vehicle {
@Override
public String toString() {
return "manufactureName = " + manufactureName + "\nnumOfCylinder = " + numOfCylinder + "\n" + owner.toString();
}
private String manufactureName;
private int numOfCylinder;
private Person owner;
public Vehicle() {
}
public Vehicle(String manufactureName, int numOfCylinder, Person owner) {
this.manufactureName = manufactureName;
this.numOfCylinder = numOfCylinder;
this.owner = owner;
}
public String getManufactureName() {
return manufactureName;
}
public void setManufactureName(String manufactureName) {
this.manufactureName = manufactureName;
}
public int getNumOfCylinder() {
return numOfCylinder;
}
public void setNumOfCylinder(int numOfCylinder) {
this.numOfCylinder = numOfCylinder;
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = owner;
}
public static void main(String[] args) {
Truck tr;
Scanner scan = new Scanner(System.in);
try {
System.out.print("Enter manufacturar Name : ");
String manufactureName = scan.next();
System.out.print("Enter number of cylinders : ");
int numOfCylinder = scan.nextInt();
System.out.print("Enter owner Name : ");
String owner = scan.next();
System.out.print("Enter load : ");
double loadCapacity = scan.nextDouble();
System.out.print("Enter towing capacity : ");
int towingCapacity = scan.nextInt();
tr = new Truck(manufactureName, numOfCylinder, new Person(owner), loadCapacity, towingCapacity);
int ch;
do {
System.out.println("\nEnter 1 to change vehicle properties, 2 to print and 10 to quit: ");
ch = scan.nextInt();
if (ch == 1) {
System.out.println(
"\nEnter 1 to change manufacturar, 2 to change cylinders, 3 to change owner, 4 to change load,"
+ " 5 to change towing capacity, or any other number to go back to main menu: ");
int editCh = scan.nextInt();
switch (editCh) {
case 1:
System.out.print("Enter manufacturar name: ");
manufactureName = scan.next();
tr.setManufactureName(manufactureName);
break;
case 2:
System.out.print("Enter number of cylinders: ");
numOfCylinder = scan.nextInt();
tr.setNumOfCylinder(numOfCylinder);
break;
case 3:
System.out.print("Enter owner name: ");
owner = scan.next();
tr.setOwner(new Person(owner));
break;
case 4:
System.out.print("Enter load: ");
loadCapacity = scan.nextDouble();
tr.setLoadCapacity(loadCapacity);
break;
case 5:
System.out.print("Enter towing capacity: ");
towingCapacity = scan.nextInt();
tr.setTowingCapacity(towingCapacity);
break;
}
} else if (ch == 2)
System.out.println(tr.toString());
} while (ch != 10);
} catch (Exception e) {
System.out.println("Exception occures, again run code");
}
scan.close();
}
}