Some of the exercises below provide sample runs to better clarify what the program in question is supposed to do. In these sample runs, text given in green has been typed by the user, while white text has been output by the program. Additionally, the "$
" symbol indicates the command prompt, while the "↵" symbol indicates the user has pressed the return key.
Exercises identified with a breadboard logo (shown at left) require the following library:
breadboards.jar.
Create a class named Matrix.java
whose objects will behave similar to matrices. In particular, the class should have...
toString()
method that creates a string representation of the matrix, with its column elements separated by tabs and its row elements separated by "\n"
.numRows()
and numCols()
which return the number or rows and columns of the matrix, respectively.getElement(r, c)
that returns the element in the r
_th row and the c
_th column of the matrix.setElement(r, c, value)
that sets the value of the element in the r
_th row and the c
_th column to value
.sum(a,b)
that creates a new matrix equal to the sum of matrices a and b, and that can be invoked using the class name (e.g. Matrix c = Matrix.sum(a,b);
)product(a,b)
that creates a new matrix equal to the product of matrices a and b, that can be invoked using the class name (e.g. Matrix c = Matrix.product(a,b);
subMatrix(deletedRow, deletedCol)
that creates a new (smaller) matrix from the existing one by deleting the specified row and column. If there is no such row or column, or if the matrix only has one row or column, null
should be returned.determinant()
that uses "expansion by minors" to compute and return the determinant of a matrix. If the number of rows is not equal to the number of columns for the matrix in question, this method should return null.You can use the following class and sample run to check your work:
public class MatrixTest{ public static void main(String[] args) { double[][] arrayForMatrixA = {{3,2,1}, {0,2,-5}, {-2,1,4}}; double[][] arrayForMatrixB = {{3,0,1}, {6,2,-1}, {5,4,-2}}; double[][] arrayForMatrixC = {{-4,2,1,5}, {3,1,-1,9}, {1,-4,8,4}}; Matrix a = new Matrix(arrayForMatrixA); Matrix b = new Matrix(arrayForMatrixB); Matrix c = new Matrix(arrayForMatrixC); Matrix d = new Matrix(3,2); System.out.println("matrix a:"); System.out.println(a.toString()); System.out.println("matrix b:"); System.out.println(b.toString()); System.out.println("num rows in a = " + a.numRows()); System.out.println("num cols in b = " + b.numCols()); System.out.println("element in c at (2,3) = " + c.getElement(2,3) + "\n"); d.setElement(2, 0, 5); System.out.println("matrix d with 5 in position (2,0):"); System.out.println(d.toString()); System.out.println("matrix sum a + b:"); System.out.println(Matrix.sum(a, b)); System.out.println("matrix product a * b:"); System.out.println(Matrix.product(a, b)); System.out.println("submatrix of a with 1st row, 1st column deleted:"); System.out.println(a.subMatrix(1,1)); System.out.println("matrix c:"); System.out.println(c.toString()); System.out.println("submatrix of c with 0th row, 2nd column deleted:"); System.out.println(c.subMatrix(0,2)); System.out.println("determinant of matrix a:"); System.out.println(a.determinant()); } }
$ java MatrixTest↵
matrix a:
3.0 2.0 1.0
0.0 2.0 -5.0
-2.0 1.0 4.0
matrix b:
3.0 0.0 1.0
6.0 2.0 -1.0
5.0 4.0 -2.0
num rows in a = 3
num cols in b = 3
element in c at (2,3) = 4.0
matrix d with 5 in position (2,0):
0.0 0.0
0.0 0.0
5.0 0.0
matrix sum a + b:
6.0 2.0 2.0
6.0 4.0 -6.0
3.0 5.0 2.0
matrix product a * b:
26.0 8.0 -1.0
-13.0 -16.0 8.0
20.0 18.0 -11.0
submatrix of a with 1st row, 1st column deleted:
3.0 1.0
-2.0 4.0
matrix c:
-4.0 2.0 1.0 5.0
3.0 1.0 -1.0 9.0
1.0 -4.0 8.0 4.0
submatrix of c with 0th row, 2nd column deleted:
3.0 1.0 9.0
1.0 -4.0 4.0
determinant of matrix a:
63.0
Write classes named Card
and Deck
with all of the methods necessary to do the following:
public class Card {
public static final int SPADES = 0;
public static final int HEARTS = 1;
public static final int CLUBS = 2;
public static final int DIAMONDS = 3;
public static final int ACE = 1;
public static final int JACK = 11;
public static final int QUEEN = 12;
public static final int KING = 13;
private int suit;
private int rank;
public Card(int rank, int suit) {
this.rank = rank;
this.suit = suit;
}
public String toString() {
String s = "";
switch (this.rank) {
case ACE : s += "Ace"; break;
case JACK : s += "Jack"; break;
case QUEEN : s += "Queen"; break;
case KING : s += "King"; break;
default : s += this.rank;
}
s += " of ";
switch (this.suit) {
case SPADES : s += "Spades"; break;
case HEARTS : s += "Hearts"; break;
case CLUBS : s += "Clubs"; break;
case DIAMONDS : s += "Diamonds"; break;
}
return s;
}
public boolean equals(Object o) {
Card c = (Card) o;
return ((this.rank == c.rank) && (this.suit == c.suit));
}
public int rank() {
return this.rank;
}
public int suit() {
return this.suit;
}
}
import java.util.ArrayList;
import java.util.Random;
public class Deck {
public static final int[] SUIT_ORDER =
{Card.SPADES, Card.HEARTS, Card.CLUBS, Card.DIAMONDS};
public static final int[] RANK_ORDER =
{Card.ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10,
Card.JACK, Card.QUEEN, Card.KING};
public static final int FULL_DECK_SIZE = 52;
private ArrayList cards;
public Deck() {
cards = new ArrayList();
for (int i = 0; i < FULL_DECK_SIZE; i++) {
int suit = SUIT_ORDER[i / 13];
int rank = RANK_ORDER[i % 13];
Card card = new Card(rank, suit);
cards.add(card);
}
}
public String toString() {
String s = "";
for (int i = 0; i < cards.size(); i++) {
s += cards.get(i).toString() + "\n";
}
return s;
}
public boolean equals(Object o) {
Deck d = (Deck) o;
if (this.numCards() != d.numCards())
return false;
for (int i = 0; i < this.numCards(); i++) {
if (! this.get(i).equals(d.get(i)))
return false;
}
return true;
}
public int numCards() {
return cards.size();
}
public Card get(int pos) {
return cards.get(pos);
}
public void add(Card card) {
cards.add(card);
}
public void remove(Card card) {
cards.remove(card);
}
public Card remove(int pos) {
return cards.remove(pos);
}
public void shuffle() {
final int NUM_SWAPS = 1000;
Random random = new Random();
for (int i = 0; i < NUM_SWAPS; i++) {
int pos = random.nextInt(cards.size());
Card card = cards.remove(pos);
cards.add(card);
}
}
public Card draw() {
Card topCard = cards.remove(0);
return topCard;
}
}
Write a class named CollidingBallsAnimator
that extends the Breadboard
class of the breadboards package. Upon entering some number of balls in the text field at the bottom of the breadboard window, and clicking the "Go" button, the same number of balls with randomly chosen initial positions, velocities, sizes, and colors will bounce perpetually off the sides of the central area of the window and each other. Clicking the "Stop" button should remove all of the balls from the screen.
For collisions between balls (of potentially different sizes), the physics of the situation should be as accurate as possible -- taking into account a mass for each ball proportional to the square of its radius.