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

In this assignment, you will enter, store, and retrieve information about ships.

ID: 3576973 • Letter: I

Question

In this assignment, you will enter, store, and retrieve information about ships. You have been provided with a JavaFX GUI, a JUnit test file, and an interface file that will guide you in developing the application. (Note: the information about each ship is not exactly as the maritime experts would state it. Follow these requirements instead.)

1) When the user starts the application, the following window will be presented (JavaFX GUI provided)

2) The user will select a radio button specifying which type of ship they wish to create. The ship type text field will be pre-populated, but the user will type in the Ship’s name and year built, and for a cargo ship, the tonnage, or for a cruise ship, the number of passengers.

3) If the user selects the Warship radio button, another set of radio buttons will be displayed corresponding to six categories of warship. If the user decides to enter a general warship (i.e., not one of the six sub-types), they are presented with appropriate textboxes.

4) If the user decides to enter one of the six sub-categories of warships by selecting a radio button, they will be provided with a pre-populated ship type and appropriate text fields to fill in.

5) In any of the above cases, the user will then select the “Add a Ship” button to create an instance of the specified ship and add it to the ShipRegistry arraylist. If the user wishes to return to the radio-buttons for the three class of ships, they can select “Reset the Ship Types”.  

6) To present a window showing the current ships, the user should select “Display Ships”. The ships should be displayed in sorted order, by the alphabetic order of the ship name.

7) To read from a file, select the “Read File” button. The user will be presented with a file explorer from which they can select an input file. If the file is correctly formatted, each line will become a ship added to the ShipRegistry. Similarly, to write the current ShipRegistry to a file, the user selects “Write Ships”.

Specifications:

General - Write a Java program to allow the user to enter information about a ship, add it to a “registry”, write the ship information to a file, and read the information into the registry.

·         Create an abstract class called Ship with private fields ‘name’ and ‘year’ of type String, and ‘type’ of ShipType, and public getters and setters for each. The field ‘name’ represents the name of the ship, ‘year’ is the year commissioned, and ‘type’ is a string that represents one of the enumeration values below.

·         Data Elements: Create three classes that inherit from Ship: CargoShip, CruiseShip, and WarShip. Instances of WarShip will be further refined into five types, but will all be WarShip objects.

·         Create an enumeration class named ShipType.java that has the following values: CARGO, CRUISE, WARSHIP, CARRIER, CRUISER, DESTROYER, MINE_SWEEPER, and SUBMARINE. These categories are meant to represent the following:

CARGO – a merchant ship carrying cargo

CRUISE – a passenger ship

WARSHIP – a general warship, which is not further refined by the following categories (we will record guns, torpedoes, and aircraft, although older warships only had guns)

CARRIER – an aircraft carrier

CRUISER – a cruiser (while cruisers have sophisticated electronic and sonar equipment, missile launchers, etc, which define them, we will simplify them by just recording a number of guns)

DESTROYER – a destroyer (while destroyer have torpedo launchers, missile launchers, depth charges, and sophisticated electronic and sonar equipment, etc, we will simplify them by just recording a number of guns)

MINE_SWEEPER – a counter-mine ship (while mine-sweepers have sophisticated electronics, sonar, other counter-mine equipment, we will simplify them by just recording a number of guns)

SUBMARINE – a submarine (while modern submarines have missile launch capability, we will simplify them by just recording a number of torpedo launchers)

·         Create a ShipRegistry class that implements ShipRegistryInterface. It will contain an Arraylist which holds Ship objects (Cargo, Cruise, and Warship all extending Ship). It will have methods required by the ShipRegistryInterface, i.e., addShip, getShips, getShipDescriptions, getWarshipDescriptions, readFile, and writeFile.

·         Create a toString method for each ship class that will return a String describing the ship’s name, year commissioned, and type, and the following field or fields depending on ship type. For each ship type, the fourth and subsequent fields will be:

CARGO – number of tons of cargo

CRUISE – number of passengers

WARSHIP – number of guns, number of torpedo launchers, and number of aircraft

CARRIER – number of aircraft

CRUISER – number of guns

DESTROYER – number of guns

MINE_SWEEPER – number of guns

SUBMARINE – number of torpedo launchers

      These methods need not be precisely worded, and are not tested by the JUnit tests. They are used to display the ships in the GUI if the “Display Ships” button is selected.

·         Create a toString() method for the ShipRegistry class that sorts the current ships according to the alphabetic order of the ship’s name, and then iterates through the current ships and appends each ship’s toString method to the string returned. This will require the Ship class to implement the Comparable interface, which requires Ship to implement the compareTo method. This method should get the ships’ names and compare the names using the library compareTo method for Strings.

