DESC 391 Assignments Fall 2009 - Assignment 2: Circles and dice Print E-mail
Written by Chitu Okoli and Bilal Abdul Kader   
Friday, 19 September 2008 11:35
Article Index
DESC 391 Assignments Fall 2009
1: Interest and coins
2: Circles and dice
3: Student scores, and reservations
4: GUI programming
All Pages

Assignment 2: Circles and dice

Due: 11:59 pm on Friday October 9

The purpose of this assignment is for you to begin writing simple object-oriented programs in Java. There are two exercises.

1. Calculate a circle’s circumference and area (3.5%)

Console

Welcome to the Circle Tester
 
Enter radius:  3
Circumference: 18.85
Area:          28.27
 
Continue? (y/n): y
 
Enter radius:  6
Circumference: 37.70
Area:          113.10
 
Continue? (y/n): n
 
Goodbye. You created 2 Circle object(s).

Operation

  • The application prompts the user to enter the radius of a circle.
  • If the user enters invalid data, the application displays an appropriate error message and prompts the user again until the user enters valid data.
  • When the user enters a valid radius, the application calculates and displays the circumference and area of the circle to the nearest 2 decimal places.
  • The application prompts the user to continue.
  • When the user chooses not to continue, the application displays a goodbye message that indicates the number of Circle objects that were created by the application.

Specifications

  • Create a class named Circle to store the data about this circle. This class should contain these constructors and methods:
    public Circle(double radius)
    public double getCircumference()
    public String getFormattedCircumference()
    public double getArea()
    public String getFormattedArea()
    private String formatNumber(double x)
    public static int getObjectCount()
  • Create any fields (instance variables) necessary to implement your solution. However, these must all be declared private.
  • The formulas for calculating circumference and area are:
    circumference = 2 * pi * radius
    area = pi * radius
    2
  • For the value of pi, use the PI constant of the java.lang.Math class.
  • Create a class named CircleApp that gets the user input, creates a Circle object, and displays the circumference and area.
  • Create a Validator class like the one from chapter 6 to validate the input data in this application. Use a comment at the top of the class file to clear cite that you got the code from chapter 6, with the full textbook title and author name. (Note: This specification is not optional—that is, you must use code from chapter 6, and you must cite it properly. This is to demonstrate that you know how to properly cite borrowed code.)

2. Roll the dice (4.5%)

Console

Welcome to the Monopoly Roller application

Roll the dice? (y/n): y

Roll 1:
2
5
Total: 7

Roll again? (y/n): y

Roll 2:
3
3
Total: 6

Double! Roll again!

Roll 3:
1
6
Total: 7

Roll again? (y/n): y

Roll 4:
2
2
Total: 4

Double! Roll again!

Roll 5:
3
3
Total: 6

Double! Roll again!

Roll 6:
4
4
Total: 8

Go to jail!

Roll again? (y/n): n

Operation

  • If the user chooses to roll the dice, the application rolls two six-sided dice, displays the results of each, and asks if the user wants to roll again, following MonopolyTM  rules for rolling dice.

Specifications

  • Create a class named Die to store the data about each die. This class should contain these constructors and methods:
    public Die()              // default to a six-sided die
    public Die(int sides)     // allow a variable number of sides
    public void roll()        // Use Math.random() to generate a number from 1 to 6
    public int getValue()
  • Create any fields (instance variables) necessary to implement your solution. However, these must all be declared private.
  • Create a class named PairOfDice to store two dice. This class should contain two instance variables of the Die type, an instance variable that holds the sum of the two dice, and these constructors and methods:
    public PairOfDice()           // default to six-sided dice
    public PairOfDice(int sides)  // allow a variable number of sides
    public void roll()
    public int getValue1()        // get value of die1
    public int getValue2()        // get value of die2
    public int getSum()           // get the sum of both dice
  • Create a class named DiceRollerApp that uses the PairOfDice class to roll the dice. This class should display the value of each die, and the sum of the two dies. For this application, assume that two six-sided dice are used.
  • If a double is rolled (that is, both dies roll the same number), then the system should automatically roll again without prompting the user. If the resulting roll is also a double, then the system should automatically roll again. However, if the third consecutive roll is a double, the system should print, “Go to jail!” and then continue as normal (that is, prompt the user whether to roll again or not).
  • Accept only “y” or “n” (lower or upper case). (If you like, you could also accept “yes” or “no”.) For any other input, display an appropriate error message and repeat the prompt.

Submission

To submit the assignment, do the following:

  • Package your completed assignment as one zip file with two executable jar files:
    • The first jar file named "YourFirstName YourLastName Assignment 2 CircleApp.jar" should have three classes: CircleApp, Circle, Validator. Package it in BlueJ using Project -> Create Jar File... -> Continue (Main class: CircleApp; select Include source and BlueJ project files).
    • The second jar file named "YourFirstName YourLastName Assignment 2 DiceRollerApp.jar" should also have three classes: Die, PairOfDice, DiceRollerApp. Package it in BlueJ using Project -> Create Jar File... -> Continue (Main class: DiceRollerApp; select Include source and BlueJ project files).
    • Zip the two jar files into one file named "YourFirstName YourLastName Assignment 2.zip".
  • E-mail the zip file to This e-mail address is being protected from spambots. You need JavaScript enabled to view it no later than 11:59 pm on Friday October 9.
  • This assignment counts as 8% of your class grade.

 

 



Last Updated on Tuesday, 08 December 2009 11:16