Name__________________________________________

Csci 6                                                                                                               Fall, 2003

 

Lab_04 - Work with classes and JCreator.

 

Rational class and RationalTest

 

Rational class

            A Rational Number is a value that can be written as a ratio of two integers (a fraction).            You will be writing a Rational class and a test program (RationalTest) for this class        using JCreator.

·         Create a new workspace (Lab4WS)

·         Create a new project (Drew Application) in this workspace (RationalTest).

·         Create a new Java file in this project (Rational)

 

·         A Rational has 2 instance fields (numerator, denominator)

·         A Rational has the following methods:

1.       one constructor with two parameters, one for the value of the numerator and one for the value of the denominator. This constructor will ensure that the denominator is unsigned (positive) and non zero and will reduce the Rational by calling the private method reduce. Examples (found in the test program):

 

                                    Rational r1 = new Rational (-2,-4);

            after the constructor code has executed,

                                    r1.numerator = 1 and r1.denominator = 2

 

                                    Rational r1 = new Rational (2,-4);

            after the constructor code has executed,

                                    r1.numerator = -1 and r1.denominator = 2

 

                                    Rational r1 = new Rational (2,0);

            after the constructor code has executed,

                                    r1.numerator = 2 and r1.denominator = 1

 

                                    Rational r1 = new Rational (-2,-4);

            after the constructor code has executed,

                                    r1.numerator = 1 and r1.denominator = 2

 

                                    Rational r1 = new Rational (-2, 4);

            after the constructor code has executed,

                                    r1.numerator = -1 and r1.denominator = 2

 

 

2.       public method getNumerator that returns the integer value of the numerator

3.       public method getDenominaotr that returns the integer value of the denominator

4.       public method getReciprocal that returns a Rational that is the reciprocal of this Rational.

5.       public method add that has one Rational parameter and returns a Rational that is the sum of this Rational and add's Rational parameter.

6.       public method subtract that has one Rational parameter and returns a Rational that is the difference of this Rational and subtract's Rational parameter.

7.       public method multiply that has one Rational parameter and returns a Rational that is the product of this Rational and multiply's Rational parameter.

8.       public method divide that has one Rational parameter and returns a Rational that is the quotient of this Rational and divide's Rational parameter.

9.       private method reduce that will reduce the numerator and denominator by their largest common factor.

10.   public method equals that has one Object parameter. Compares this Rational to the parameter. The result is true if and only if the two have numerators values that are equal and denominator values that are equal.

11.   public method compareTo that has one Object parameter. Compares this Rational to the parameter. Returns the value 0 if this Rational is equal to the parameter; a value less than 0 if this Rational is numerically less than the parameter; and a value greater than 0 if this Rational is numerically greater than the parameter (signed comparison).

12.   public method toString that returns a String representation of this Rational. Example : if numerator = 5 and denominator = 7, toString returns "5/7".

 

RationalTest

Your RationalTest contains the main method.

 

Get a values for the numerator and denominator of r1 (n and d) from the user either with EasyReader or JOptionPane. These value should be integers between -20 and 20 inclusive.

 

Rational r1 = new Rational(n,d);

 

Get a values for the numerator and denominator of r2  (n2 and d2)by randomly generating them using the java.util.Random class. These value should be integers between -20 and 20 inclusive.

 

Rational r2 = new Rational(n2,d2);

 

            Include the following statements in your test program:

 

Rational rSum, rDiff, rProd, rQuot, rRecip;

System.out.println ("First rational number: " + r1);

System.out.println ("Second rational number: " + r2);

 

if (r1.equals(r2))

    System.out.println ("r1 and r2 are equal.");

else

    System.out.println ("r1 and r2 are NOT equal.");

 

if (r1.compareTo(r2) < 0)

    System.out.println ("r1 is less than r2.");

else if (r1.compareTo(r2) > 0)

    System.out.println ("r1 is greater than r2.");

else

    System.out.println ("r1 and r2 are equal.");

 

rRecip = r1.reciprocal();

System.out.println ("The reciprocal of r1 is: " + r3);

 

rSum = r1.add(r2);

rDiff = r1.subtract(r2);

rProd = r1.multiply(r2);

rQuot = r1.divide(r2);

 

System.out.println ("r1 + r2: " + rSum);

System.out.println ("r1 - r2: " + rDiff);

System.out.println ("r1 * r2: " + rProd);

System.out.println ("r1 / r2: " + rQuot);

 

 

·         If you find it useful to add other "utility methods" to your Rational class, you may do so.

·         Remember to document using Javadoc comments.

·         Remember to check Company Rules.

·         Remember to choose appropriate test cases.

·         You are permitted to work with one other person on this project but you must document this in your comments. If the entire project is worked on together, submit only one copy.

·         Include a statement at the beginning of your program that confirms that the work in the program is that of the author(s) listed and theirs alone.

 

 

 

Roulette

 

2.   A modified game of roulette is played as follows.  A player has a purse that contains coins. The player bets one coin chosen randomly from his purse on a number on the roulette wheel. This modified roulette wheel is shown below.

 

 

 

 

 

 

 

 

 

 


