Download presentation
Presentation is loading. Please wait.
Published byLiani Tedjo Modified over 6 years ago
1
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod
2
Today Continue Encapsulation, which includes defining:
Private Attributes Constructors Mutators Accessors toString compareTo equals clone Document the class using javadoc comments. Fall 2018 CISC124 - Prof. McLeod
3
So Far… We still really need a class to contain historical data about Halloween numbers, including weather conditions. We have private attributes and constructors. We figured out how to prevent a privacy leak on mutable arguments coming in to the object. Constructors are a one-shot deal, invoked during instantiation. If you provide mutators your object’s attributes are mutable. Fall 2018 CISC124 - Prof. McLeod
4
Assigning Private Attributes - Constructors, Cont.
Suppose you don’t want to have to force the user to supply all parameters when he instantiates the object? How are you going to do this? Suppose you want to edit parameters after you have created the object? How are you going to do this, too? Overload the constructors. Provide mutator(s). Fall 2018 CISC124 - Prof. McLeod
5
Assigning Private Attributes - Mutator Methods
Called “set” methods – in fact, by convention, the word “set” is the first word in the method name. These methods must also check for legal parameters. Usually the constructor invokes mutators, when they exist. Should you write a mutator for each parameter? Fall 2018 CISC124 - Prof. McLeod
6
Halloween3 Has overloaded constructors. Has mutators.
Better information privacy. A Halloween3 object can be created with or without the weather condition, but must have the year, the number of munchkins and the temperatures. All parameters can be changed later. How can you still let the constructor use mutators, but not allow the user to change an attribute later? Fall 2018 CISC124 - Prof. McLeod
7
Aside - Dependent Attributes
The legality of weather condition and temperature array attributes are related. At the moment, because they have separate mutators it would be possible to set one attribute to an illegal value compared to the other one. How would you fix this? Fall 2018 CISC124 - Prof. McLeod
8
Aside - The this Thing Used by one constructor to invoke the other one. Can be used to supply a reference to the current object in that object’s code at run-time. this means “myself”. Another use – suppose you have the same attribute name as a constructor parameter – year, for example. In the constructor, I would need to say: this.year = year; Fall 2018 CISC124 - Prof. McLeod
9
We Need More! How do we get data values back out of the Halloween3 object? What happens when you try to print the object? How can you compare objects for sorting and searching? How do you clone the object? Finally – how do we document the object so other people can use it without looking at the code? Fall 2018 CISC124 - Prof. McLeod
10
Accessors Accessor methods return the value of an attribute.
By convention, accessor methods start with the word get. One accessor per attribute. For example: public int getNumMunchkins () { return numMunchkins; } // end getNumMunchkins Accessor Fall 2018 CISC124 - Prof. McLeod
11
Accessors, Cont. And, it is not unusual for an accessor to return some thing that is a result of some process carried out on its other attributes. For example, an attribute String could be a filename. An accessor could return the contents of the file, even though these contents are stored in the file, not in the object. Fall 2018 CISC124 - Prof. McLeod
12
Accessors, Cont. These can be pretty simple methods.
Except when you are returning a mutable object. What is wrong with the following? public int[] getTemperatures() { return temperatures; } // end getTemperatures Accessor Fall 2018 CISC124 - Prof. McLeod
13
Accessors, Privacy Leaks, Cont.
Better: public int[] getTemperatures() { return temperatures.clone(); } // end getTemperature Accessor Fall 2018 CISC124 - Prof. McLeod
14
Other Standard Methods
toString() equals() compareTo() clone() These are all properly coded in the, now complete, Halloween4 class. It also contains Javadoc comments (the blue ones), which you can ignore for now. Fall 2018 CISC124 - Prof. McLeod
15
toString() Method As it is, if we try to print a Halloween object, we will just get “gobbldeygook”: (This String is composed of the object type and its hash code…) So, to get a more useful view of the contents of the object, define a toString() method that returns a String. Fall 2018 CISC124 - Prof. McLeod
16
toString() Method, Cont.
This method will be called automatically whenever a String version of the object is needed. You decide on how you want to represent your object as a String. The method in Halloween4 produces a string like: In 1982 there were 200 kids. Temperatures each hour were: 12, 10, 11, and 9 deg C., and the weather was clear. Fall 2018 CISC124 - Prof. McLeod
17
Aside - Annotations The “@Override” thingy.
You don’t have to use these, but the pre-compiler likes them. This one tells the pre-compiler that “We know we are overriding the toString method inherited from the parent class (Object) and we are OK with it!” The pre-compiler likes these so much it will, at times, encourage you to put them in. Fall 2018 CISC124 - Prof. McLeod
18
equals() Method Accepts another object of type Halloween4 and returns true if they are equal, false otherwise. You get to define what “equality” means. (Usually all the attributes have to match.) Remember why you cannot use == to compare objects? Fall 2018 CISC124 - Prof. McLeod
19
equals() Method, Cont. Two ways to write an equals() method (both will return true or false): Accept a Halloween4 object as a parameter. Accept an Object as a parameter (Object is the base class for all objects in Java!) The proper way is the second one – then you will override the equals() method inherited from the base Object class. See this method in Halloween4. Fall 2018 CISC124 - Prof. McLeod
20
equals() Method, Cont. The instanceof keyword checks to see if the supplied Object is indeed a Halloween4 object before it attempts to cast it (otherwise you get a ClassCastException). Then the object is cast to be of type Halloween4 using the casting operator. Note how the method can refer directly to the attributes of the other object! How is this possible? Fall 2018 CISC124 - Prof. McLeod
21
compareTo() Method Compares a supplied Halloween4 object with the current one, based on your comparison criteria. It returns an int value. (Like the compareTo() method in the String class.) You have to decide on the basis for comparison. For Halloween4 you could just use numMunchkins, for example. Fall 2018 CISC124 - Prof. McLeod
22
compareTo() Method, Cont.
The base Object class does not have this method, so you don’t have to worry about writing one to take an Object as a parameter. Fall 2018 CISC124 - Prof. McLeod
23
clone() Method Returns a deep copy of the current object.
A “deep copy” is not aliased in any way to the original object, but contains the same values for each attribute. See TestHalloween4.java for our object in action! Fall 2018 CISC124 - Prof. McLeod
24
Exercises and Assignment
You are now able to work on both Exercises 8 and 9. After we see how to implement a couple of interfaces in Halloween5 and discuss Javadoc commenting you will know what you need for assignment 3. Fall 2018 CISC124 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.