Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is what project 2 entails and here is my code so far. I need help getting project 2 done. ----------------------CargoShip.java-------------------------- import java.util.Scanner; public class

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribed

Here is what project 2 entails and here is my code so far. I need help getting project 2 done.

----------------------CargoShip.java--------------------------

import java.util.Scanner;

public class CargoShip extends Ship{

double cargoValue;

double cargoVolume;

double cargoWeight;

public CargoShip(Scanner scan) {

super(scan);

cargoWeight=scan.nextDouble();

cargoVolume=scan.nextDouble();

cargoValue=scan.nextDouble();

}//end scanner constructor CargoShip subclass

//All get methods

public double getCargoValue() {

return cargoValue;

}//end getCargoValue

public double getCargoVolume() {

return cargoVolume;

}//end getCargoVolume

public double getCargoWeight() {

return cargoWeight;

}//end getCArgoWeight

public void setCargoValue(double cargoValue) {

this.cargoValue = cargoValue;

}//end setCargoValue

public void setCargoVolume(double cargoVolume) {

this.cargoVolume = cargoVolume;

}//endsetCargoVolume

public void setCargoWeight(double cargoWeight) {

this.cargoWeight = cargoWeight;

}//end setCargoWeight

//Overriden toString mehtods

@Override

public String toString()

{

return "Cargo Ship: "+name+" "+index;

}//end toString method

}//end CargoShip class

----------------------Dock.java--------------------------

import java.util.Scanner;

public class Dock extends Thing{

Ship ship;

public Dock(Scanner scan) {

super(scan);

}//end scanner constructor Dock subclass

public Ship getShip() {

return ship;

}//end getShip

public void setShip(Ship ship) {

this.ship = ship;

}//end setShip

//Overriden toString method

@Override

public String toString()

{

return "Dock: "+super.toString()+" Ship: "+ship.toString();

}//end toString method

}//end Thing class

----------------------Job.java--------------------------

import java.util.ArrayList;

import java.util.Scanner;

public class Job extends Thing{

double duration;

ArrayList requirements;

public Job(Scanner scan) {

super(scan);

}//end scanner construcotr Job subclass

//All get methods

public double getDuration() {

return duration;

}//end getDuration

public ArrayList getRequirements() {

return requirements;

}//end getRequirements

//All set methods

public void setDuration(double duration) {

this.duration = duration;

}//end setDuration

public void setRequirements(ArrayList requirements) {

this.requirements = requirements;

}//end setRequirements

}//end Job class

----------------------NameComparator.java--------------------------

import java.util.Comparator;

public class NameComparator implements Comparator{

@Override

public int compare(Thing a, Thing b) {

return a.getName().compareToIgnoreCase(b.getName());

}//end compare method

}//end compareNames

----------------------PassengerShip.java--------------------------

import java.util.Scanner;

public class PassengerShip extends Ship{

int numberOfOccupiedRooms;

int numberOfPassengers;

int numberOfRooms;

public PassengerShip (Scanner scan) {

super (scan);

if (scan.hasNextInt()) numberOfPassengers = scan.nextInt();

if (scan.hasNextInt()) numberOfRooms = scan.nextInt();

if (scan.hasNextInt()) numberOfOccupiedRooms = scan.nextInt();

} //end Scanner constructor PassengerShip

//All get methods

public int getNumberOfOccupiedRooms() {

return numberOfOccupiedRooms;

}//end getNumberOfOccupiedRooms

public int getNumberOfPassengers() {

return numberOfPassengers;

}//end getNumberOfPassengers

public int getNumberOfRooms() {

return numberOfRooms;

}//end getNumberOfRooms

//All set methods

public void setNumberOfOccupiedRooms(int numberOfOccupiedRooms) {

this.numberOfOccupiedRooms = numberOfOccupiedRooms;

}//end setNumberOfOccupiedRooms

public void setNumberOfPassengers(int numberOfPassengers) {

this.numberOfPassengers = numberOfPassengers;

}//end setNumberOfPassengers

public void setNumberOfRooms(int numberOfRooms) {

this.numberOfRooms = numberOfRooms;

}//end setnumberOfRooms

//Overriden stString method

@Override

public String toString () {

String temp = "Passenger ship: " + super.toString();

if (jobs.isEmpty())

return temp;

temp = jobs.stream().map((mj) -> " - " + mj).reduce(temp, String::concat);

return temp;

} // end method toString

}

----------------------Person.java--------------------------

import java.util.Scanner;

public class Person extends Thing{

String skills;

public Person(Scanner sc)

{

super(sc);

skills=sc.next();

}

public String getSkills() {

return skills;

}

public void setSkills(String skills) {

this.skills = skills;

}

@Override

public String toString()

{

return "Person: "+name+" "+index+" "+skills;

}

}

