Question
You must write both a composite and a component class yourself. We now describe in detail what you need to do to complete this question.
You must write both a composite and a component class yourself.
We now describe in detail what you need to do to complete this question.
- a.You are first required to write a short description, using the provided table, in ordinary English, of the two classes you have identified and how they relate to each other. Please read the guidance below before you begin.
You are advised to keep your example simple, but you should be able to identify the following features:
- at least three instance variables in your composite class, one of which references an instance of your component class
- at least two instance variables in your component class
- appropriate methods in each class, including at least one method that allows the composite class to communicate with the component class in order to compute some value, for example to provide an attribute of the composite class.
Your description only needs to address the following requirements:
- i.Which is the component class, and which is composite class?
- ii.What are the instance variables of each of these classes and what are some examples of their values?
- iii.Identify and describe a method (not toString()) that allows the composite class to communicate with the component class in order to compute some value. (It is not sufficient to merely copy the value returned by the component object. It must be used in some kind of computation.)
- iv. Briefly describe other interesting features of the classes, but keep your description short, as suggested in the example below. You dont have to describe all of the features of the classes.
Within this project you are required to write the two classes that model your two kinds of object as agreed with your tutor. Your classes must follow the specification below. Note that in addition to the requirements set out in part (a) you are asked to give your classes some additional behaviours.
Requirement | Your answer |
1(a) Composite class | Game |
1(b) Component class | Player |
1(c) Relationship between the two | The Game has a Player |
2(a) Composite class instance variables and examples of use | Player, of type Player (providing the has a relationship) Title of type String, the title of the game Diablo 3 Releaseyear of type String, the year of game released 2013 Retailprice of type int, the retail value of the game 39 |
2(b) Component class instance variables and examples of use | Name of type String, the name of player Moock Age of type String, the age of the player 20 |
3 Description of method (not toString()) that allows the composite class to communicate with the component class in order to compute some value | Game has a method isUnderAge() that returns true if its player is 18 or over. Compute a Boolean value based on player age. |
4 Other interesting features of the class | The game will also provide a method isADeal() which returns true if a lower price is up, so it computes a boolean value based on retail value of the game. |
One class (the composite class) should have an instance variable that references an instance of the other class (the component class). This represents the has-a relationship between the classes.
In addition to the instance variable described above, each class should have at least two instance variables that represent attributes. It is not advisable to have more than the minimum number of instance variables unless you are confident of what you are doing and can spare the extra time.
The composite class should have a method, other than toString(), that sends its component object a message and makes use of the message answer in a computation of some sort.
Further requirements:
Each class should include at least one constructor with one or more arguments. In most cases you will need only one per class.
Each class should have, where appropriate, getter and setter methods for each of its instance variables.
Each class should have a method toString() that returns a description of an object of that class as a String that would be meaningful to a human reader.
The toString() method in the composite class must include at least one item of information obtained from the component object by sending it a message.
None of your methods should make use of dialog boxes for any purpose.
You should comment your code appropriately and use appropriate access modifiers. Insert your name, TMA02 Q2 and the date in the class description.
When you create each class in BlueJ you should start by deleting the example instance variable, the line of code in the constructor that assigns 0 to the variable, and the sample method from each class. You should also remove the comments related to the variable and sample method.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started