## CSci 1211 Problem set 8. Due Monday, March 29th at 5pm

### Problem 1 (10 points)

Write a class `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)
If the requested amount is larger than the total of the two accounts combined, then the transaction is refused, the message ```Insufficient funds``` is printed, and the accounts retain their balances.
Write a test-driver class for the class Account, write enough test examples to test all features.

### Problem 2 (10 points)

This problem deals with a (subset) of the game of chess. this web site has all the information about chess needed for this problem (and much more!). What you need to know is that chess is played on 8-by-8 board with rows labelled from 1 to 8, and columns labelled from a to h. You also need to know how the following chess pieces move:

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.

• To implement the three classes (note that the behavior of a pawn depends on the color, since it can't move backwards). You may ignore: castling for the king, promotion to a queen for a pawn, blocking of one piece by another of the same color (even if a piece is blocked, the move should still be considered legal).
• To write a program to test the behavior (in particular, make sure that the program disallows illegal moves).
• Write another test program in which you create a vector of ChessPieces that represents this configuration. Print out the positions of all pieces (in a loop, using the toString() method of ChessPiece).

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.

### Problem 3 (15 points)

In this problem you are asked to create a class that allows you to compare colors based, roughly, on their level of lightness/darkness. Colors are stored using rgb (red, green, blue) encoding, the same is encoding of colors in a color TV. Each color component has a value between 0 and 255. The higher the level, the more of this color is present. Pure red is r=255, g=0, b=0. However, higher levels of colors also mean a lighter color: white is r=255, g=255, b=255, black is r=0, g=0, b=0. You can experiment with colors on this page.

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).

### Problem 4 (5 points)

Draw the object diagram for the following program. Show what will be printed in the end. As usual, it's OK to run the program.
``````
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--;
}
}
``````

This page is a part of the course CSci 1211 at UMM.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.