Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

DNA Databases

Authors: Stefan Kiesbye

1st Edition

0737758910, 978-0737758917

More Books

Students also viewed these Databases questions