Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA introduction course Create a project called AShip with two java files calle

ID: 670991 • Letter: J

Question

JAVA introduction course

Create a project called AShip with two java files called Ship.java and ShipTest.java
On top of each source code file (each .java file)

below is UML diagram of the class Ship. You can see that the class has 2 private fields and 5 public methods: 2 get methods, 2 set methods, and a method called timeToCrossEnglishChannel.

In Ship.java:
create a public class called Ship based on the UML diagram

Include the following three comments that label the three categories of class members

// fields // constructors // methods

Declare two private fields: name and speed.
The speed of boats is measured in knots. In other words: the value of speed is both entered and stored in knots.

Declare a public get and set method for each of the fields
Declare a public method called timeToCrossEnglishChannel.

It takes no parameter and returns the number of hours it takes to cross the channel. The return value is of type double.

Assume that the ship crosses the English Channel at the Strait of Dover, where the distance between France and England is 21 miles. You will need to convert the speed from knots to miles per hour. Use the following approximation:

1 knot = 1.151 mph

In ShipTest.java

The class ShipTest does not represent a real life object. It is also not intended to be used as a type of variables. The class InShipTest has the sole purpose to provide the required environment for the main method. (Each method in Java needs to be declared in the context of a class)

Write code to test the class Ship:

Create an instance of type Ship. Call the variable of type Ship paddleStreamer.

The name of the paddleStreamer is Rob Roy and the speed is 6 knots.

Use this information to assign the fields.

Create a second instance of type Ship. Call the variable hovercraft.

The name of the hovercraft is Princess Ann and the speed is 60 knots.

Use this information to assign the fields of hovercraft.

Display the information of both ship instances as shown in the Output above.

For each ship there is a label (e.g. Paddle Streamer). This lets the user know what information is displayed.
List the name, speed, and crossing time in the following three lines. Display the crossing time rounded to one decimal place.
The information of the two different ships should be separated by a single empty line. This makes your output more clear and easier to read

Output:

- name: String

- speed: Double

+ getName () : String

+ getSpeed () : double

+ setName (n: String)

+setSpeed (s: double)

+ timeToCrossEnglishChannel(): double

Ship

- name: String

- speed: Double

+ getName () : String

+ getSpeed () : double

+ setName (n: String)

+setSpeed (s: double)

+ timeToCrossEnglishChannel(): double

Explanation / Answer

Program:

//ship.java

public class Ship {
String name;
double speed;
public String getName()
{
return this.name;
}
public double getSpeed()
{
return this.speed;
}
public void setName(String n)
{
this.name=n;
}
public void setSpeed(double s)
{
this.speed=s;
}
public double timeToCrossEnglishChannel()
{
//distance between France and England is 21 miles.
//1 knot = 1.151 mph
double t=0;
t=21/this.speed;
return t;
}
}

//ShipTest.java

import java.text.DecimalFormat;
import java.math.RoundingMode;
public class ShipTest {
private static DecimalFormat df2 = new DecimalFormat("##.#");
public static void main(String[] args) {
Ship paddleStreamer=new Ship();
//The name of the paddleStreamer is Rob Roy and the speed is 6 knots.
paddleStreamer.setName("Rob Roy");
paddleStreamer.setSpeed(6);
double t1=paddleStreamer.timeToCrossEnglishChannel();
Ship hovercraft=new Ship();
//Princess Ann and the speed is 60 knots.
hovercraft.setName("Princess Ann");
hovercraft.setSpeed(60);
double t2=hovercraft.timeToCrossEnglishChannel();
System.out.println("Paddle Streamer");
System.out.println("Name: "+paddleStreamer.name);
System.out.println("Speed: "+paddleStreamer.speed+" knots");
System.out.println("Time to cross the channel: "+df2.format(t1)+" hours");
System.out.println("");
System.out.println("Hover Craft");
System.out.println("Name: "+hovercraft.name);
System.out.println("Speed: "+hovercraft.speed+" knots");
df2.setRoundingMode(RoundingMode.DOWN);
System.out.println("Time to cross the channel: "+df2.format(t2)+" hours");
  
}
  
}

Output:

Paddle Streamer
Name: Rob Roy
Speed: 6.0 knots
Time to cross the channel: 3.5 hours

Hover Craft
Name: Princess Ann
Speed: 60.0 knots
Time to cross the channel: 0.3 hours