Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a a java project that I had to build from scratch. Its main purpous was to be a battlship program. where a character

I have a a java project that I had to build from scratch. Its main purpous was to be a battlship program. where a character array marked by O's is the board. I was to generate three sets of random numbers to be the computers ships and put them ito an array list. withwich i copared them with an equals to method agains user inputs. My code was fine up until the equals method but then I completely broke it.

import java.util.Random; import java.util.ArrayList; import java.util.Scanner;

public class Location { // attributes private int row; private int column; //constructor public Location(int row, int column) { this.row = row; this.column = column; } //- a copy constructor public Location(Location ship){ this(ship.row, ship.location); } // getter and setter public void setRow(int row){ this.row = row; } public void setColumn(int column){ this.column = column; } public int getRow(){ return row; } public int getColumn(){ return column; } public class Driver { public static void main(String[] args){ { final int All_Ships = 3; // Creation of the gameboard char[][] gameboard = new char [5][5]; char[][] gameboard = {O,O,O,O,O, O,O,O,O,O, O,O,O,O,O, O,O,O,O,O, O,O,O,O,O,}; } { // To print the game board columns by row for(char c=0; c

ArrayLists loationsList = new ArrayLists <>(); // Creating the Random number generator Random rand = new Random(); /* To make it generate random numbers 3 times between 0-4 and assign it to a location (r,c) and append it to the arraylist locationList*/ Loction ship = new Location(r,c); int row = rand.nextInt(5); int column = rand.nextInt(5); locationsList.add(ship); Loction ship = new Location(r,c); int row = rand.nextInt(5); int column = rand.nextInt(5); locationsList.add(ship); Loction ship = new Location(r,c); int row = rand.nextInt(5); int column = rand.nextInt(5); locationsList.add(ship); /* make the user guess where the ships are by asking for a row and column. tell them how many attempts(4) are left, how many ships are left, and then let them guess again.*/ Scanner sc = new Scanner(System.in);

int turnstaken = t; int shipsleft = locationsList.size(); for (i = 0; i < 4; t++) { int row = sc.nextInt(); int column = sc.nextInt(); System.out.println("Turn" + t + "of 4"); System.out.println("Guess a row:" + row); System.out.println("Guess a column:" + column);

// creating an equals too method for co-ordinate comparison { for(char c=0; c

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

Principles Of Database Systems With Internet And Java Applications

Authors: Greg Riccardi

1st Edition

020161247X, 978-0201612479

More Books

Students also viewed these Databases questions