Class Methods
Standard Class Methods and Methods in Classes
- public interface and private implementation
- accessors and mutators
- constructors - default, parameterized
- methods
Bunny.java
BunnyTestHarness.java
Deitel 3, 4
Instance variables should be private from now on and classes that
have them should always have
accessors and mutators, constructors, and toString, unless specified
otherwise.
Classwork
Part A
☑ Create a class Fraction which represents a fraction. It will need to represent a
numerator and a denominator as separate integers.
☑ Create an accessor and mutator
for each instance var. Use an if for validation so we avoid dividing by zero.
(0/-5 is perfectly valid)
☑ Create a default constructor that sets the instance variables to reasonable default values.
(What would you think of as a standard example fraction, or what would
you think of as a good starting point for a fraction? There is
more than one right answer.)
☑ Also create a parameterized constructor that takes values for both numerator and denominator
as parameters. Chain them together with this() to make sure they start with good values
even if we are given 0 for the denominator.
☑ Create a toString method that returns a
String for the fraction, formatted in the usual way,
e.g. "(3/4)"
☑ 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).
(This class also has code to test later parts, you can comment out
multiple lines of code starting with /* and ending with */
☑ Add code to main to check your
parameterized constructor too.
Part B
☑ Create a new class Player
This will represent a person who makes various attempts at a task, and keeps track of success rate.
☑ Give Player an instance variable to hold a name.
☑ Give Player an instance variable of type Fraction named score.
(Player will use the denominator of score for tries and the numerator for successes.
Player should not have its own separate variables for these!)
☑ Write a constructor that sets a default name and sets up the score as 1/1
(what tools do you already have to set up a Fraction with good starting values?).
☑ Write accessor and mutator for
score, and for name. For score, do not accept scores where numerator >
denominator. (This is a requirement we have for scores, not something we
are requiring of all Fractions in any context.)
☑ Add a method win() to update the
score when the task is successful. In this case, we need to increase both
the number of tries and the number of successes.
☑ Add a method lose() to update
the score when the task is unsuccessful. In this case we need increase only the number of tries.
☑ Write a toString() which returns
something likeConstance has been successful 6 out of 10 tries.
☑ Go back to main and uncomment the rest of the test code and check that your class works.
[EC]: Add a constructor that takes a String and two ints. Set the name to the String, and then set up the score using the first number for numerator and the second for denominator.
(think about validation and where it should happen) Test this in main.
[EC]: Back in the Fraction class, change the
toString so that if the numerator is larger than the denominator it
prints as a mixed number (e.g. "1 (4/8)" rather than "(12/8)"
using only operators and skills covered so far in the course. You
will probably want to add some extra code in the main to check that this
works for different values