In java GUI Design a Ship class with the following information: Name Year A cons
ID: 3808019 • Letter: I
Question
In java GUI
Design a Ship class with the following information:
Name
Year
A constructor and appropriate accessors and mutators
toString method
A speed method to calculate/determine the spped(generate a random value for the speed between 30 – 50 knots per hour).
Design a CruiseShip class that extends the Ship class. The CruiseShip class will have the following:
Maximum passengers
A constructor and appropriate accessors and mutators
A toString that displays the ship’s name and number of passengers. (override the base class toString)
Override the speed method where the speed = (random value between 50 – 70) – (.1 * # passengers * 200). If the value is negative take the absolute value
Design a CargoShip class that extends the Ship class. The CargoShip class will have the following:
Cargo capacity
A constructor and appropriate accessors and mutators
A toString that displays the ship’s name and capacity. (override the base class toString)
Override the speed method where the speed = (random value between 40 – 50) – (.001 * cargo size)
Demonstrate the classes in a GUI program that has a Ship array. Assign the various ships to the array and allow the user to step through the array.
Your application will be able to read a text file containing ship information. The data file will be in the following format, where each data member is separated by a colon:For a ship:
<ship_type>:<ship_name>:<ship_year>
For a CruiseShip:
<ship_type>:<ship_name>:<max_passengers>
For a CargoShip:
<ship_type>:<ship_name>:<cargo_capacity>
The ship type is a number in the range listed below. A type number out of this range can be ignored.
<ship_type>
Range
Ship
1-49
CruiseShip
100-200
CargoShip
50-99
<ship_type>
Range
Ship
1-49
CruiseShip
100-200
CargoShip
50-99
Explanation / Answer
Ship.java
import java.util.Random;
//creating Ship class with required properties
class Ship{
private String name;
private int year;
//constructor initializes instance vars
public Ship(String name,int year){
this.name = name;
this.year = year;
}
//constructor initializes instance vars with default values
public Ship(){
this.name = "";
this.year = 0;
}
//setters
public void setName(String name){
this.name = name;
}
public void setYear(int year){
this.year = year;
}
//getters
public String getName(){
return this.name;
}
public int getYear(){
return this.year;
}
//overrides toString method in Object class
public String toString(){
return "";
}
//calculates speed using Random class and returns
public int calculateSpeed(){
Random r = new Random();
int no = r.nextInt(21);
no+=30;
return no;
}
}
CruiseShip.java
import java.util.Random;
//this class inherits properties of Ship class also
class CruiseShip extends Ship{
private int maxPassengers;
//constructor initializes instance vars
public CruiseShip(String name,int maxPas){
this.maxPassengers = maxPas;
this.setName(name);
}
//setter
public void setMaxPassengers(int maxP){
this.maxPassengers = maxP;
}
//getter
public int getMaxPassengers(){
return this.maxPassengers;
}
//overrides toString method in Ship class
public String toString(){
return getName()+" "+this.maxPassengers;
}
//calculates speed using Random class and returns
public int calculateSpeed(){
Random r = new Random();
int no = r.nextInt(21);
no+=50;
int val = (int)0.1*maxPassengers*200;
return Math.abs(no-val);
}
}
CargoShip.java
import java.util.Random;
//this class inherits properties of Ship class also
class CargoShip extends Ship{
private int cargoCapacity;
//constructor initializes instance vars
public CargoShip(String name,int cCapacity){
this.cargoCapacity = cCapacity;
this.setName(name);
}
//setter
public void setCargoCapacity(int cc){
this.cargoCapacity = cc;
}
//getter
public int getCargoCapacity(){
return this.cargoCapacity;
}
//overrides toString method in Ship class
public String toString(){
return getName()+" "+this.cargoCapacity;
}
//calculates speed using Random class and returns
public int calculateSpeed(){
Random r = new Random();
int no = r.nextInt(11);
no+=40;
int val = (int)0.001*cargoCapacity;
return Math.abs(no-val);
}
}
GUIProgram.java
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.JOptionPane;
//this GUIProgram class takes input a name of text file and reads and creates different types of ships and add them to ships array.
class GUIProgram extends JFrame implements ActionListener{
ArrayList<Ship> ships = new ArrayList<Ship>();//arraylist of ships
JLabel enterFile;//prints message "Enter file name"
JTextField fileName;//takes file name
JButton enter;//initializes the task of reading input file and add ships to ships array
public GUIProgram(){
Container c = getContentPane();
setLayout(new FlowLayout());
enterFile = new JLabel("Enter file name:");
fileName = new JTextField(20);
enter = new JButton("Submit");
//adding action listener to button
enter.addActionListener(this);
//adding elements to frame
add(enterFile);
add(fileName);
add(enter);
}
public static void main(String a[]){
GUIProgram gui = new GUIProgram();
gui.setSize(900,500);//frame size
gui.setResizable(false);
gui.setTitle("Adding Different Ships");//title for frame
gui.setVisible(true);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
if(!fileName.getText().trim().equals("")){//we will go for reading file only if the content in text field is not empty
readFile(fileName.getText().trim());//reads file and add ships to ships array
}
}
public void readFile(String fileName){
//reads file and add ships to ships array
try{
FileReader txtfile = null;
BufferedReader br = null;
StringBuilder sb = null;
txtfile = new FileReader(fileName);
br = new BufferedReader(txtfile);
sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
String str[] = line.split(":");//splitting contents of a line
int shipType = Integer.parseInt(str[0]);
int num = Integer.parseInt(str[2]);
Ship ship = null;
//based on value of shiptype we will create different types of ships
if(shipType>=1 && shipType<=49){
ship = new Ship(str[1],num);
}
else if(shipType>=100 && shipType<=200){
ship = new CruiseShip(str[1],num);
}
else if(shipType>=50 && shipType<=99){
ship = new CargoShip(str[1],num);
}
if(ship!=null)//we will add this ship to ships array only if it is instantiated
ships.add(ship);
line = br.readLine();
}
br.close();
txtfile.close();
//displaying message that reading and adding completed!!
JOptionPane.showMessageDialog(null,"All ships are added to ships array","FileRead completed!",JOptionPane.WARNING_MESSAGE);
}
catch(IOException e){
//if some error occur in file reading
System.out.println("Some error in reading input file!!!");
}
}
}
Thankyou...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.