In: Computer Science
Note: Anwer this as mention in the Question and do it urgently ( just simple uml of dry run kindly please fo fast)
Q1: Test the follow code step by step from the main. Add enough detail to justify the use of OOP concepts in these examples. Also draw the UML diagram using all given code. Finally show the output of this code [Note: Only output will not acceptable]
public class Person
{
private String name;
public Person()
{
name = "none";
}
public Person(String theName)
{
name = theName;
}
public Person(Person theObject)
{
name = theObject.name;
}
public String getName()
{
return name;
}
public void setName(String theName)
{
name = theName;
}
public String toString()
{
return name;
}
public boolean equals(Object other)
{
return name.equals(((Person)other).name);
}
}
public class Vehicle
{
private String mf;
private int cd;
private Person own;
public Vehicle()
{
mf = "none";
cd = 1;
own = null;
}
public Vehicle(String themf, int numcd, Person theown)
{
mf = themf;
cd = numcd;
own = new Person(theown);
}
public Vehicle(Vehicle other)
{
mf = other.mf;
cd = other.cd;
own = new Person(other.own);
}
public void setmf(String newmf)
{
mf = newmf;
}
public void setcd(int newNum)
{
cd = newNum;
}
public void setown(Person newown)
{
own = new Person(newown);
}
public String getmf()
{
return mf;
}
public int getcd()
{
return cd;
}
public Person getown()
{
return own;
}
public String toString()
{
return mf + ", " + cd + " cd, owned by " + own;
}
public boolean equals(Vehicle other)
{
return mf.equals(other.mf) && cd == other.cd;
}
}
public class Truck extends Vehicle
{
private double load;
public int capacity;
public Truck()
{
super();
load = 0;
capacity = 0;
}
public Truck(String m, int c,
Person p, double loads,
int capc)
{
super(m, c, p);
load = loads;
capacity = capc;
}
public Truck(Truck oth)
{
super(oth);
load = oth.load;
capacity = oth.capacity;
}
public void setload(double newLoad)
{
load = newLoad;
}
public void setcapacity(int newCaps)
{
capacity = newCaps;
}
public double getload()
{
return load;
}
public int getcapacity()
{
return capacity;
}
public String toString()
{
return super.toString() + ", " + load + " lbs load, " + capacity +
" tow";
}
public boolean equals(Truck oth)
{
return super.equals(oth) &&
load == oth.load &&
capacity == oth.capacity;
}
}
public class CompleteTest
{
public static void main(String args[])
{
Person owner1 = new Person("Nathan Roy");
Person owner2 = new Person("Peter England");
Vehicle aCar = new Vehicle("Honda", 5, owner2);
Truck aTruck = new Truck();
aTruck.setmf("Skoda");
aTruck.setcd(100);
aTruck.setown(owner1);
aTruck.setload(150.50);
aTruck.setcapacity(3200);
System.out.println("Truck Details: ");
System.out.println(aTruck.getmf());
System.out.println(aTruck.getcd());
System.out.println(aTruck.getown());
System.out.println(aTruck.getload());
System.out.println(aTruck.getcapacity());
System.out.println();
System.out.println("Details of Vehicle 1: ");
System.out.println(aCar);
System.out.println("Details of Vehicle 2: ");
System.out.println(aTruck);
}
}
Code
with commnetns
Person class
public class Person {
//instace varibale type String that stores the name of Person
private String name;
//default constructor that set name to none
public Person()
{
name = "none";
}
//parameterize constructor that set the name as give in
parameter
public Person(String theName)
{
name = theName;
}
//copy constructor that assing the name of theObject to the this
name
public Person(Person theObject)
{
name = theObject.name;
}
//getter method for name
public String getName()
{
return name;
}
//setter method for name taht set the name
public void setName(String theName)
{
name = theName;
}
//toString method that return the name of person
public String toString()
{
return name;
}
//equals method that check this and other object are same or
name
public boolean equals(Object other)
{
return name.equals(((Person)other).name);
}
}
Vehicle class
public class Vehicle {
//properteis of Vehicle
private String mf;//
private int cd;
private Person own;
//default constructor for Vehicle that sets default value
public Vehicle()
{
mf = "none";
cd = 1;
own = null;
}
//patametrize constructor
public Vehicle(String themf, int numcd, Person theown)
{
mf = themf;
cd = numcd;
own = new Person(theown);
}
//copy constructor
public Vehicle(Vehicle other)
{
mf = other.mf;
cd = other.cd;
own = new Person(other.own);
}
//setter method
public void setmf(String newmf)
{
mf = newmf;
}
public void setcd(int newNum)
{
cd = newNum;
}
public void setown(Person newown)
{
own = new Person(newown);
}
//getter methods
public String getmf()
{
return mf;
}
public int getcd()
{
return cd;
}
public Person getown()
{
return own;
}
//toString method that return the string with properties of
vehicle
public String toString()
{
return mf + ", " + cd + " cd, owned by " + own;
}
//equals method that check fo equality of vehicle
public boolean equals(Vehicle other)
{
return mf.equals(other.mf) && cd == other.cd;
}
}
Truck class
public class Truck extends Vehicle{
//instance varibales of Vehicle
private double load;
public int capacity;
//default constructor
public Truck()
{
super();
load = 0;
capacity = 0;
}
//parameterize constructor
public Truck(String m, int c,Person p, double loads,int capc)
{
super(m, c, p);
load = loads;
capacity = capc;
}
//copy constructor
public Truck(Truck oth)
{
super(oth);
load = oth.load;
capacity = oth.capacity;
}
//set methods for propertise of vehicle
public void setload(double newLoad)
{
load = newLoad;
}
public void setcapacity(int newCaps)
{
capacity = newCaps;
}
//get methods for propertise of vehicle
public double getload()
{
return load;
}
public int getcapacity()
{
return capacity;
}
//tostring method
public String toString()
{
return super.toString() + ", " + load + " lbs load, " + capacity +
" tow";
}
//equal for checking equality
public boolean equals(Truck oth)
{
return super.equals(oth) &&
load == oth.load &&
capacity == oth.capacity;
}
}
CompelteTest
public class CompleteTest {
public static void main(String[] args) {
//creates the two object of the class person with parameterize
constructor
Person owner1 = new Person("Nathan Roy");
Person owner2 = new Person("Peter England");
//creater a vehicle object with parameterize constructor
Vehicle aCar = new Vehicle("Honda", 5, owner2);
//crates a object of Truck object wirh default constructor
Truck aTruck = new Truck();
//sets all the properties of Truck using setters methods
aTruck.setmf("Skoda");
aTruck.setcd(100);
aTruck.setown(owner1);
aTruck.setload(150.50);
aTruck.setcapacity(3200);
System.out.println("Truck Details: ");
System.out.println(aTruck.getmf());
System.out.println(aTruck.getcd());
System.out.println(aTruck.getown());
System.out.println(aTruck.getload());
System.out.println(aTruck.getcapacity());
System.out.println();
System.out.println("Details of Vehicle 1: ");
System.out.println(aCar);
System.out.println("Details of Vehicle 2: ");
System.out.println(aTruck);
}
}
Output
Truck Details:
Skoda
100
Nathan Roy
150.5
3200
Details of Vehicle 1:
Honda, 5 cd, owned by Peter England
Details of Vehicle 2:
Skoda, 100 cd, owned by Nathan Roy, 150.5 lbs load, 3200 tow
Uml Diagram
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.