Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can anyone help me to write a java program. One of your experts answered in C++. But I don't need in C++. I need it

Can anyone help me to write a java program. One of your experts answered in C++. But I don't need in C++. I need it in JAVA please. Write a Java program which will accept an input file as a command line argument. This text file contains the following information for a battleship configuration: Size of the square grid as an int Number of battleships and sizes in grids (Battleships can have a size from 2 to 6.) Battleship grid where 0s are blank spaces and 1s are battleships (The battleships can only go up and down or side to side, not diagonal. The battleships will also not touch or overlap) A sample input file would contain look like this: Text Box: It is an 8 x 8 grid There are 4 battleships of sizes 2, 3, 4, and 5. You can see the ships in the grid. 8 4 2 3 4 5 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 The program will take in this file and pass the file to a method called: checkGrid. This method will then read the file and determine if the grid given matches the specifications of the grid. (For example, are there 8 rows and 8 columns, are there 4 battleships, is each ship the correct length). If this is the case, checkGrid will return a boolean true value. You can then output, The specifications for the battleship are correct from the main method. If not, the checkGrid method will return a Boolean false output. You again can output from the main method, The specifications for the battleship are incorrect. Here is the specification for checkGrid: public static boolean checkGrid(File input) { //Your code here } To check the grid, you need to place the grid into a two-dimensional array. Here is the specification of the method you will use to create the two-dimensional array: public static int[][] generateGrid(Scanner in, int gridSize) { //Your code here } This method will be passed the scanner object created in checkGrid and then read the grid into a two-dimensional array. It will then return this array back to the checkGrid method. Once the two-dimensional array has been created, it can then be checked to see if the ships are present in the array and that the array contains only 0s and 1s. If the ships are present and the array only contains valid data, 0s and 1s, then the checkGrid method will return true. It will return false if this is not the case. The program should be able to handle problems with input. These problems include, a missing input file and an incorrect input file name. Utilize exception handling to handle these problems and continue the execution of the program. The input file called project6GoodData has the above data without any data errors. Each entry is separated by a space or multiple spaces. The input file called project6BadData has errors in the data. Some numbers are actually characters instead. For extra credit, you can handle these errors as well and simply ignore any lines with incorrect data. For more about battleship, please see: https://en.wikipedia.org/wiki/Battleship_(game). *************************************************************************************************** project6BadData.txt 8 4 e 3 4 5 0 0 0 0 0 0 0 0 1 1 g 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 ***************************************************************************************************** project6Data.txt 8 4 2 3 4 5 0 0 0 0 0 0 0 0 1 1 1 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0

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

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions