CMPS 200 Homework Assignment 12, Fall 2011
American University of Beirut · Introduction to Programming · 2 pages
More CMPS 200 files
- Homework 11 · Fall 2018HomeworkView
- Homework Last Assignment · Fall 2018HomeworkView
- Homework Asst10 · Fall 2017HomeworkView
- Homework 9 · Spring 2017HomeworkView
- Homework Assignment 2 · Fall 2016HomeworkView
- Homework Assignment 5 · Fall 2016HomeworkView
- Homework Assignment 6 · Fall 2016HomeworkView
- Homework 1 · Spring 2016HomeworkView
Show the text of this homework
Faculty of Arts & Sciences
Department of Computer Science
CMPS 200—Introduction to Programming
Assignment 12 – Due Mon Jan 2, 2012
Reading Material:
Chapter 8
In addition, p. 286-290 show an example describing the use of the throw construct for raising
exceptions. P. 184, 287, 351, and 463 show examples of Exceptions thrown in various contexts.
1. Fraction
The data type Fraction you wrote in the previous assignment had a deficiency in that it returned
fractions that were not simplified (e.g., 24/48, 128/32, etc.). Fix this problem by writing a private
instance method simplify() that gets called as needed. Hint: the simplify() method will call
another private method gcd() that computes the greatest common divisor.
You can find the greatest common divisor (gcd) of two integers x and y using Euclid's algorithm, which is
an iterative computation based on the following observation: If x > y, then if y divides x, the gcd of x and
y is y; otherwise the gcd of x and y is the same as the gcd of x % y and y.
Test your enhanced implementation of the Fraction data type. Create test data to test all the
methods of the class. Write a client program that reads a set of fractions (pairs of integers) from
standard input and computes their cumulative sum and product.
2. Turtle
Write the Turtle class discussed in class and use it in a program that creates the following geometric
figure on the left.
The figure consists of n=36 identical circles. A circle is drawn as n turtle steps, each of length d=0.04
followed by a (360/n) degree rotation counterclockwise. Each of the n circles starts with the turtle at
(0.5,0.5) initially rotated by (360/n) degrees more than the previous circle. The first circle starts with the
turtle rotated 0 degrees. The figure on the right has one of the circles (the 6th one) highlighted in red.
Extend Turtle in the following ways:
Add color so that the path may be drawn in specified colors. Write a client to demonstrate this
feature.
Add error checking. For example, throw a RuntimeException with some meaningful
information if the turtle goes outside the designated boundary. Write appropriate client code
to demonstrate this feature.
Introduction to Programming (CMPS 200)
Fall 2011
2 of 2
3. PeriodicTable
Create a data type Element for representing elements in the periodic table of elements. Include private
variables to store element name, atomic number, symbol, and atomic weight, and accessor methods for
each of these values. Create appropriate tests to verify the correctness of the implementation.
Create a data type PeriodicTable. The data type should have a constructor that reads values from
a file to create an array of Element objects; a method that allows a user to enter a molecule name, such
as H2 O, and obtain its molecular weight; and a method that takes an array of molecule names
(Strings) and returns an array of names sorted by molecular weight.
The file elements.txt on moodle contains the data pertaining to elements, one element per line.
Learn how to use the instance method split() of the String class to help parse the individual lines.
4. Cardgame
In this program, you are to write a class CardGame that simulates a game of cards between two players.
In this game, each player draws a hand of five random cards from a standard card deck. The cards are
compared one by one (first card of player 1 with first card of player 2, second card of player 1 with
second card player 2, etc.). The strongest card (as defined in the Card class of last assignment) wins and
the player with the larger number of winning cards wins the game.
Design an appropriate internal representation for storing the state of the game (deck of cards,
cards of each player, winner of each card, game winner).
Write appropriate methods to shuffle the deck and distribute five cards to each player.
Write appropriate methods to evaluate the winning cards and find the winner of the game.
Write methods to display graphically the game as shown below. The zipped folder cards.zip
contains pictures of all cards, 73 pixels wide by 97 pixels high. You may use these files for your
display (you will need to generate the filenames dynamically). Hints: Use the Draw class, which
is very similar to StdDraw but is a class with a constructor and instance methods rather than a
module of static methods. The use of Draw will allow your program to generate multiple
canvases to draw on: one for the initial game state, and one that shows the winning cards. The
picture(double x, double y, String filename) instance method of the Draw class
displays the picture in filename centered at the location x, y in the canvas.
Notes
This assignment is a bit more involved than previous ones as the programs of problems 3 and 4 are now
more substantial and require some careful planning before implementation, and systematic incremental
debugging during implementation. Good Luck and let us know if you have any questions.