9A
Write a class BookstoreArray
which manages Book
objects. This is similar to a problem in the last homework but using arrays rather than ArrayLists.
The Book
class is provided for you. It has a constructor that takes the title of book and its price as parameters. It has methods to getPrice
and getTitle
. Copy the Book
class into your project. Do not change it in any way. This is the same class you used in the previous lesson.
A BookstoreArray
has an instance variable Book[]
. Call the instance variable books. The constructor takes and array of Book
objects as a parameter.
Provide these methods
public double average()
Gets the average cost of all the books in the BookstoreArray
. If the array is empty, return 0;public void swap(int index1, int index2)
- swaps the element at index1 with the element at index2. If either index is out of bounds, do not changing anything.public String cheapest()
gets the title for the Book
with the lowest price. If more than one Book
has the same price, return the title of the first. If the BookstoreArray
object is empty, return the empty stringpublic String toString()
gets a string representation of the Books
in the BookstoreArray
- provided. (This is why the instance variable must be called books.)Provide Javadoc.
For the draft, define the instance variable and implement the constructor. Implement the average method.
9B
In this problem you will complete methods in the IntArray
class to process integer arrays
The constructor for IntArray
take an int array as a parameter.
IntArray
has these methods:
public int sum()
gets the sum of the integers in the arraypublic String displaySum
() which returns a String listing the integers in the array separated by a + followed by an = and then the sum. Do not recalculate the sum. Call the sum() method.For the draft, implement the constructor and the sum method
9C
In this problem you will create a class TicTacToeGame
to model a tic-tac-toe game board. The board is represented by a 3 X 3 int array. Rather than X's and O's the players' positions are represented by integer constants
public static final int NONE = 0; //indicates a free space
public static final int PLAYER_1 = 1; //indicates a space marked by player 1
public static final int PLAYER_2 = 2; //indicates a space marked by player 2
You will implement methods to process the array. You will not implement the whole game play.
The constructor takes a 2-d array as a parameter. The array contains integers indicating which spaces are marked by which player. The game may be complete or only partially complete
Provide these method that process the array board
public int winner()
returns the winner of the game either NONE, PLAYER_1,or PLAYER_2. If a row, column or diagonal contains three NONE's, return -1. There is a problem.public String getBoard()
returns a string representation of the game board. The string will contain 3 lines each with 3 numbers followed by a space. See the output below.public int[] counts()
returns an int array where array[0] is the number of unused spaces, array[1] is the number of moves made by player 1, and array[2] is the number of moves made by player 2.Output for getBoard
1 2 1
2 1 2
2 0 1
1 1 2
2 2 1
1 2 1
1 0 2
0 1 1
2 2 2
Provide Javadoc
For the draft, implement the constructor and the getBoard method
I just realized we have not covered 2d-arrays so I will give you the pseudocode for getBoard. With that and your reading, you should be able to do this. If not, you know where to get help (updated April 10)
initialize a String board = "";
for each row (there are 3)
for each column (there are 3)
board = board + game[row][column] + " " (add the next character to the string board)
board = board + "\n" (start a new row)
}
return board;
}