·         Create a writeString method for each ship class and category that returns an exact string that will be written to an output file, and then perhaps read back in. The writeString method for each type of ship will have the following form. Note that all fields are separated by commas, so that the output file that is written using this method becomes a comma-delimited file (e.g., a csv file). These methods do need to be in exactly the following format. The first three fields are the string representation, and the subsequent field(s) is a string representation of an integer.

CARGO – “name,shipType,year,tonnage”

CRUISE – “name,shipType,year,passengers “

WARSHIP – “name,shipType,year,guns,torpedos,aircraft”

CARRIER – “name,shipType,year,aircraft”

CRUISER – “name,shipType,year,guns”

DESTROYER – “name,shipType,year,guns”

MINE_SWEEPER – “name,shipType,year,guns”

SUBMARINE – – “name,shipType,year,torpedoes”

***DriverFX.java***

***MainPaneFX.java***

import java.io.File;

***ShipRegistryInterface.java***

import java.io.File;

***ships.csv***

Edmund Fitzgerald / Cargo / 1958 / 8713

Enterprise (CVN-65) / Carrier / 1960 / 90

Queen Elizabeth 2 / Cruise / 1967 / 1777

Queen Mary 2 / Cruise / 2003 / 2620

Constitution / Warship / 1798 / 44 / 0 / 0

Bainbridge (DD-1) / Destroyer / 1903 / 7

Ardent (MCM-12) / Mine Sweeper / 1994 / 2

Antietam (CG-54) / Cruiser / 1986 / 18

Explanation / Answer

Answer:

import java.util.*;

public class BattleShipGame {

    public static void main(String[] args) {
        int[][] board = new int[10][10];
        int[][] ships = new int[10][10];
        int[] shoot = new int[10];
        int attempts=0,
            shotHit=0;
      
        initBoard(board);
        initShips(ships);
      
        System.out.println();
      
        do{
            showBoard(board);
            shoot(shoot);
            attempts++;
          
            if(hit(shoot,ships)){
                hint(shoot,ships,attempts);
                shotHit++;
            }              
            else
                hint(shoot,ships,attempts);
          
            changeboard(shoot,ships,board);
          

        }while(shotHit!=3);
      
        System.out.println(" Battleship Java game finished! You hit 3 ships in "+attempts+" attempts");
        showBoard(board);
    }
  
    public static void initBoard(int[][] board){
        for(int row=0 ; row < 10 ; row++ )
            for(int column=0 ; column < 10 ; column++ )
                board[row][column]=-1;
    }
  
    public static void showBoard(int[][] board){
        System.out.println(" 1 2 3 4 5");
        System.out.println();
      
        for(int row=0 ; row < 10 ; row++ ){
            System.out.print((row+1)+"");
            for(int column=0 ; column < 10 ; column++ ){
                if(board[row][column]==-1){
                    System.out.print(" "+"~");
                }else if(board[row][column]==0){
                    System.out.print(" "+"*");
                }else if(board[row][column]==1){
                    System.out.print(" "+"X");
                }
              
            }
            System.out.println();
        }

    }

    public static void initShips(int[][] ships){
        Random random = new Random();
      
        for(int ship=0 ; ship < 10; ship++){
            ships[ship][0]=random.nextInt(10);
            ships[ship][1]=random.nextInt(10);
          
          
            for(int last=0 ; last < ship ; last++){
                if( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) )
                    do{
                        ships[ship][0]=random.nextInt(5);
                        ships[ship][1]=random.nextInt(5);
                    }while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) );
            }
          
        }
    }

    public static void shoot(int[] shoot){
        Scanner input = new Scanner(System.in);
      
        System.out.print("Row: ");
        shoot[0] = input.nextInt();
        shoot[0]--;
      
        System.out.print("Column: ");
        shoot[1] = input.nextInt();
        shoot[1]--;
      
    }
  
    public static boolean hit(int[] shoot, int[][] ships){
      
        for(int ship=0 ; ship<ships.length ; ship++){
            if( shoot[0]==ships[ship][0] && shoot[1]==ships[ship][1]){
                System.out.printf("You hit a ship located in (%d,%d) ",shoot[0]+1,shoot[1]+1);
                return true;
            }
        }
        return false;
    }

    public static void hint(int[] shoot, int[][] ships, int attempt){
        int row=0,
            column=0;
      
        for(int line=0 ; line < ships.length ; line++){
            if(ships[line][0]==shoot[0])
                row++;
            if(ships[line][1]==shoot[1])
                column++;
        }
      
        System.out.printf(" Hint %d: Row %d -> %d ships " +
                                 "Column %d -> %d ships ",attempt,shoot[0]+1,row,shoot[1]+1,column);
    }

    public static void changeboard(int[] shoot, int[][] ships, int[][] board){
        if(hit(shoot,ships))
            board[shoot[0]][shoot[1]]=1;
        else
            board[shoot[0]][shoot[1]]=0;
    }
}