Question

I need the following code to keep the current output but also include somthing similar to...

I need the following code to keep the current output but also include somthing similar to this but with the correct details from the code.

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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");
      
    }
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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[1]=new Vehicle("Ford",9,person[1]);

       vehicle[2]=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");

     

// extra

        System.out.println("\n\nTruck details::"); //truck details

        for(int i=0;i<3;i++)

        System.out.println(vehicle[i].mname+","+vehicle[i].cylinders+"cd, owned by "+ person[i].getName() + "," +truck[i].load + "lbs load, " +truck[i].towCapacity+"tow");

    }

}

// person class

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;

    }

}

//truck class

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;

    }

}

//vehicle class

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;

    }

}

sers RaviiDesktop notespl b javac :\Users\Rav 11\Desktop\n o tes\pl_d>Javac person java truck-Java Users Ravii Desktop notes pl b>javac vehicle.jaua Users Ravii Desktop notes pl b javac NewClass java Users Ravii DesktopNnotes pl b>java NewClass Equal ot equal Equal Truck details:: Ford.8cd. owned by John.10.6lbs load. 12tow Ford.9cd. owned by John.10.6lbs load. 12tow Ferrari.10cd. owned by Michael Scofield.20.5lbs load 19tow

Add a comment
Know the answer?
Add Answer to:
I need the following code to keep the current output but also include somthing similar to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's...

    Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's name (type String), number of cylinders in the engine (type int), and owner (type Person given in Listing 8.1 in the textbook and in LMS). Then create a class called Truck that is derived from Vehicle and has additional properties: the load capacity in tons (type double, since it may contain a fractional part) and towing capacity in tons (type double). Give your classes...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE....

    CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE. SAME CODE SHOULD BE USED FOR THE DOCUMENTATION PURPOSES. ONLY INSERT THE JAVA DOC. //Vehicle.java public class Vehicle {    private String manufacturer;    private int seat;    private String drivetrain;    private float enginesize;    private float weight; //create getters for the attributes    public String getmanufacturer() {        return manufacturer;    }    public String getdrivetrain() {        return drivetrain;...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • Can anyone identify which OO Design Patterns are being used in the following code?: package mis;...

    Can anyone identify which OO Design Patterns are being used in the following code?: package mis; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; class helpDeskGuidline{    private void setGuideLine(){           }    public void rateService(){           } } public class HelpDeskService extends helpDeskGuidline{ //HelpDeskService class extends helpDeskGuidline this    //called inheritance    private static int ticketId=1;    Ticket ticket; // HelpDeskService class using ticket class object. this is has-A relationship (aggregation)          ArrayList<Ticket> allTicket=new ArrayList<>();    HashMap<Ticket,Person> ticketAllocation=new HashMap<>();    HashMap<Person,Integer> assignee=new HashMap<>();    HelpDeskService(){        Person...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • public class Item implements Comparable { private String name; private double price; /** * Constructor for...

    public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; }    /** * gets the name * @return name name of item */ public String getName() { return name; }    /** * gets price of item * @return price price of item */...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT