| Project #4 | CMSC 131 |
| Due Tuesday 10/18/05 at 11:00 PM | Object-Oriented Programming I |
| Type of project: Closed | Fall 2005 |
Objective
To practice writing classes and JUnit test cases.
Overview
For this project you will write two classes, Tiger and Trainer. You will also write an extensive set of JUnit tests, which should test all of the methods you have written in your classes. This project is different from most projects in this course, because there is no main() method anywhere. What you are creating are two re-useable modules (Tiger and Trainer) that could later be made part of a complete program.
Put all of your work (Tiger.java and Trainer.java) in the folder we have provided labelled "src".
Tests
Writing the Classes
We are leaving many decisions about the implementation of these classes for you to decide. DO NOT post questions on the wiki about how these choices should be made. (Obviously your questions about how to make these decisions are welcome during office hours; we will guide you, but we will not explicitly tell you what to do.) Below are descriptions of the two classes you must write.
Tiger Class
The "state" for a Tiger object contains three fields, described below. You may also include any static constants that you think are useful.
- name (represented as a string)
- age in years (integer values only)
- weight in pounds (values may have decimal points in them)
The methods you must write are described below. As mentioned previously, we are intentionally leaving a lot of decisions about how to write these methods for you to decide. In particular, you must decide which of these things (if any) should be static. In addition to the methods below, please feel free to also include as many private methods as you find useful.
Public methods
- default constructor -- set the name to "Tony"; set age to 6; set weight to 475.7 pounds.
- constructor with one "String" parameter -- set the name to the parameter; set age to 6; set weight to 475.7 pounds.
- constructor with three parameters representing the name, age, and weight intended for the Tiger being constructed. Before setting the fields, you must verify that the age and weight are within acceptable ranges. The age for a Tiger must be between 2 and 14 (inclusive). If the age parameter passed in by the user does not fall within this range, you should silently (without any error message) set the Tiger's age to 6. The weight for a Tiger must be between 405.0 and 590.0 pounds (inclusive). If the weight parameter passed in by the user does not fall within this range you should silently set the Tiger's weight to 475.7 pounds. Set the Tiger's name to be equal to the name parameter.
- copy constructor -- for this project, you should NOT check if the parameter is equal to null (even though this was suggested in class).
- getName -- access method
- getAge -- access method
- getWeight -- access method
- speak() -- a method with no parameters, which returns a String. The return value should be equal to: "My name is <name> - welcome to the circus." where <name> represents the Tiger's name.
- say(String message) -- a method with one "String" parameter, which returns a String. The return value will be exactly the same as the parameter, EXCEPT: all occurrences of the substring "dog" within the parameter will be replaced by "cat". For example, if the parameter is "The dog is man's best friend." then the return value should be: "The cat is man's best friend." Note that the replacement rule is case-sensitive, so do not do any replacements for substrings like "DOG", "DoG", "dOg", etc. [Hint: you may want to read over the documentation for the API of the String class to find a couple of methods that are useful for implementing the "say" method.] EDIT: THIS METHOD WOULD BE FINE EITHER STATIC OR NON-STATIC. YOU SHOULD MAKE IT NON-STATIC, OTHERWISE ONE OF THE RELEASE TESTS WILL FAIL! (SORRY.)
- equals -- write a good equals method. Two Tigers should be considered equal if all three fields are exact matches.
- toString -- the return value should be of the form: "<name>, age <age>, weight <weight>" where <name>, <age>, and <weight> are the corresponding fields of the current object.
Private methods
- isAgeValid(int ageToCheck) -- returns true if the parameter is between 2 and 14 (inclusive), false otherwise.
- isWeightValid(double weightToCheck) -- returns true if the parameter is between 405.0 and 590.0 (inclusive), false otherwise.
Trainer Class
The "state" for a Trainer object contains two fields, below. You may also include any static constants that you think are useful -- if you do, keep them private.
- name (a String representing the Trainer's name)
- a reference to a Tiger (supposedly the Tiger that the Trainer works with.)
The methods you must write are described below. As mentioned previously, we are intentionally leaving a lot of decisions about how to write these methods for you to decide. In particular, you must decide which of these things (if any) should be static. In addition to the methods below, please feel free to also include as many private methods as you find useful.
public methods
- default constructor -- initialize the Trainer's name to "Tim", and give him a Tiger constructed using the default Tiger constructor.
- constructor with two parameters representing the Trainer's name and the Tiger he works with. Assign the fields accordingly. Important: Since the Tiger class is "mutable", the Tiger that is assigned to this Trainer should be a COPY of the Tiger parameter.
- copy constructor -- as above, be sure to avoid "aliasing" by ensuring that the Tiger assigned to the current object is a COPY of the Tiger assigned to the parameter.
- getName -- access method.
- getTiger -- access method.
- doTheAct() -- returns a String. As an example of the proper return value, assume that the current Trainer is named "Fred". Imagine that his Tiger is named "Bob", age 7, weight 499.9 pounds. In this case, the correct return value would be: "I am Fred, trainer of Bob, age 7, weight 499.9. Bob says: My name is Bob - welcome to the circus."
- equals -- write a good equals method. Two trainers are considered equal if they have the same name and identical Tigers.
- toString -- returns a String. As an example of the proper return value, assume that the current Trainer is named "Fred". Imagine that his Tiger is named "Bob", age 7, weight 499.9 pounds. In this case, the correct return value would be: "Fred, trainer of Bob, age 7, weight 499.9". (Note that there is no "period" at the end of the return value.)
private method
- makeTigerSpeak() -- returns a String. As an example of the proper return value, assume that the current Trainer has a Tiger named "Bob", age 7, weight 499.9 pounds. In this case, the correct return value would be: "Bob says: My name is Bob - welcome to the circus."
Requirements
Grading
Your grade will be determined as follows:
Challenge Problem
Add a method to the Tiger class with the following prototype:
public String lazySpeak(String message, int n)
The return value should consist of the concatenation of every nth word from the message. In other words: If n = 3, and the message is "This is my message. How are you doing today? I hope you are doing well." Then the return value should be the String "This message. you I are".
Note: There could be arbitrarily many spaces between words in the parameter. In your return String you may have any number of spaces between words, including 0.
If you do the challenge problem this time, please submit JUST ONE version of your project as usual.