Question

In: Computer Science

1. Create a base class called Vehicle that has the manufacturer’s name (type String), number of...

1. Create a base class called Vehicle that has the manufacturer’s name (type String), number of cylinders in the engine (type int). Then create a class called Truck that is derived from Vehicle and has additional properties: the load capacity in tons (type double) and towing capacity in tons (type double).

Add the following methods to the Vehicle class:

  1. A default constructor that sets the vehicle class properties to default values (manufacturer = "None"; cylinders = 0).
  2. A constructor that sets a vehicle properties (manufacturer and cylinders) to a specified values.
  3. The accessor and mutator methods for manufacturer and cylinders.
  4. A method called writeOutput that prints out a vehicle object information (manufacturer and cylinders).
  5. A method named equals to test whether two instances of vehicle objects are equal. The method returns Boolean data type.

Add the following methods to the Truck class:

  1. A default constructor that sets the truck class properties to default values (load capacity = 0; towing capacity = 0).
  2. A constructor that sets a truck properties (manufacturer, cylinders, load capacity, and towing capacity) to a specified values.
  3. The accessor and mutator methods for load capacity, and towing capacity.
  4. A method called writeOutput that overrides the writeOutput from Vehicle. This new version needs to prints out a truck object information (manufacturer, cylinders, load capacity, and towing capacity).
  5. A method named equals to test whether two instances of truck objects are equal. The method returns Boolean data type.

Write a driver program that tests all your methods. Create at least two objects of truck class, and two objects of vehicle class.

(JAVA)

Solutions

Expert Solution

// Screenshot of the code

// Sample output

// Code to copy

Vehicle.java

public class Vehicle {
   private String maker;
   private int cylinders;
  

   public Vehicle()
   {
   maker="none";
   cylinders=1;
     
   }
   public Vehicle(String m,int n)//m is maker,n is the number of cylinders
   {
   maker=m;
   cylinders=n;
     
   }
  
   public void setMaker(String m)
   {
   maker=m;
   }
   public void setCylinders(int newNum)
   {
   cylinders=newNum;
   }
  
   public String getMaker()
   {
   return maker;
   }
   public int getCylinders()
   {
   return cylinders;
   }
  
   public String writeOutput()
   {
   return "\nManufacturer: "+maker+"\nEngine: "+cylinders;
   }
   public boolean equals(Vehicle other)
   {
   return maker.equals(other.maker)&&cylinders==other.cylinders;
   }
}

Truck.java

public class Truck extends Vehicle{
   private double loadCap;
   public double towCap;

   public Truck()
   {
   super();
   loadCap=0.00;
   towCap=0.00;
   }
   public Truck(String maker,int cylinders,double loadCapa,double towCapa)
   {
   super(maker,cylinders);
   loadCap=loadCapa;
   towCap=towCapa;
   }
     
   public void setLoadCap(double newLoadCap)
   {
   loadCap=newLoadCap;
   }
   public void setTowCap(double towCap2)
   {
   towCap=towCap2;
   }
   public double getLoadCap()
   {
   return loadCap;
   }
   public double getTowCap()
   {
   return towCap;
   }
   public String writeOutput()
   {
   return super.toString()+","+loadCap+" pound load, "+towCap+" tow";
   }
   public boolean equals(Truck other)
   {
   return super.equals(other)&&loadCap==other.loadCap&&towCap==other.towCap;
   }
}


VehicleDemo.java

import java.util.Scanner;
public class VehicleDemo {

   public static void main(String[] args) {
       Scanner input =new Scanner(System.in);
     

   Vehicle aCar1=new Vehicle("Chevrolet",5);

   Vehicle aCar2=new Vehicle();
   String maker1;  
   int cylinders1;

   System.out.println("Input your cars info:");
   System.out.print("What is the manufacture name? ");
   maker1=input.nextLine();
  
   System.out.print("What is the number of cylinders? ");
   cylinders1=input.nextInt();     

   aCar2.setMaker(maker1);
   aCar2.setCylinders(cylinders1);
     

   Truck aTruck1=new Truck();
   aTruck1.setMaker("Toyota");
   aTruck1.setCylinders(6);
     
   aTruck1.setLoadCap(250.55);
   aTruck1.setTowCap(3000);

   Truck aTruck2=new Truck();
   String maker2;     
   int cylinders2;
   double towCap;
   double loadCap;

   System.out.println("\nInput your trucks info:");
   System.out.print("What is the manufacture name? ");
   maker2=input.next();  
   System.out.print("What is the number of cylinders? ");
   cylinders2=input.nextInt();
   System.out.print("What is the load capacity? ");
   loadCap=input.nextDouble();
   System.out.print("What is the tow capacity? ");
   towCap=input.nextDouble();
     

   aTruck2.setMaker(maker2);
   aTruck2.setCylinders(cylinders2);  
   aTruck2.setLoadCap(loadCap);
   aTruck2.setTowCap(towCap);

   System.out.println("\nFirst Cars info:");
   System.out.println(aCar1.getMaker());
   System.out.println(aCar1.getCylinders());
  

   System.out.println("\nSecond Cars info:");
   System.out.println(aCar2.getMaker());
   System.out.println(aCar2.getCylinders());
  

   System.out.println("\nFirst Trucks info:");
   System.out.println(aTruck1.getMaker());
   System.out.println(aTruck1.getCylinders());     
   System.out.println(aTruck1.getLoadCap());
   System.out.println(aTruck1.getTowCap());

   System.out.println("\nSecond Trucks info:");
   System.out.println(aTruck2.getMaker());
   System.out.println(aTruck2.getCylinders());  
   System.out.println(aTruck2.getLoadCap());
   System.out.println(aTruck2.getTowCap());

     
   if(aTruck1.equals(aTruck2)==true)
   {
   System.out.println("The trucks are both the same.");
   }
   else
   {
   System.out.println("The trucks are not the same.");
   }
   if(aCar1.equals(aCar2)==true)
   {
   System.out.println("The cars are both the same.");
   }
   else
   {
   System.out.println("The cars are not the same.");
   }
   input.close();
   }

}


Related Solutions

Create a class called Vehicle that includes four instance variables:      name, type,     tank size and...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and average petrol consumption. Provide 2 constructors, the first takes name and type as parameter, the second takes four parameters for the four instance variables. (2 pt) Provide also a method called distancePerTank that calculate the average distance that a vehicle can travel if the tank is full (multiplies the tank size by the average petrol consumption), then returns the value. (2 pt) Provide a...
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
1) Create a class called Employee that includes three instance variables — a first name (type...
1) Create a class called Employee that includes three instance variables — a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. 2) Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating...
1) Create a class called Employee that includes three instance variables — a first name (type...
1) Create a class called Employee that includes three instance variables — a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. 2)Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating five...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a method called Washing. The Washing method should return a string to the main class with the text "Now washing dishes!" In the main class create an array of DishWasher size 3. Give each DishWasher a different color and CubicFeet. Use a foreach loop to display that info, call the Washing method, and display the text returned from the Washing method call. c#
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the user to choose game tester type...
Using java, create a class called MyString that has one String called word as its attribute...
Using java, create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n, then perform 2n swaps. Use this in an application called Jumble that prompts the user for a...
Write a C++ program that creates a base class called Vehicle that includes two pieces of...
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely: wheels (type int) weight (type float) Program requirements (Vehicle class): Provide set and a get member functions for each data member. Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members. Provide a pure virtual member function by the name displayData()...
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT