Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java 2 Questions Help: Lab 1.1 Fruit and Lab 1.2 Vehicle 1.Create a set of three classes that use inheritance: A Fruit class that has

Java 2 Questions Help: Lab 1.1 Fruit and Lab 1.2 Vehicle

1.Create a set of three classes that use inheritance:

A Fruit class that has two instance variables, private String name and private double weight. Fruit also has a 2-parameter constructor to set those and two getter methods for them, plus a toString method that returns this, overriding Objects toString - name and weightare the values of those instance variables:

Fruit: name, weight: weight

Note: use String.format to make sure the weight only has only 2 decimal places. Here's how:

String.format("%.2f", weight); // returns a String with exactly 2 digits following the decimal point, rounded 

A ColoredFruit class that extends Fruit and adds private String color, the color of the fruit. ColoredFruit has a 3-parameter constructor that calls Fruits constructor by using super and then sets the color, a getter for the color, and a toString method that overrides FruitstoString, adding this text at the end of what Fruit's toString returns:

, color: color - call Fruits toString & add this extra String to it.

Finally, an Apple class that extends ColoredFruit but does not add any instance variables or methods. Apple only has a 1-parameter constructor that takes a double weight parameter; Apples constructor calls ColoredFruits 3-parameter constructor by using super, passing in name "Apple", the weight, and color "red".

None of these classes should have a main method. This zyBook exercise provides a test class that creates & prints objects from these 3 classes.

Template:

public class Fruit // a simple Fruit class { /* declare the name and weight instance variables here */ /* write the 2-parameter Fruit constructor here */ /* write the two getter methods for name and weight here */ @Override public String toString() { /* write your implementation of the toString method here */ /* use String.format to return the weight with only 2 decimal places */ } /* THE COLOREDFRUIT AND APPLE CLASSES FOR YOU TO CREATE FOLLOW THIS MAIN METHOD BELOW */ public static void main(String[] args) { /* DO NOT MODIFY THESE TESTS IN MAIN */ Fruit fruit = new Fruit("a fruit", 0.5); ColoredFruit coloredFruit = new ColoredFruit("a colored fruit", 0.25, "orange"); Apple apple = new Apple(0.3); System.out.println(fruit); System.out.println(coloredFruit); System.out.println(apple); } }

/* DO NOT CHANGE THE FOLLOWING CLASS TO BE PUBLIC! */ class ColoredFruit /* modify this line so that ColoredFruit inherits from Fruit */ { /* declare the color instance variable for ColoredFruit here */ /* create the ColoredFruit constructor here and have it call Fruit's constructor by using super() before setting the color instance variable */ /* write a getter method for color here */ @Override public String toString() { /* write an overridden toString method here that adds color information to the end of the String that Fruit's toString returns, using super. */ } }

/* DO NOT CHANGE THE FOLLOWING CLASS TO BE PUBLIC! */ class Apple /* modify this line so that Apple inherits from ColoredFruit */ { /* write the Apple constructor here that only takes a weight parameter and calls ColoredFruit's constructor as specified in the requirements */ }

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

Template:

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

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

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions