Question
Do Chapter 8 Practice Program 3, create Vehicle and Truck classes: Create a base class called Vehicle that has the manufacturers name (type String ),
Do Chapter 8 Practice Program 3, create Vehicle and Truck classes:
Create a base class called Vehicle that has the manufacturers name (type String), # of cylinders in the engine (type int), and owner (type Person from the Sakai Week 11 Source Code folder). The Vehicle class should have a toString method that returns this:
manufacturer name, cylinders cylinders, owner owner name
Use the getters for the name and cylinders instance variables to obtain their information. Get the owner name by using the Persongetname() method (Person comes from the Sakai Week 11 Source Code folder).
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 (also type double). The Truck class should have a toString method that returns whatever Vehicle's toString returns followed by this information:
, load capacity load capacity tons, towing capacity towing capacity tons
Use the getters for the load capacity and towing capacity instance variables to obtain this information, and return their double values with only 2 decimal places by using String.format. Here's how to use String.format to do that:
double d = 2.71828; String with2DecimalPlaces = String.format("%0.2f", d); // 0.2 f says only 2 places // with2DecimalPlaces now refers to the String "2.72"
Give your classes a reasonable complement of constructors (Vehicle: 0- and 3-parameter constructors; Truck: 0- and 5-parameter constructors) and accessor methods (getters and setters for all instance variables in each class). Trucks constructors should call Vehicles constructors using super().
You are provided a driver program (no pun intended) that tests all your methods. It will be easier to test the constructors if you write a toString method for both classes. You can test the setters by calling them in the constructors, and the getters by calling them in the toString methods; use Persons getName to get their name in the Vehicle toString method.
Hint: if you use all of the setters in the constructors and all of the getters in the toString methods then the driver program only has to create objects using the two constructors in each class and print those objects in order to test everything.
IN JAVA:
public class Vehicle // from Chapter 8 Practice Program 3
{
/* declare the name, cylinders, and owner instance variables here;
owner is type Person (supplied for you from the Source Code folder) */
/* write a no-parameter (default) constructor here that calls the
three-parameter Vehicle constructor using "this()", passing in "none"
as the name, 0 as the number of cylinders, and a new Person object
with name "unknown" (you'll have to create that object using new) */
/* write the 3-parameter Vehicle constructor here that takes the name,
# cylinders, and Person owner in that order; instead of setting the
three instance variables directly, call the 3 setters to do that */
/* write getters and setters for all three instance variables here */
@Override
public String toString()
{
/* write Vehicle's toString method here based on the requirements above */
}
/* THE TRUCK CLASS FOR YOU TO CREATE FOLLOWS THIS MAIN METHOD BELOW */
public static void main(String[] args)
{
/* DO NOT MODIFY THIS SET OF TESTS IN MAIN */
Vehicle v1 = new Vehicle();
System.out.println("Vehicle: " + v1);
System.out.println();
Vehicle v2 = new Vehicle("Ford", 6, new Person("Sam Slade"));
System.out.println("Vehicle: " + v2);
System.out.println();
Truck t1 = new Truck();
System.out.println("Truck: " + t1);
System.out.println();
Truck t2 = new Truck("Chevy", 8, new Person("Sylvia Slade"), 2.5, 7);
System.out.println("Truck: " + t2);
}
}
/* DO NOT MARK THE FOLLOWING CLASS PUBLIC! */
class Truck /* change this line so that Truck inherits from Vehicle */
{
/* declare the loadCapacity and towingCapacity instance variables here */
/* write a no-parameter Truck constructor here that calls the no-parameter
Vehicle constructor by using super(), then in it use the setters for the
loadCapacity and towingCapacity instance variables to set them to 0 */
/* write a 5-parameter Truck constructor here that takes the name, number of
cylinders, Person owner, load capacity, and towing capacity, in that order;
it should call the Vehicle 3-parameter constructor by using super() to pass
the first 3 parameters to that constructor, then use the setters for the
loadCapacity and towingCapacity instance variables to set them */
/* write getters and setters for loadCapacity and towing capacity here */
@Override
public String toString()
{
/* write a toString method that adds to Vehicle's toString the information
required in the exercise statement above; use super. to call Vehicle's toString */
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started