Question: In this question you are to create a Ship class and a CruiseShip class (derived from the Ship class). All instructions are in comments in

In this question you are to create a Ship class and a CruiseShip class (derived from the Ship class). All instructions are in comments in the classes. You also need to complete the main method. You have been asked to write methods that are not used in this program - write them anyway because I will grade them. If in doubt, ask!.

Test3.java

public class Test3 {

public static void main(String[] args) { Scanner scnr = new Scanner(System.in); //create two cruiseships String name = scnr.next(); int yearBuilt = scnr.nextInt(); int capacity = scnr.nextInt(); CruiseShip s1 = new CruiseShip(name, yearBuilt, capacity); name = scnr.next(); yearBuilt = scnr.nextInt(); capacity = scnr.nextInt(); CruiseShip s2 = new CruiseShip(name, yearBuilt, capacity); //write code to output which is larger //you must use compareTo //do not put them into an arrayList or array. Do not sort using a sort such as Collections sort. if( //use compareTo here ) System.out.println(s1.getName() + " is larger"); else if ( //use compareTo here ) System.out.println(s2.getName() + " is larger"); else System.out.println("They are the same size"); } }

Ship.java

//ship has attributes //name (a String) //and //yearBuilt (an int) //write constructors, ALL mutators and accessors, and a writeOutput . //even though these are mostly unused in this program, you need to write them to get a grade public class Ship { }

CruiseShip.java

/* *CruiseShip inherits from Ship and has an attribute called capacity (an int) which is the maximum number of passengers write all constructors, accessors, and mutators write a writeOutput method. Write a compareTo that compares according to the capacity

*/

public class CruiseShip extends Ship { int capacity ; public CruiseShip(String name, int yearBuilt, int capacity) { super(name, yearBuilt); this.capacity = capacity; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } @Override public String toString() { return super.toString() + ", capacity=" + capacity ; } public int compareTo(CruiseShip other) { if(this.capacity > other.capacity) return 1 ; else if(this.capacity < other.capacity) return -1 ; return 0 ; } }

Rubric:

Ship class: attributes, constructors, accessors, mutators

Ship class: writeOutput

CruiseShip class: correctly derived from Ship class

CruiseShip class: constructors and writeOutput correctly depend on Ship class

CruiseShip class: accessors, mutators

CruiseShip class : compareTo correctly written

Main: correct use of compareTo

Correct output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!