----------------------PortTime.java--------------------------

public class PortTime {

int time;

public PortTime(int time) {

this.time = time;

}

public int getTime() {

return time;

}

public void setTime(int time) {

this.time = time;

}

}

----------------------QueComparator.java--------------------------

import java.util.Comparator;

public class QueComparator implements Comparator{

@Override

public int compare(Ship o1, Ship o2) {

if(o1.weight>o2.weight)

return 1;

else if(o1.weight

return -1;

else if(o1.length>o2.length)

return 1;

else if(o1.length

return -1;

else if(o1.width>o2.width)

return 1;

else if(o1.width

return -1;

else if(o1.draft>o2.draft)

return 1;

else if(o1.draft

return -1;

else

return 0;

}

}

----------------------SeaPort.java--------------------------

import java.util.ArrayList;

import java.util.Scanner;

public class SeaPort extends Thing{

ArrayList docks;

ArrayList ships;

ArrayList que;

ArrayList persons;

public SeaPort(Scanner sc) {

super(sc);

docks=new ArrayList();

ships=new ArrayList();

que=new ArrayList();

persons=new ArrayList();

}

public ArrayList getDocks() {

return docks;

}

public void setDocks(ArrayList docks) {

this.docks = docks;

}

public ArrayList getShips() {

return ships;

}

public void setShips(ArrayList ships) {

this.ships = ships;

}

public ArrayList getQue() {

return que;

}

public void setQue(ArrayList que) {

this.que = que;

}

public ArrayList getPersons() {

return persons;

}

public void setPersons(ArrayList persons) {

this.persons = persons;

}

public String toString () {

String st = " SeaPort: " + super.toString();

for (Dock md: docks) st += " " + md;

st += " --- List of all ships in que:";

for (Ship ms: que ) st += " > " + ms;

st += " --- List of all ships:";

for (Ship ms: ships) st += " > " + ms;

st += " --- List of all persons:";

for (Person mp: persons) st += " > " + mp;

return st;

} // end method toString

}

----------------------SeaPortProgram.java--------------------------

import java.awt.EventQueue; //unused

import java.awt.EventQueue;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.Scanner;

import javax.swing.ButtonGroup;

import javax.swing.JFrame;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JRadioButton;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JScrollPane;

import javax.swing.JComboBox;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class SeaPortProgram extends JFrame {

private JFrame frame;

private JTextField textField;

private JRadioButton name;

private JTextArea output;

private JTextField search;

private JRadioButton skill;

private JRadioButton index;

private JButton jButton1;

private JButton jButton2;

private JButton sortData;

private ButtonGroup buttonGroup1;

World world = new World(null);

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

new SeaPortProgram().setVisible(true);

try {

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager

.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

}

}

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(SeaPortProgram.class.getName())

.log(java.util.logging.Level.SEVERE, null, ex);

}

SeaPortProgram window = new SeaPortProgram();

window.frame.setVisible(true);

}

});

}

public SeaPortProgram() {

initialize();

}

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 450, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

JButton btnOpen = new JButton("Open");

btnOpen.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

// fix-5 no implementation found for this OPEN action performed

//please write openActionPerformed(evt) method and call here

}

});

btnOpen.setBounds(186, 243, 117, 29);

frame.getContentPane().add(btnOpen);

JButton btnCancel = new JButton("Cancel");

btnCancel.setBounds(296, 243, 117, 29);

frame.getContentPane().add(btnCancel);

JLabel lblLookUp = new JLabel("Look Up:");

lblLookUp.setBounds(6, 43, 61, 16);

frame.getContentPane().add(lblLookUp);

JLabel lblFileName = new JLabel("File Name:");

lblFileName.setBounds(6, 71, 76, 16);

frame.getContentPane().add(lblFileName);

JLabel lblNewLabel = new JLabel("Files Of Type:");

lblNewLabel.setBounds(6, 99, 86, 16);

frame.getContentPane().add(lblNewLabel);

textField = new JTextField();

textField.setBounds(82, 65, 331, 28);

frame.getContentPane().add(textField);

textField.setColumns(10);

JScrollPane jScrollPane2 = new JScrollPane();

jScrollPane2.setBounds(6, 127, 438, 109);

frame.getContentPane().add(jScrollPane2);

JComboBox comboBox = new JComboBox();

comboBox.setBounds(94, 95, 319, 27);

frame.getContentPane().add(comboBox);

JComboBox comboBox_1 = new JComboBox();

comboBox_1.setBounds(68, 39, 345, 27);

frame.getContentPane().add(comboBox_1);

// fix-3 initilized the followng components (jButton1 ...to... sortData)

buttonGroup1 = new ButtonGroup();

jButton1 = new JButton();

index = new JRadioButton();

