Surname 1
1
Declare an array of Customers in main. Write a static method (call
from main) that returns an array of Customer objects. In this
method, read how many Customers from the user (prompt first),
allocate memory for the array, then in a for loop, prompt and read
a name, account number and savings balance, which are then
passed to the Customer constructor when instantiating and
assigning to an array element. .
2
Modify the Customer class (as given in the answer to Exercise
5.3) to have two StringBuilders, one for first name, one for last
name (instead of one String for the name). Change the accessor
that returns one String for the name, but return the name in the
form "last, first". Change the mutator (setName) so it has one
String for the name, and parses the parameter into two parts,
assuming the form "last, first" (with one space after the comma).
Assign the part before the comma to the StringBuilder for the
lastName, and the rest after the ", " to the StringBuilder for the
firstName. DON'T instantiate new StringBuilders in this method
(nor in the constructor)! Use the declarations here:
public class Customer {
private StringBuilder lastName= new StringBuilder();
private StringBuilder firstName= new StringBuilder();
// the rest of the declarations are the same as before, except no
String var.
// methods to change:
public String getName(){ // change this
Surname 2
}
public boolean setName(String name){
// you change this as specified above
}
3
Redo the setName method you wrote in Exercise 2 that used the
StringBuilder for the last name and first name in the Customer
class to use either a StringTokenizer of split method in the
setName instance method (if you used one of these before, use
the other for this). Only submit the setName method.
4
You will be re-doing Prog. HW#2 in an object-oriented way, BUT
IF THERE WERE ERRORS in HW#2, it is the STUDENT'S
RESPONSIBILITY to FIX ANY ERRORS that are in HW#2 in
HW#3 or POINTS WILL BE DEDUCTED IN HW#2 AND HW#3 for
the same errors. If you submit HW#2 on time, you will get the
grade & feedback for it before HW#3 is due. However, even if the
student didn't get the grading feedback yet for HW#2 (for
example, if the student submits HW#3 early before HW#2 was
graded, OR if the student submits HW#2 14 days late, then
submits HW#3 the next day without waiting for feedback), any of
the same errors that are in HW#2 and HW#3 will STILL BE
DEDUCTED in both!
Write a class called BattleshipBoard in which you declare as a
private instance variable, a 2-dim. array of ints (I'm calling it
shipGrid), a private instance variable for another 2-dim. array of
ints (I'm calling hitGrid), and a private instance variable for the
Surname 3
number of ships (int). In the constructor OR declaration, allocate
memory so the dimensions of the 2-dim. arrays are 10 X
10. Include the following constructors or instance methods (NO
static METHODS HERE):
1 a constructor (int parameter) that allocates memory for the
shipGrid AND hitGrid (if not done in the declaration), AND
assigns to the int parameter to the number of ships instance
variable IF the parameter is 4 to 9 (inclusive), but if it isn't,
assign 4 (default)
2 default constructor (no parameters) that calls the 1st
constructor passing 4 (should be a public static final int)
3 public accessor method that has NO PARAMETERS and
returns the number of ships (int instance variable value)
4 public void method that has NO PARAMETERS that does
ALMOST the same thing as the method you wrote in HW#2
to set all the elements of the hitGrid to 0, but assigns
numbers to the instance variable hitGrid 's elements (NOT to
a parameter)
5 public void method that has NO parameters that does
ALMOST the same thing as fillShipGrid() given in HW#2,
but instead assigns to this object's shipGrid 'selements AND
uses the number of ships instance variable (NOT a
parameter); have this method call a PRIVATE INSTANCE
method that does the same as placedShip() (has different
parameters, see F here...)
6 PRIVATE INSTANCE boolean method that does the same
as placedShip() given in HW#2, except there is NO 2-dim.
array parameter, you will use the private instance variable
shipGrid instead
7 public void method that has NO PARAMETERS that calls
the instance method that resets the hitGrid to all 0s (method
4.) and also call the fillShipGrid instance method (method
5.)
8 public int method that combines the checkGrid() and
setHitGrid() methods from HW#2, but has ONLY 2 int
Surname 4
9
parameters (one for a row number I'm calling rowNum and
one for a column number I'm calling colNum), and operates
on the shipGrid and hitGrid private instance variables (NOT
parameters). Similar to checkGrid and setHitGrid in HW#2,
have the method:
◦ check if valid rowNum (0 - 9) and colNum (0 – 9). If not,
return –1
◦ check if the element in hitGrid at the rowNum & colNum
isn't 0, return -2 (already chosen)
◦ check if the element in shipGrid at the rowNum &
colNum is 0, and if so, set the hitGrid at the same
rowNum & colNum to -1, then return 0
◦ check if the element in shipGrid at the rowNum &
colNum is > 0, and if so...
▪ add 10 to the element value and assign it to the
hitGrid element at the same rowNum & colNum.
For example, if the element in shipGrid at the
rowNum & colNum 3, assign 13 to the hitGrid's
corresponding element.
▪ check if there are the same number of elements
as the element value in the same row, or same
column (for example, if you assigned 13, check for
3 elements with 13 in the same row or same
column). (you may assume the same number will
always be contiguous in a row or column). If so,
▪ change the hitGrid elements (for example
13's) to the element %10 (for example 3)
▪ change the shipGrid elements at the same
locations to 0 (ship was sunk, remove from
shipGrid)
▪ return the ship number (for example 3) if
sunk, return ship number + 10 if not sunk
PRIVATE void method to do the same as displayGrid in
HW#2, so it ONLY has a 2-dim. array of ints parameter and
displays as specified in HW#2
Surname 5
10 public void method (NO PARAMETERS) to display ONLY
the hitGrid (calling method 9., passing the hitGrid)
11 public void method to do similar as in HW#2's
displayResults method, except there are NO
PARAMETERS (you will operate ONLY on the private
instance variables, should call method 9. 2 times)
Write another class called BattleshipGame that has a private
instance variable (I'm calling it battleshipBoard) that's a
BattleshipBoard, and 2 private int instance variables (one for the
number of ships I'm calling numShips, and another for the
maximum # of guesses allowed I'm calling maxGuesses). You
may instantiate the BattleshipBoard, initialize the numShips to 5
and maxGuesses to 25 in their declaration or in a default
constructor (your choice). Include public instance methods
displayInstructions (NO PARAMETERS, but displays numShips
and numGuesses instance variables' values in the appropriate
places) and playBattleship (also a public instance method)
similar to HW#2, except there are NO PARAMETERS, so the
method will call the battleshipBoard's reset() (method 7., call here
instead of main), displayHitGrid(), setHit(), and displayResults()
methods where appropriate (make sure the logic is pretty much
the same as in HW#2, and you're allowed to use the
HW2_Methods' wantsToContinue() method)!
Write driver class for main, call it Prog3 that is in a separate file
and class than BattleshipBoard and BattleshipGame. Declare
inside main a BattleshipGame object variable and instantiate it
BEFORE ANY LOOP! Then in main:
• call the BattleshipGame's displayInstructions() method
• **in a LOOP:
◦ call the BattleshipGame's playBattleship() method
◦ call the wantsToContinue() method (asking if the user
wants to play another)
◦ if the return value is true, repeat to **
Surname 6
DO NOT USE ANY CLASS-SCOPE VARIABLES in the DRIVER
CLASS (except a static final Scanner for user input) FOR the
Prog3 class!
Turn in with similar input specifications as Prog. HW#2: a run with
at least 3 games played in which one you quit early, one you
guess all the ships, and one you reach 25 guesses without
sinking all the ships. The test runs are the same as in HW#2.
Purchase answer to see full
attachment