in Java Inheritance hierarchy watercraft <====sailboat and canoe a. (Refer to in
ID: 3737013 • Letter: I
Question
in Java Inheritance hierarchy
watercraft <====sailboat and canoe
a. (Refer to inheritance hierarchy 8-1.) Write the code for the WaterCraft class. This class should include instance variables named id (int), type (string), and length (double) that can only be accessed by the class. Code a single constructor that assigns default values to the instance variables, and code get and set methods for the instance variables.
b. Write the code for the Sailboat class. This class should extend the WaterCraft class by adding an instance variable named sails (int) and get and set methods for this variable. It’s constructor should initialize the instance variables defined by the WaterCraft class by calling the constructor of that class, and it should assign a default value to the sails instance variable.
c. (Refer to inheritance hierarchy 8-1.) Code a public method for the WaterCraft class that overrides the toString method of the Object class. This method should return a string that contains the id, type, and length variables in this format: ID: 1234 Type: Sailboat Length: 19'
d. Assume that you have created a Sailboat object named s and assigned it to a WaterCraft object named w. Write a statement that will print the type of the WaterCraft object to the console.
e. Assume that you have created a WaterCraft object named w. Write an if clause that will check if the object is a Sailboat object, using the easiest technique available.
f. Assume that the WaterCraft class has been defined as abstract with an abstract method name getDisplayText() that accepts no parameter and returns a string: i. public abstract String getDisplayText();
Write the code for a method in the Sailboat class that overrides the abstract getDisplayText method in the WaterCraft class. The value that’s returned by this method should include the length and type fields from the WaterCraft class and the sails field from the Sailboat class formatted like this: 19.0' Sailboat with 3 sails
PLEASE IN THE ANSWER PUT A IS THIS, B IS THIS AND SO ON thank you
Explanation / Answer
//a abstract class WaterCraft{ private int id; private String type; private double length; public WaterCraft(int id, String type, double length) { this.id = id; this.type = type; this.length = length; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } //c @Override public String toString() { return "WaterCraft{" + "id=" + id + ", type='" + type + ''' + ", length=" + length + '}'; } abstract public String getDisplayText(); } //b class SailBoat extends WaterCraft{ private int sails; public SailBoat(int id, String type, double length, int sails) { super(id, type, length); this.sails = sails; } public int getSails(){ return sails; } public void setSails(int sails){ this.sails = sails; } //f @Override public String getDisplayText() { return getLength()+"' "+getType()+" with "+sails+" sails"; } } class Main{ public static void main(String[] args) { //d SailBoat s = new SailBoat(1,"SailBoat",19,3); WaterCraft w = s; System.out.println(w.getType()); //e WaterCraft w1 = new WaterCraft(2, "WC", 10) { @Override public String getDisplayText() { return null; } }; if( w1 instanceof SailBoat){ System.out.println("W1 is instance of sailboat"); } //f System.out.println(s.getDisplayText()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.