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

C++ Exercise in classes Recitation Activity For today\'s recitation activity you

ID: 3804286 • Letter: C

Question

C++ Exercise in classes

Recitation Activity For today's recitation activity you are going to implement three separate classes. Part 1 Battle Ship The first class you will implement is called battleship. The battleShip.h file has been made available on Moodle. You are also given the the beginning of a main file for Battle Ship: Battleship main.cpp. Download these files and place them into the same folder on your computer. Open them with CodeBlocks. You will implement the battleship class and fill out the main file for part 1 of the recitation You will create your class file battleship.cpp. n battleShip.cpp you will define the methods. (Remember: methods define what your object does). How many methods do you see in your header file? It will be your job to fill in these methods so that they work.The constructor always has the same name as your class file, and it never has a data type. Make sure to import the proper class file "battleShip.h" and libraries etc. in the header of your battleship.cpp file. See the BattleShip main.cpp for further instructions. You will need to create 3 instances of your class, set up some of the methods, and expand on the logic of the while loop When you have finished, implementing these methods, show your TA to check off your work. Then move onto the next part Part 2: Planet Class Parts 2 and 3 of this recitation activity have a shared main file Solarsystem main.cpp, provided to you on Moodle The next class you will implement is called planet. This time we have given you the class file planet.cpp It is your job to create the header file for the planet class. Create the file planet.h in CodeBlocks in the same directory as the main file. The planet class has two constructors, a default constructor (which takes no arguments) and a constructor which takes the arguments: string planetName, float planetRadius (the radius of the planet, in km), float planetDist (the distance the planet orbits from the sun, in Astronomical Units) Once you have implemented the header file for the planet class, you will need to modify main to test your class. Use the table of planet data provided to you in the main file to create four planet objects of the Solar System. It will be helpful to use an array for this task, as the code to test your planets assumes an array of planet objects.

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;
    }
}