CS 161 Lab I - Coin Collection
Due Thurs Apr 11 at 11:59pm
Overview
In this lab, you will be writing a simple program to organize, analyze, and display a collection of exotic and interesting coins. You will use a different class for each type of coin, taking advantage of inheritance and polymorphism to easily implement your program.
This lab will be completed in pairs. Be sure to review the pair programming guidelines. You also must work with a different partner than you have before!
Objectives
- To experiment with inheritance and polymorphism
- To learn about File IO for images, and to practice learning new functions from Java documentation.
- To practice and review working with ArrayLists and HashMaps
- To practice with static methods
Necessary Files
You will need to download and extract the BlueJ project from the LabI.zip file. This project will supply you with the following classes:
-
Coin
This class will act as a "base" class for your coins. You are welcome to modify this class, though modification should not be required (remember to use inheritance to avoid changing the parent class!). -
CoinDisplay
You can use this provided class to display an ArrayList of coins (whatever kinds of coins those may be). You only need to instantiate the class using the provided constructor.
images
folder that contains some of the images you will use to display the coins.
Details
In this lab, you will be making a significant number of new classes. These are described below
BEFORE YOU BEGIN: skim through all of the instructions, to get a sense for what you will need to do and what classes you will need. Hint: the first thing you should make is described last!
Other coin classes
The main thing you will need to do is to create classes to represent specific types of coins. Each of these classes should extend the provided Coin
class.
-
You should make classes to represent the following coins:
- A US Coin (in general; this will act as another parent class)
- A US Quarter (this class should extend US Coin. Think about how you can reuse code from the parent class!)
- A US Silver Dollar (this class should also extend US Coin. Think about how you can reuse code from the parent class!)
- A Canadian Dollar
- A Paisa (an Indian coin that is similar to a penny, see here)
- And at least one other exotic coin of your choosing. Feel free to find something interesting! Look at the currency used in other countries (or even other times!)
- You will need to override some of
Coin
's methods: specifically, the Constructor, thedraw()
method, thegetName()
method. - The overridden constructor should specify the
value
of the coin. This value should be in US dollars: so a US Dollar will have a value of 1.0, and a US Quarter will have a value of 0.25. You will need to look up the approximate exchange rates for the other coins--there are lots of website with this information (including Google!). -
The overridden
draw()
method should draw an image of the coin on the provided Graphics object. You can use theGraphics.drawImage()
method to draw aBufferedImage
on a Graphics (note that there is a version of the method that allows you to scale an image). But to do that, you'll need to load an image file (like a .jpg or .png) into a BufferedImage object. You can do this using the staticImageIO.read()
method.- Note that you will need to make a
File
object to pass as a parameter--using code such asnew File("images/us_dollar.jpg")
should do the trick. - The
ImageIO.read()
method throws aIOException
, so you will need to wrap it in a try/catch statement.
- Note that you will need to make a
CoinInspector
You will also need to create a new class called CoinInspector
. This class will contain a number of static methods that will each analyze a given ArrayList of Coins (provided as a parameter):
-
totalValue()
This method should calculate the total value of the Coins in the ArrayList, and return that value. This should be straightforward. -
mostValuable()
This method find and return the "most valuable" Coin in the ArrayList (that is, the coin with the highest value). You can use a basic king-of-the-hill search for this. Note that the values of the coins are doubles, and so can be compared using the normal < and > operators. -
mostCommon()
This method should return the name of the "most common" type of coin in the ArrayList. To figure this out, you will need to go through the list and count the number of times each coin type appears in the list. Then go through those counts to determine which is the most common.- This is a great place to use a
HashMap
! Make a map linking the name of a coin type to the number of coins. Then for every coin you can get the current count, increase it by one, and then put that increased count back in the HashMap. (Hint: remember to check that the coin type already has an entry in the map--if not, you will need to create a new one!) - Once you have all the counts in the HashMap, you can iterate through the keys in the HashMap and do another king-of-the-hill search for which count is the largest. Remember that you can iterate through the keys using
for(Type key : list.keySet())
(be sure and replace Type and list with appropriate values.)
- This is a great place to use a
CoinTester
Finally, you will also need a Tester class to run and test your code. In this tester, you should make a new ArrayList of Coins, add lots of different coins (of all types), and then call the three static methods. You should also use the CoinDisplay to show your coins!
- Note that because each subclass of
Coin
is still aCoin
, they can all be added to a single ArrayList of Coins! This is polymorphism at work!
Make this class first! Then you can use it to test all of your other code.
Submitting
- Once you are sure that your program works, make sure that both your and your partner's names are in a class comment at the top of the file. Then submit your program to the submissions folder.
- Upload your entire BlueJ project folder to the LabI folder on the submission folder on hedwig. Make sure you upload your work to the correct folder! The lab is due at midnight on the day of the lab.
- OH YEAH: please fill out the lab partner evaluation on Moodle.
Grading
This assignment will be graded on approximately the following criteria:
- Creating all of the coins, using proper inheritance and method overriding [40%]
- CoinInspector.totalValue() method [10%]
- CoinInspector.mostValuable() method [10%]
- CoinInspector.mostCommon() method [20%]
- CoinTester class that shows off your coins! [5%]
- You have used good programming style and documentation [10%]
- You completed your lab partner evaluation [5%]