CS 200 - Assignment 7

Due: Tuesday, December 11th by 11:59 PM

Updated: Dec 10th (additional extra credit option)

Java Digit Classes

In this class you will implement classes that represent digits (numbers between 0 and 9) that can be drawn as a block of characters of a specified height and width.

Note: it is sufficient for your program to handle only the digits 0 through 3.

Each digit class you implement will be a subclass of an abstract base class called Digit.

Getting Started

Download CS200_Assign7.zip.

In Eclipse, choose File->Import....  Choose General->Existing Projects into Workspace, then click Next.  Choose Select archive file, click the Browse button, and select the zip file (CS200_Assign7.zip).  Click Finish.

Your Task

Your main task is to implement the createDigit method of the DigitFactory class.  This method takes three parameters:

n - the digit to create (value will be in the range 0..9)

height - the height of the digit to create

width - the width of the digit to create

The createDigit method returns an instance of the Digit class (really, an instance of a subclass of Digit) which, when its draw method is called, will print out the a digit,  (See the example runs in Testing below.)

The draw method initially looks like this:

public Digit createDigit(int n, int height, int width) {
throw new UnsupportedOperationException("TODO - implement");
}

The statement that reads throw new UnsupportedOperationException... terminates the program with an error message.

Your job is to change the createDigit method so it looks something like this:

public Digit createDigit(int n, int height, int width) {
switch (n) {
case 0: return new Zero(height, width);
case 1: return new One(height, width);
case 2: return new Two(height, width);
case 3: return new Three(height, width);
}

throw new UnsupportedOperationException("TODO - implement");
}

This implementation of the method can create Digit objects representing the values 0 through 3.  It makes use of three subclasses of Digit called Zero, One, Two, and Three.  (You will need to define these classes.)

For this assignment, it is sufficient for createDigit to handle the values 0, 1, 2, and 3.

Testing

In the Package Explorer, right-click on DigitPrinter.java, and choose Run As->Java Application.

Example run (user input in bold red):

How large should the digits be (height and width)?
7 5
Now enter some digits. (Enter a negative number to exit.)
3 0 1
*****
*
*
*****
*
*
*****

*****
* *
* *
* *
* *
* *
*****

*
*
*
*
*
*
*

-1
Bye!

Example run (user input in bold red):

How large should the digits be (height and width)?
9 11
Now enter some digits. (Enter a negative number to exit.)
2 2 0
***********
*
*
*
***********
*
*
*
***********

***********
*
*
*
***********
*
*
*
***********

***********
* *
* *
* *
* *
* *
* *
* *
***********

-1
Bye!

Hints

The constructors of the Digit subclasses will need to use the super keyword to invoke the base class Digit's constructor.

The subclasses of Digit can call the getHeight and getWidth methods to find out the height and width of the digit.

Extra Credit

You will receive extra credit if your program can handle all of the digits 0 through 9.

Updated: Dec 10th

You can receive additional extra credit by implementing a class called MultiDigitNumber.  It should work as follows:

Change the createDigit method in DigitFactory so that

new MultiDigitNumber(n, height, width)

is returned whenever n >= 9.

When a MultiDigitNumber's draw method is called, it displays each digit of the number, in order.  E.g., the MultiDigitNumber object created when n=220 should draw the digits 2, 2, 0.

You will need to modify the code in DigitPrinter's main method so that createDigit is called to create Digit objects for values greater than 9.

Submitting

Export your program to a zip file:

Right-click on the CS200_Assign7 project in the package explorer.  Choose Export...

Choose General->Archive File.  Click Next.

Make sure the checkbox next to CS200_Assign7 is checked.

Click Browse to choose the directory and filename of the zipfile.  I suggest calling the zip file CS200_Assign7_Solution.zip

Click Finish to save the project in the zip file.

Upload the saved zip file to the Marmoset server as Project 7:

https://camel.ycp.edu:8443/