This question concerns a class called Child which is to be developed as a subclass of the
Question:
This question concerns a class called Child which is to be developed as a subclass of the class Person (click to view this file), which has already been developed. Persons have a first name, a last name, wear white shirts by default, have a number of friends, and have an amount of money. Children may be in a playing state, or not playing. The Java library class java.awt.Color is used to represent shirt colours such as Color.WHITE. Note that printing out a colour produces output such as java.awt.Color[r=0,g=0,b=255] (which represents Color.BLUE in this case). The three numbers represent components of Red, Green, and Blue colour.
Develop only the class Child
(a) Add the class Child below, making it a subclass of Person.
(b) Add a private instance variable to the class called playing of type boolean.
(c) Add a public constructor for Child whose first parameter is the child's first name and whose second parameter is the child's second name.
The constructor should initialise the child's first and last names using the received arguments. The instance variable playing should be set to true. The initial money should be set to 10.
(d) Add a standard getter method for playing called isPlaying.
(e) Add a public method play, which takes no arguments and returns no value. The method sets playing to true and increments the child's number of friends by 1.
(f) Add a public method work, which takes no arguments and returns no value. The method sets playing to false and and decrements the child's number of friends by 1. (Don't worry about the value becoming negative.)
(g) Add a public method getNickname that returns a nickname for the child based on their first and last names at the time the method is called. The method returns the first three letters of the child's first name concatenated to the last three letters of the child's last name in lowercase. (You can assume the names are long enough.)
For example, if the child's first name is "Betsy" and their last name is "Dee" the method will return the string "Betdee".
(h) Add a public method buySnack which does not return a value and has a single parameter of type int representing the cost of a snack. If the child has enough money to buy the snack then the method decreases the money the child owns by the argument received, otherwise it just prints
I need money
(i) Add a public method
goHome which does not return a value and takes no arguments. If the child has no friends then the method prints
I'm going home
Otherwise the method prints
Bye
as many times as the child has friends, each on a separate line.
(j) Add a public setShirtColour method to override the inherited method of that name.
If the child is not playing, the child's method behaves in the same way as the inherited method.
If the child is playing, its setShirtColour method behaves as follows:
If the child is wearing a shirt that isColor.WHITE then the method prints
I'm changing now
before setting the child's shirt colour to the argument received by the method.
If the child is not wearing a shirt that is Color.WHITE then the method prints
I'm wearing play clothes already
but doesn't change the shirt colour.
Below is an example test case for this class.
For example:
Test | Result |
---|---|
//working Child f = new Child("Jan", "Feb"); System.out.println(f.getNumFriends()); f.work(); System.out.println(f.isPlaying()); System.out.println(f.getNumFriends()); |
Business Law Principles for Today's Commercial Environment
ISBN: 978-1305575158
5th edition
Authors: David P. Twomey, Marianne M. Jennings, Stephanie M Greene