Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need this to show the output of all the objects (java) class Person { private String name; public Person() { name=None; } public Person(String

I need this to show the output of all the objects (java)

class Person { private String name; public Person() { name="None"; } public Person(String theName) { if(theName!="") //Data validation name=theName; } public Person(Person theObject) { this.name=theObject.name; } public String getName() //Accessor method { return name; } public void setName(String theName) //Mutator method { if(theName!="") //Data validation name=theName; } public String toString() { return "Name is:"+name; } public boolean equals(Person other) { if(this.getName()== other.getName()) return true; else return false; } }

class Vehicle { String mname; int cylinders; Person p; public Vehicle() { mname="None"; cylinders=2; } public Vehicle(String mname,int cylinders,Person obj) { if(mname!="") //Data validation this.mname=mname; if(cylinders>2&&cylinders<31) //Data validation this.cylinders= cylinders; this.p=new Person(obj.getName()); } public String getMname() //Accessor method { return mname; } public int getCylinders() //Accessor method { return cylinders; } public Person getPerson() //Accessor method { return p; } public void setPerson(Person obj) //Mutator method { this.p.setName(obj.getName()); } public void setMname(String mname) //Mutator method { if(mname!="") //Data validation this.mname=mname; } public void setCylinders(int cylinders) //Mutator method { if(cylinders>2&&cylinders<31) //Data validation this.cylinders=cylinders; } public String toString() { return "Name is:"+mname+" Number of cylinders: "+cylinders; } public boolean equals(Object other) { if(other instanceof Vehicle) { Vehicle v=(Vehicle) other; if(getMname()==v.getMname() ) if(getCylinders()== v.getCylinders()) return true; }

return false; } }

class Truck extends Vehicle { double load; int towCapacity; Truck() { load=towCapacity=1; } Truck(double load,int towCapacity) { if(load>0) //Data validation this.load=load; if(towCapacity>0) //Data validation this.towCapacity=towCapacity; } public double getLoad() //Accessor method { return load; } public int getTowCapacity() //Accessor method { return towCapacity; } public void setLoad(double load) //Mutator method { if(load>0) //Data validation this.load=load; } public void getTowCapacity(int towCapacity) //Mutator method { if(towCapacity>0) //Data validation this.towCapacity=towCapacity; } public String toString() { return "Load is:"+load+" Tow capacity is: "+towCapacity; } public boolean equals(Truck other) { if(other instanceof Truck) { Truck t=(Truck) other; if(getLoad()==t.getLoad() ) if(getTowCapacity()== t.getTowCapacity()) return true; }

return false; } }

public class NewClass { public static void main(String args[]) { Person person[]=new Person[3]; //Creating three persons person[0]=new Person("John"); person[1]=new Person("John"); person[2]=new Person("Michael Scofield"); Vehicle vehicle[]=new Vehicle[3];//Creating three trucks

vehicle[0]=new Vehicle("Ford",8,person[0]); vehicle[0]=new Vehicle("Ford",9,person[1]); vehicle[0]=new Vehicle("Ferrari",10,person[2]); Truck truck[]=new Truck[3];//Creating three trucks truck[0]=new Truck(10.6,12); truck[1]=new Truck(10.6,12); truck[2]=new Truck(20.5,19); if(person[0].equals(person[1])) //.equals test System.out.println("Equal"); if (vehicle[0].equals(vehicle[2])) //.equals test System.out.println("Equal"); else System.out.println("Not equal"); if(truck[0].equals(truck[1])) //.equals test System.out.println("Equal"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

Which job has negative profit?

Answered: 1 week ago