name = new JRadioButton();

skill = new JRadioButton();

output = new JTextArea();

jButton2 = new JButton();

sortData = new JButton();

jButton1.setText("Search");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

// fix-4 calling wrong method actionPerformed(evt); have to call jButton2ActionPerformed(evt);

// actionPerformed(evt);

jButton2ActionPerformed(evt);

}

});

buttonGroup1.add(index);

index.setText("Index");

buttonGroup1.add(name);

name.setText("Name");

buttonGroup1.add(skill);

skill.setText("Skill");

output.setColumns(20);

output.setRows(5);

jScrollPane2.setViewportView(output);

jButton2.setText("Clear");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

}

});

sortData.setText("Sort");

sortData.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

sortDataActionPerformed(evt);

}

});

//fix-6 index, name, skill, output and sortData are not added to frame|content pane.

// please add them in appropriate position as needed

}

public void readFile(File f)

{

try

{

BufferedReader input=new BufferedReader(new FileReader(f));

HashMap hashmap=new HashMap();

while(input.ready())

{

String str=input.readLine().trim();

Scanner sc=new Scanner(str);

if(!str.startsWith("//"))

{

String type="";

if(sc.hasNext())

{

type=sc.next();

}

if(type.equalsIgnoreCase("port"))

{

world.assignPort(new SeaPort(sc));

}

else if(type.equalsIgnoreCase("dock"))

{

world.assignDock(new Dock(sc));

}

else if(type.equalsIgnoreCase("ship"))

{

Ship s=new Ship(sc);

hashmap.put(s.getIndex(), s);

world.assignShip(s);

}

else if(type.equalsIgnoreCase("pship"))

{

Ship s=new passengerShip(sc);

hashmap.put(s.getIndex(), s);

world.assignShip(s);

}

else if(type.equalsIgnoreCase("cship"))

{

Ship s=new CargoShip(sc);

hashmap.put(s.getIndex(),s);

world.assignShip(s);

}

else if(type.equalsIgnoreCase("person"))

{

world.assignPerson(new Person(sc));

}

}

}

output.setText(world.toString());

}

catch (Exception e)

{

System.out.println(e+"-----");

}

} // } fix-1 added this end brace

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

if (index.isSelected()) {

try {

int ind = Integer.parseInt(search.getText());

output.setText("");

String out = "";

ArrayList arr = world.getPorts();

for (SeaPort s : arr) {

boolean flag = false;

ArrayList dArr = s.getDocks();

for (Dock d : dArr) {

if (d.getIndex() == ind) {

out += d.toString() + " ";

flag = true;

break;

}

}

if (flag)

break;

ArrayList sArr = s.getShips();

for (Ship d : sArr) {

if (d.getIndex() == ind) {

out += d.toString() + " ";

flag = true;

break;

}

}

if (flag)

break;

ArrayList qArr = s.getQue();

for (Ship d : qArr) {

if (d.getIndex() == ind) {

out += d.toString() + " ";

flag = true;

break;

}

}

if (flag)

break;

ArrayList pArr = s.getPersons();

for (Person d : pArr) {

if (d.getIndex() == ind) {

out += d.toString() + " ";

flag = true;

break;

}

}

if (flag)

break;

}

output.setText(out);

} catch (Exception e) {

System.out.println(e.getMessage());

}

} else if (skill.isSelected()) {

String skillString = search.getText();

output.setText("");

String out = "";

ArrayList arr = world.getPorts();

for (SeaPort s : arr) {

ArrayList pArr = s.getPersons();

for (Person p : pArr) {

if (p.getSkills().equalsIgnoreCase(skillString)) {

out += p.toString() + " ";

}

}

}

output.setText(out);

} else if (name.isSelected()) {

String nameString = search.getText();

output.setText("");

String out = "";

ArrayList arr = world.getPorts();

for (SeaPort s : arr) {

ArrayList dArr = s.getDocks();

for (Dock d : dArr) {

if (d.getName().equalsIgnoreCase(nameString)) {

out += d.toString() + " ";

}

}

ArrayList sArr = s.getShips();

for (Ship d : sArr) {

if (d.getName().equalsIgnoreCase(nameString)) {

out += d.toString() + " ";

}

}

ArrayList qArr = s.getQue();

for (Ship d : qArr) {

if (d.getName().equalsIgnoreCase(nameString)) {

out += d.toString() + " ";

}

}

ArrayList pArr = s.getPersons();

for (Person d : pArr) {

if (d.getName().equalsIgnoreCase(nameString)) {

out += d.toString() + " ";

}

}

}

output.setText(out);

}

}

// } fix-2 remove this end brace

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

search.setText("");

output.setText("");

}

