CSE 283 Introduction to Object Oriented Design

Barbara Nostrand, Ph.D.


CSE 283 Labs
Welcome to the site for CSE 283 Labs!

 

Lab 6: Projects

Prev | Next | Lab 6

 

Objectives

1. Practice designing, declaring and using classes.
2. Practice defining attributes.
3. Practice defining methods.

Introduction

Each of these projects deals with objects that are not readily modeled using the predefined Java data types. In each project, you are to build a class to represent the object described, as well as the operations appropriate for such an object. Your instructor will tell you which one of the following projects to do.

Projects

6.1. Extend class Fraction by adding methods for the arithmetic operators +, - and /, and the six relational operators (==, !=, <, >, <= and >=). Then write code that tests your new methods.

For extra credit, write a method for inverting a Fraction (i.e., if fract == n/d, its inversion is d/n).

6.2. A quadratic equation has the form

   ax2 + bx + c = 0

where a, b, and c are all real values. Write a class Quadratic that can be used to model a quadratic equation, with operations to construct, input, output, access the attributes, evaluate (for a given value of x), and find the value x=-b/2a at which the value of the Quadratic is minimized (or maximized).

To test your class, write a program called Evaluate that allows a user to enter a quadratic; computes and displays the maximum and minimum; and then allows the user to evaluate it at some input value.

Enter the quadratic 3x^2 + 2x + 1 = 0 by prompting for and inputing the coefficients in the following format:

   coefficients = 3.0 2.0 1.0

6.3. A phone number consists of four separate pieces of information: an area code, an exchange, a local number, and a long-distance indicator (true or false). Design and build a PhoneNumber class that models a phone number, providing operations to construct, input, output, access each of the attributes of a PhoneNumber object, and indicate whether or not the number is long-distance. The input operation should read a local or long-distance number and set the long-distance indicator accordingly. The output operation should display a local number differently from the way it displays a long-distance number (e.g., 555-1234 vs. (616) 555-1234).

To test your class, write a program that simulates an intelligent computer modem dialer by reading a PhoneNumber,  and displaying the number to be dialed.  If the number is a long distance number,  it should be preceded by 1-;  otherwise it should be displayed as a local number. 

6.4.  A dual stop watch can time two runners simultaneously.  It has one button to start the timer and two buttons to stop the timer (one for each runner. ) It will have three attributes for keeping track of the time:  a start time and for each runner a stop time.  It will also have two attributes that indicate the state (running or not running) of the stop watch for each runner.  Design and build a DualStopWatch class that models a stop watch.  Your class should provide operations to construct,  start timing, stop the timer for either runner,  and display the time for either runner.  If the stop watch is running,  you should display the current elapsed time.  If the watch is not running you should display the final elapsed time. 

Hint:  You can get the current system time in milliseconds using the class java.util.Date via the code: 

   long current = (new Date()).getTime();

To test your classes,  write a program that starts the watch and prompts the user to stop it.  Make sure to check all the operations of the stop watch. 

6.5. Craps is a popular game of chance both in casinos and back lots.  Write a class called Craps which plays a simplified game of craps with the user.  The computer takes the roll of the 'house'.  Your system should include a class called Dice which simulates the dice.  The behaviour of the program should be as follows: 

  1. The player enters the amount of money in their banque roll. 
  2. The player bets some amount of money up to the amount remaining in their banque roll. 
  3. The computer rolls the dice for the player and announces the value of both dice and the total. 
  4. If the total is 2 or 12 the player loses his bet to the house and play resumes at step 2. 
  5. If the total is 7 or 11 the player wins his bet from the house and play resumes at step 2. 
  6. Any other total is called a point. 
  7. The computer rolls the dice for the player and announces the total. 
  8. If the total is 7 the player loses his bet to the house,  and play resumes at step 2. 
  9. If the total is equal to the point the player wins his bet from the house, and play resumes at step 2. 
Some rolls have special names. A total of 2 is called 'snake eyes' and a roll of 12 is called 'box cars'. 

The game ends either when the player runs out of money or enters a bet of 0. When the game ends,  the computer announces the amount of money still held by the player. 

Be sure to implement a Die class with a roll method.  You will need a source of random numbers to immplement your roll method.  The Java Class Library includes a Random class which instantiates random number generator objects.  To use this class,  you will need to import it.  The Random class is in the java.util library.  This is a large library,  so please do not import all of the classes in it.  Instead,  just import the class Random.  The asterix commonly seen at the end of an import statement indicates that all of the classes in a particular directory are to be imported.  It does not mean that sub-libraries are to be imported.  Libraries and sublibraries must always be explicitly specified. 

Prev | Next | Lab 6

 
  Home

Schedule

Help

Lab 0

Lab 1

Lab 2

Lab 3

Lab 4

Lab 5

Lab 6

Lab 7-8

Lab 9

Lab 10

Lab 11

Lab 12

Lab 13






Last modified: 2007 OCT 18
bnostran@syr.edu