Question
Required in Java. Thank you.

1. Create a base class called Vehicle that has the manufacturers name (type String), number of cylinders in the engine (type
public class Person private String name; public Person() name = No name yet.; public Person(String initialName) name = init
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Person.java


public class Person {
   private String name;
  
   public Person()
   {
       name = "No name yet.";
   }
  
   public Person(String initialName)
   {
       name = initialName;
   }
  
   public void setName(String newName)
   {
       name = newName;
   }
  
   public String getName()
   {
       return name;
   }
  
   public void writeOutput()
   {
       System.out.println("Name: " + name);
   }
  
   public boolean hasSameName(Person otherPerson)
   {
       return (this.name.equalsIgnoreCase(otherPerson.getName()));
   }
}

Vehicle.java


public class Vehicle {
   private String manufacturerName;
   private int numberOfCylinders;
   private Person owner;

   //default constructor
   public Vehicle()
   {
       manufacturerName = "No name yet.";
       numberOfCylinders = 0;
       owner = new Person();
   }

   //parameterized constructor
   public Vehicle(String mName, int nCylinders, String ownerName)
   {
       manufacturerName = mName;
       numberOfCylinders = nCylinders;
       owner = new Person(ownerName);
   }

   //accessors
   //method that sets manufacturer's name
   public void setManufacturerName(String mName)
   {
       manufacturerName = mName;
   }

   //method that sets number of cylinders
   public void setNumberOfCylinders(int nCylinders)
   {
       numberOfCylinders = nCylinders;
   }

   //method that sets owners name
   public void setOwner(String name)
   {
       owner.setName(name);
   }

   //method that gets manufacturer's name
   public String getManufacturerName()
   {
       return manufacturerName;
   }

   //method that gets number of cylinders
   public int getNumberOfCylinders()
   {
       return numberOfCylinders;
   }

   //method that gets owners name
   public String getOwner()
   {
       return owner.getName();
   }
  
   //equals method
   public boolean equals(Vehicle otherVehicle)
   {
       if((this.manufacturerName.equalsIgnoreCase(otherVehicle.getManufacturerName()))
               && (this.numberOfCylinders == otherVehicle.getNumberOfCylinders()) && (this.getOwner().equalsIgnoreCase(otherVehicle.getOwner())))
           return true;
       else
           return false;

   }
  
   //method that prints the Vehicle information
   public void printVehicle()
   {
       System.out.println("Manufacturer's Name: " + getManufacturerName());
       System.out.println("Number of cylinders in engine: " + getNumberOfCylinders());
       System.out.println("Owner's Name: " + getOwner());
   }
}

Truck.java


public class Truck extends Vehicle {
   private double loadCapacity;
   private double towingCapacity;

   //default constructor
   public Truck()
   {
       super();
       loadCapacity = 0.0;
       towingCapacity = 0.0;
   }

   //parameterized constructor
   public Truck(String mName, int nCylinders, String ownerName, double lCapacity, double tCapacity)
   {
       super(mName, nCylinders, ownerName);
       loadCapacity = lCapacity;
       towingCapacity = tCapacity;
   }

   //method that sets load capacity
   public void setLoadCapacity(double lCapacity)
   {
       loadCapacity = lCapacity;
   }

   //method that sets towing capacity
   public void setTowingCapacity(double tCapacity)
   {
       towingCapacity = tCapacity;
   }

   //method that gets load capacity
   public double getLoadCapacity()
   {
       return loadCapacity;
   }

   //method that gets towing capacity
   public double getTowingCapacity()
   {
       return towingCapacity;
   }

   //method that display truck information
   public void printTruck()
   {
       printVehicle();
       System.out.println("Load Capacity: " + loadCapacity);
       System.out.println("Towing Capacity: " + towingCapacity);
   }

   //equals method
   public boolean equals(Truck otherTruck)
   {
       if(getManufacturerName().equalsIgnoreCase(otherTruck.getManufacturerName())
           && (getNumberOfCylinders() == otherTruck.getNumberOfCylinders()) && (getOwner().equalsIgnoreCase(otherTruck.getOwner()))
           && this.getLoadCapacity() == otherTruck.getLoadCapacity() && this.getTowingCapacity() == otherTruck.getTowingCapacity())
           return true;
       else
           return false;

   }
}


TestVehicle.java


public class TestVehicle {

   public static void main(String[] args) {
       //create two objects for vehicle class
       Vehicle v1 = new Vehicle("Ford", 2, "Shreyas");
       Vehicle v2 = new Vehicle("Ford", 2, "Chandan");
       //display the vehicle information
       System.out.println("Vehicle-1:");
       v1.printVehicle();  
       System.out.println("Vehicle-2:");
       v2.printVehicle();
       //check if both the vehicles are equal
       if(v1.equals(v2))
           System.out.println("Both vehicles are equal");
       else
           System.out.println("Both vehicles are not equal");
      
       //create two objects for Truck class
       Truck t1 = new Truck ("Mahindra", 4, "Shreyas", 225, 125);
       Truck t2 = new Truck ("Mahindra", 4, "Shreyas", 225, 125);
       //display the Truck information
       System.out.println("\n\nTruck-1:");
       t1.printVehicle();  
       System.out.println("Truck-2:");
       t2.printVehicle();  
       //check if both the Trucks are equal
       if(t1.equals(t2))
           System.out.println("Both trucks are equal");
       else
           System.out.println("Both trucks are not equal");
   }

}

Output:

= E Console X x | = EH EH = = = = = <terminated TestVehicle [Java Application] C:\Program Files Vava jre1.8.0_151\bin\javaw.e

Code Screenshots:

Person.java X Vehicle.java Truck.java TestVehicle.java 2 public class Person { private String name; public Person() mt nog naPerson.java Vehicle.java X Truck.java TestVehicle.java public class Vehicle { private String manufacturerName; private int nuTestVehicle.java Person.java Vehicle.java X Truck.java 36 //method that sets owners name 370 public void setOwner (String nam70 720 //method that prints the Vehicle information public void printVehicle) System.out.println(Manufacturers Name: + getPerson.java Vehicle.java Truck.java X TestVehicle.java public class Truck extends Vehicle { private double loadCapacity: priv40 410 //method that gets towing capacity public double getTowingCapacity() 42 43 44 return towingCapacity; 470 //method thatPerson.java Vehicle.java Truck.java TestVehicle.java X 2 public class TestVehicle { public static void main(String[] args) {

Add a comment
Know the answer?
Add Answer to:
Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's...
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
  • In Java* Please implement a class called "MyPet". It is designed as shown in the following...

    In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...

  • 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...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • How to solve this problem? Consider the following class : public class Store Public final double...

    How to solve this problem? Consider the following class : public class Store Public final double SALES TAX_RATE = 0.063B private String name; public Store(String newName) setName ( newName); public String getName () { return name; public void setName (String newName) { name = newName; public String toString() return "name: “ + name; Write a class encapsulating a web store, which inherits from Store. A web store has the following additional attributes: an Internet Address and the programming language in...

  • 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";     }       ...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and...

    The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and calories (int), along with get/set methods for both. Implement the methods in the Meal class found in Meal.java public class FoodItem{ private String name; private int calories;    public FoodItem(String iName, int iCals){ name = iName; calories = iCals; }    public void setName(String newName){ name = newName; }    public void setCalories(int newCals){ calories = newCals; }    public int getCalories(){ return calories;...

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