The spinner spins. If the spinner lands on '0', the player gets nothing back. If the spinner lands on '2', the player gets his coin back and an additional coin of equal value. If the spinner lands on '3', the player gets 3 coins of the same value as the coin he bet.

In this particular version of roulette, you either win big or you lose big because in this version of roulette, the spinning continues until you double the money in your purse or you have no coins left in your purse! The classes used are:

 

      The Spinner class is used to "spin" a generic roulette wheel that has all choices equally                           probable.

      The Coin class defines a coin.

      The Purse class that holds the coins.

      The Game class that simulates the roulette game.

      The GameTest class that tests the Roulette Game.

 

Incomplete definitions of these classes are below.

You need to write code for this class.

 

            import java.util.Random;

      // The Spinner class is used to simulate the spin.

public class Spinner

{

   // Constructs a spinner with s choices

   public Spinner(int s)

   {

         // you write the code

   }

 

   // returns an integer from 0 to choices - 1    

   // inclusive. All possibilities are equally likely.

   public int spin()

   {

         // you write the code

   }

   

   private Random generator;

   private int choices;

}

 

There is no code for you to write for any method this class.

 

//   A coin with a monetary value.

public class Coin

   {

   /**

      Constructs a coin.

      @param aValue the monetary value of the coin.

      @param aName the name of the coin

   */

   public Coin(double aValue, String aName)


 

   /**

      Gets the coin value.

      @return the value

   */

   public double getValue()

 

 

   /**

      Gets the coin name.

      @return the name

   */

   public String getName()

 

   // Example of implementing equals for the Coin class

   public boolean equals(Object otherObject)

   { 

      Coin other = (Coin)otherObject;

      return name.equals(other.name)

         && value == other.value;

   }

 

   private double value;

   private String name;

}

 

There is no code for you to write in this class. We have not covered all of topics that you may see used in this class but you should understand how to invoke each method and you should understand the task each method performs.

 

public class Purse

{

   // constructs an empty purse

   public Purse()

  

   // Add a coin to the purse.

   public void add(Coin aCoin)

  

   // Removes a coin from the purse.

   public Coin removeCoin()

 

   // Returns the total value of the coins in the purse

public double getTotal()

 

// Returns the number of coins in the purse

   public int coinCount()

     

   // Other methods and private stuff goes here

}

 

You DO have to write code for this class!

 

public class Game

{

   public Game(Purse myPurse)

   {

         myWheel = new Spinner(4);    

   }

  

  

   public int spinTheWheel()

   {

      // You write the code - follow the pseudocode given

     

   }    

  

   private Spinner myWheel;

 

}

 

/**

   This class tests the Roulette Game

*/

public class GameTest

{

   public static void main(String[] args)

   {

      double NICKEL_VALUE = 0.05;

      double DIME_VALUE = 0.1;

      double QUARTER_VALUE = 0.25;

 

 // You should try adding different types and numbers of coins

 // when testing this program.

      Purse myPurse = new Purse();

 

         //adding coins to my purse  

         myPurse.add(new Coin(DIME_VALUE, "dime"));

         myPurse.add(new Coin(DIME_VALUE, "dime"));

         myPurse.add(new Coin(QUARTER_VALUE, "quarter"));

         myPurse.add(new Coin(NICKEL_VALUE, "nickel"));

     

      // intstantiating a Game  

      Game roulette = new Game(myPurse);

      System.out.println("Spinning Wheel");    

     

      // printing starting amount of money in purse

      double total = myPurse.getTotal();

      System.out.println("My staring money is: " + total);

      // printing ending amount of money in purse      

      total = roulette.playRoulette(myPurse);

      System.out.println("The total now is " + total);

    }  

}

 

a)   Write the Spinner constructor. This constructor has one parameter that will be the number of equally probable choices on the Spinner. The constructor will initialize choices to this parameter value and will instantiate a new random number generator.

 

b)  You are to write the Spinner class public method spin.  spin will return a random spin of the wheel (0 to  choices -1). All  possibilities are equally probable.

 

c)   Write the Game method spinTheWheel that will call spin to generate a random number and then return the appropriate number on the roulette wheel. Notice that all outcomes on the wheel do not have equal probabilities. Use the header below when writing spinTheWheel.

 

            public int spinTheWheel()

 

d)   Write the method playRoulette, that will bet (remove a coin from the purse), spin this roulette wheel, and accumulate appropriate winnings by adding coins to the purse until the player either wins big (doubles his original purse value) or loses big (has no coins left in his purse). Write playRoulette using the method header below. Pseudocode is given below the method header.

 

public double playRoulette(Purse myPurse)

·         Find out how much money in the purse at the beginning so that you know when you win big.

·         while (there are coins left in my purse and I haven't doubled my money)

o        Take a coin from the purse (invoking the appropriate method of the Purse class) and write its name to the screen.

o        Spin the wheel (invoking the appropriate method of the Game class) and write the resulting spin to the screen.

o        Add the appropriate number of the appropriate coins to the purse (invoking the appropriate method of the Purse class within a loop)

·         Return the amount of money in the purse.