private void sortDataActionPerformed(java.awt.event.ActionEvent evt) {

for (SeaPort s : world.getPorts()) {

Collections.sort(s.getDocks(), new NameComparator());

Collections.sort(s.getPersons(), new NameComparator());

Collections.sort(s.getShips(), new NameComparator());

Collections.sort(s.getQue(), new QueComparator());

}

output.setText(world.toString());

}

}

----------------------Ship.java--------------------------

import java.util.ArrayList;

import java.util.Comparator;

import java.util.Scanner;

public class Ship extends Thing{

PortTime arrivalTime,dockTime;

double draft,length,weight,width;

ArrayList jobs;

public Ship(Scanner sc)

{

super(sc);

weight=sc.nextDouble();

length=sc.nextDouble();

width=sc.nextDouble();

draft=sc.nextDouble();

jobs=new ArrayList();

}

public PortTime getArrivalTime() {

return arrivalTime;

}

public void setArrivalTime(PortTime arrivalTime) {

this.arrivalTime = arrivalTime;

}

public PortTime getDockTime() {

return dockTime;

}

public void setDockTime(PortTime dockTime) {

this.dockTime = dockTime;

}

public double getDraft() {

return draft;

}

public void setDraft(double draft) {

this.draft = draft;

}

public double getLength() {

return length;

}

public void setLength(double length) {

this.length = length;

}

public double getWeight() {

return weight;

}

public void setWeight(double weight) {

this.weight = weight;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public ArrayList getJobs() {

return jobs;

}

public void setJobs(ArrayList jobs) {

this.jobs = jobs;

}

}

----------------------Thing.java--------------------------

import java.util.Scanner;

public class Thing {

String name;

int index;

int parent;

public Thing(Scanner sc)

{

if(sc!=null)

{

name=sc.next();

index=sc.nextInt();

parent=sc.nextInt();

}

}

public int getIndex() {

return index;

}

public String getName() {

return name;

}

public int getParent() {

return parent;

}

@Override

public String toString()

{

return name+" "+index;

}

}

----------------------World.java--------------------------

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Scanner;

public class World extends Thing{

ArrayList ports;

PortTime time;

HashMap hashmap;

public World(Scanner sc) {

super(sc);

ports=new ArrayList();

}

public ArrayList getPorts() {

return ports;

}

public void setPorts(ArrayList ports) {

this.ports = ports;

}

public PortTime getTime() {

return time;

}

public void setTime(PortTime time) {

this.time = time;

}

Ship getShipByIndex (int x,HashMap hms) {

if(hms.containsKey(x))

{

return hms.get(x);

}

return null;

} // end getShipByIndex

Dock getDockByIndex(int x)

{

for (SeaPort msp: ports)

for (Dock ms: msp.docks)

if (ms.index == x)

return ms;

return null;

}// end getDockByIndex

SeaPort getSeaPortByIndex(int x)

{

for (SeaPort msp: ports)

if(msp.index==x)

return msp;

return null;

}

void assignShip (Ship ms) {

Dock md = getDockByIndex(ms.parent);

if (md == null) {

SeaPort p=getSeaPortByIndex (ms.parent);

if(p!=null)

{

p.ships.add (ms);

p.que.add (ms);

}

return;

}

else

{

md.ship = ms;

getSeaPortByIndex (md.parent).ships.add (ms);

}

} // end method assignShip

void assignPerson(Person p)

{

for(SeaPort sp:ports)

{

if(sp.getIndex()==p.getParent())

{

sp.persons.add(p);

}

}

} // end assignPerson

void assignDock(Dock d)

{

for(SeaPort sp:ports)

{

if(sp.getIndex()==d.getParent())

{

sp.docks.add(d);

}

}

}//end assignDock

void assignPort(SeaPort p)

{

ports.add(p);

}

public String toString()

{

String str="";

for(SeaPort s:ports)

{

str+=s.toString()+" ";

}

return str;

}

}

Introduction the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define: SeaPortProgram extends JFrame . o variables used by the GUI interface o world: World Thing implement Comparable o time: PortTime . SeaPort extends Thing o docks: ArrayList o que: ArrayList// the list of ships waiting to dock o ships: ArrayList /a list of all the ships at this port o persons: ArrayList //people with skills at this port . Dock extends Thing o ship: Ship . Ship extends Thing o arrivalTime, dockTime: PortTime o draft, length, weight, width: double o jobs: ArrayList . PassengerShip extends Ship o numberOfOccupiedRooms: int numberOfPassengers: i numberOfRooms: int o . CargoShip extends Ship o cargoValue: double o cargoVolume: double cargoWeight: double o skill: String o duration: double Person extends Thing . Job extends Thing - optional till Projects 3 and 4 o requirements: ArrayList

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_2

Step: 3

blur-text-image_3

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

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions

Question

What are the five forces that determine an industrys profitability?

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago