Question

Create the ArrayList program example in listing 13.1, Battlepoint. Describe the code (in your WORD document)...

Create the ArrayList program example in listing 13.1, Battlepoint.

  1. Describe the code (in your WORD document) in the 'if' statement
    • if (targets.indexOf(shot) > -1) {
  2. ADD a NEW ArrayList of points called 'misses'
  3. ADD code to add a point to the 'misses' list on a miss (if a SHOT does NOT hit a TARGET)
  4. ADD code to place an 'M' in the final output map for all shots that were MISSES.
  5. ADD code to place an H on the target for any shots that HIT a target.
  6. SUBMIT your new Battlepoint code (no input arguments).

package com.java24hours;

import java.awt.*;
import java.util.*;
public class Battlepoint {
ArrayList<Point> targets = new ArrayList<Point>();
public Battlepoint() {
// create targets to shoot at
createTargets();
// display the game map
showMap();
// shoot at three points
shoot(7,4);
shoot(3,3);
shoot(9,2);
// display the map again
showMap();
}

private void showMap() {
System.out.println("\n 1 2 3 4 5 6 7 8 9");
for (int column = 1; column < 10; column++) {
for (int row = 1; row < 10; row++) {
if (row == 1) {
System.out.print(column + " ");
}
System.out.print(" ");
Point cell = new Point(row, column);
if (targets.indexOf(cell) > -1) {
// a target is at this position
System.out.print("X");
} else {
// no target is here
System.out.print(".");
}
System.out.print(" ");
}
System.out.println();
}
System.out.println();
}

private void createTargets() {
Point p1 = new Point(5,9);
targets.add(p1);
Point p2 = new Point(4,5);
targets.add(p2);
Point p3 = new Point(9,2);
targets.add(p3);
}

private void shoot(int x, int y) {
Point shot = new Point(x,y);
System.out.print("Firing at (" + x + "," + y + ") ... ");
if (targets.indexOf(shot) > -1) {
System.out.println("you sank my battlepoint!");
// delete the destroyed target
targets.remove(shot);
} else {
System.out.println("miss.");
}
}

public static void main(String[] arguments) {
new Battlepoint();
}
}

Homework Answers

Answer #1

/************* BattlePoint.java ***************/

import java.awt.Point;
import java.util.ArrayList;

public class BattlePoint {
   ArrayList<Point> targets = new ArrayList<>();
   ArrayList<Point> misses = new ArrayList<>();
   Point hit = null;
   boolean after = false;

   public BattlePoint() {
       createTargets();
       showMap();
       shoot(7, 4);
       shoot(3, 3);
       shoot(9, 2);
       after = true;
       showMap();
   }

   private void showMap() {
       System.out.println("\n 1 2 3 4 5 6 7 8 9");
       for (int column = 1; column < 10; column++) {
           for (int row = 1; row < 10; row++) {
               if (row == 1) {
                   System.out.print(column + " ");
               }
               System.out.print(" ");
               Point cell = new Point(row, column);
               if (targets.indexOf(cell) > -1) {
                   System.out.print(String.format("%s", after ? "M" : "X"));
               } else if (after && hit != null && hit.x == row && hit.y == column) {
                   System.out.print("H");
               } else {
                   System.out.print(".");
               }
               System.out.print(" ");
           }
           System.out.println();
       }
       System.out.println();
   }

   private void createTargets() {
       Point p1 = new Point(5, 9);
       targets.add(p1);
       Point p2 = new Point(4, 5);
       targets.add(p2);
       Point p3 = new Point(9, 2);
       targets.add(p3);
   }

   private void shoot(int x, int y) {
       Point shot = new Point(x, y);
       System.out.print("Firing at (" + x + "," + y + ") ... ");
       if (targets.indexOf(shot) > -1) {
           System.out.println("you sank my battlepoint!");
           targets.remove(shot);
           hit = shot;
       } else {
           misses.add(shot);
           System.out.println("miss.");
       }
   }

   public static void main(String[] arguments) {
       new BattlePoint();
   }
}

/********** Output ***********/

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
How do I get this portion of my List Iterator code to work? This is a...
How do I get this portion of my List Iterator code to work? This is a portion of the code in the AddressBookApp that I need to work. Currently it stops at Person not found and if it makes it to the else it gives me this Exception in thread "main" java.lang.NullPointerException    at AddressBookApp.main(AddressBookApp.java:36) iter.reset(); Person findPerson = iter.findLastname("Duck"); if (findPerson == null) System.out.println("Person not found."); else findPerson.displayEntry(); findPerson = iter.findLastname("Duck"); if (findPerson == null) { System.out.println("Person not found.");...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
Question: I am using three different ways to execute this program. I am a bit confused...
Question: I am using three different ways to execute this program. I am a bit confused on the time complexity for each one. Can someone explain the time complexity for each function in relation to the nanoseconds. import java.util.HashMap; import java.util.Map; public class twosums {       public static void main(String args[]) {               int[] numbers = new int[] {2,3,9,4,8};;        int target = 10;               long nano_begin1 = System.nanoTime();        int[]...
I cannot for the life of me get this program to run properly. I don't know...
I cannot for the life of me get this program to run properly. I don't know what I'm doing wrong. Could the format of my text files be the issue? Edit: The program works this way, you choose a year and a gender and then enter a name. When you press the button its should give you the ranking or popularity of the name. There are 5 files that I have to search through, named 2006.txt to 2010.txt. First the...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...