Account
which represents a bank account and
create two instances of this class, checking
and
saving
, which, presumably, belong to the same
person. Each account object contains its own balance. However, they
also share a static variable total
which keeps track of
the total balance of the two accounts combined. The class has the
following methods:
toString
prints the balance of the account and the
total balance of both accounts, for instance if the checking has 200
and the saving has 500, checking.toString() will return:
balance = 200 total available = 700
deposit(double amount)
deposits the specified amount
into the account. Any positive amount can be deposited.
withdraw(double amount)
withdraws the amount from the
account. If the amount is larger than the balance of the account, the
withdrawal is allowed to proceed if the two accounts combined have
sufficient balance. In this case the account from which the money is
withdrawn goes into negative, and the total is reduced
respectively. For instance, if the checking has 200 and the saving has
500, it's OK to withdraw 300 from the checking, and the result will
be:
balance = -100 total available = 400
(the saving account still has 500)Insufficient
funds
is printed, and the accounts retain their balances.
Your task will be to implement the behavior of these three pieces given the following abstract class:
abstract class ChessPiece {
protected String name;
protected char color; // black or white piece
protected int row;
protected char col;
public ChessPiece(String n, char c, char cl, int r) {
name = n;
color = c;
row = r;
col = cl;
}
public String toString() {
return color + " " + name + " at " + col + row;
}
abstract public boolean moveTo(char c, int r);
// a helper method for converting columns into numbers
//Change as of 3/26: int -> char below
protected int toNumber(char c) {
switch(c) {
case 'a':
return 1;
case 'b':
return 2;
case 'c':
return 3;
case 'd':
return 4;
case 'e':
return 5;
case 'f':
return 6;
case 'g':
return 7;
case 'h':
return 8;
default:
System.out.println("Illegal position " + c);
return -1;
}
}
}
You cannot modify the abstract class.
You need to write the classes King, Rook, and Pawn. Each class needs a constructor and a method moveTo(). The method takes the new position of the piece. It checks whether the move is legal (based on the specific rules of how this piece moves and on the current position). If the move is legal, then the piece is moved and the method returns 'true', otherwise the method returns 'false' and the position is unchanged.
Below is the sample of a test program for a King:
King whiteking = new King('W','e',4);
King blackking = new King('B','a',8);
if (!whiteking.moveTo('d',4)) {
System.out.println("can't move the king to d4");
}
System.out.println(whiteking);
if (!blackking.moveTo('b',7)) {
System.out.println("can't move the king to b7");
}
System.out.println(blackking);
if (!blackking.moveTo('b',7)) {
System.out.println("can't move the king to b7");
}
if (!blackking.moveTo('b',5)) {
System.out.println("can't move the king to b5");
}
The white king is initially at e4, the black one is at a8.
The first two moves are legal, so the white king gets moved to d4,
and the black king to b7. The next move is illegal because the king
remains at the same position (an error message gets printed). The last
move is illegal because b5 is not next to d4, and another error
message gets printed.
Your task is:
Then implement the solution of this chess problem (follow the link). When a piece is taken, remove it from the vector. Print out all the positions after each move. Ignore the promotion of the pawn to a queen in the third move.
Part 1 Your task is to implement a class CompColor which is an extension of the class Color and also implements the interface Comparable. The interface requires a single method:
int compareTo(Object o)
The object is assumed to be another color. Write a method that will
return -1 if the sum of the rgb components of this color is greater
than that of the parameter (i.e. this color is lighter), 0 if the two
sums are equal, and 1 if this color is darker.
You also need to write the following constructor for CompColor:
public CompColor(Color c)
Since there is no constructor of the class Color that takes another
color, use the methods getRed(), getGreen(), and getBlue() to get the
individual color components and then pass them to the constructor
Color(int r, int g, int b)
Now you can create CompColor from color constants:
CompColor c = new CompColor(Color.orange);
Part 2 Write main to test the CompColor class. Then write two methods for finding the minimum and the maximum of the array of Comparable (use the compareTo() method in max and min):
public static Comparable min(Comparable [] c)
public static Comparable max(Comparable [] c)
Create an array of 5 color constants of your choice and use the max
and min methods to find the darkest and the lightest color among these
constants. To make sure that the two methods are not specific to
colors, test them on an array of 7 objects of class Integer
(Integer implements the interface Comparable, so it has compareTo
method).
public class PS8Q4 {
public static void main(String [] args) {
Y y1 = new Y(2);
Y y2 = new Y(3);
y1.z();
y2.z(2);
X x = new X(1);
x.z(1);
System.out.println(y1);
System.out.println(y2);
System.out.println(x);
}
}
class X {
private int x;
// constructor
public X(int x) {
this.x = x;
}
public int getX() {
return x;
}
public String toString () {
return "X: x = " + x;
}
public void z (double x) {
this.x += (int) x;
}
}
class Y extends X {
protected static int y;
public Y(int n) {
super(n);
this.y += getX();
}
public String toString () {
return "Y: x = " + getX() + " y = " + y;
}
public void z() {
y--;
}
}