Class Methods


Class Methods

Bunny.java
BunnyTestHarness.java

read ch. 6, 8, start ch 7


From now on, every class that has instance variables should have at least one constructor, and all instance variables should be private and always have accessors and mutators (you already know there should be a toString) unless specified otherwise.


Classwork


Part A

Create a class Fraction which represents a fraction. Give it integer instance variables to represent numerator and denominator.

Create an accessor and mutator for each instance var. Use validation in your mutator to avoid dividing by zero.

Create a default constructor that sets the instance variables to reasonable default values.

Also create a parameterized constructor that takes values for both numerator and denominator.   Chain the constructors together to make sure we start with good defaults.

Create a toString method that returns something like  "(3/4)" using the numerator and denominator

Add the class ClassMethodsMain.java to your project. It already has code to test a Fraction created with the default constructor (you may have to adjust the names of the methods being called depending on what you called your instance variables). 

To this main, also add code to create and print a Fraction created with the parameterized constructor.


Part B

Create a new class Player, which will represent a person who makes various attempts at a task, and keeps track of success rate.

Player should have an instance variable to hold a name and an instance variable of type Fraction named myScore (read it again: an instance variable of type Fraction). Player will use the denominator of myScore for tries and the numerator for successes. Player will NOT have separate ints for doing this.

Write a constructor that sets a default name and sets up the myScore as 1/1  (hint: you wrote a constructor in Fraction that should make this easy, but literally writing 1/1 will NOT work).

Write accessor and mutator for name.

Write accessor and mutator for myScore (hint: think about what type myScore is!).  In the mutator, do validation: we shouldn't accept a Fraction as a value for myScore if it would make the number of successes greater than the number of tries.

Write a toString() which gives the scorer's name and percent rate of success. You don't have to worry about the format of the number when printing (but hint: it shouldn't always be zero).

The main method given earlier also has code to test Player; uncomment it when ready.

 

[EC] Add a method win() and a method lose(), each of which update the values in myscore. win() increases both the number of successes and the number of tries, lose increases only the number of tries. You will need to use the accessors and mutators from Fraction to do this.

[EC] Add a constructor to Player that takes a String and two ints. Set the name to the String, and then set up the myscore using the first number for numerator and the second for denominator.  Add code to the main to test this.