Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,

Similar presentations


Presentation on theme: "Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,"— Presentation transcript:

1 Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday, the 12th. or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

2 Quiz 1 Code Grading Summary
Coding problem average was about 3/10. A zero grade with no feedback means that “No line of code was correct in any sense.” A one with no feedback means that “There was a line or two that looked OK.” A grade of 10 with no feedback means “Well done!”. Otherwise you will see feedback explaining where you lost marks, along with the sample solution. Fall 2018 CISC124 - Prof. McLeod

3 Quiz 1 Code Grading Summary, Cont.
This is your first experience with how your Prof. grades code. If you obtained <= 5 on the coding problem, you did not code well. The relative weighting of code versus non-code questions will increase as the course progresses! At least 50% of the content of the final exam will involve reading and writing Java code. Fall 2018 CISC124 - Prof. McLeod

4 Today Continue Encapsulation, which includes: Defining Exceptions.
Fall 2018 CISC124 - Prof. McLeod

5 So Far… We really need a class to contain historical data about Halloween numbers, including weather conditions. We defined the attributes, but discovered that they must be private so that the object can control the values of the attributes. (Or the State of the object.) Now, the only way to set the attributes is through methods – Constructors and Mutators. Fall 2018 CISC124 - Prof. McLeod

6 Encapsulation, Cont. Within these methods, you can write code to check the supplied arguments for validity. What do you do if the values are not legal? Throw an exception! We need to start adding Behaviour to our Class. Fall 2018 CISC124 - Prof. McLeod

7 Defining Exceptions You can throw an exception already defined in java, but: Most of the time you will want to throw your own exception objects. See the next slide for the definition of an Exception object. (Please ignore indentation and line wrapping…) Fall 2018 CISC124 - Prof. McLeod

8 Defining Exceptions, Cont.
public class IllegalHalloweenException extends Exception { public IllegalHalloweenException() { super("Illegal parameter value supplied to" + "Halloween object."); } public IllegalHalloweenException(String message) { super(message); } // end IllegalHalloweenException Fall 2018 CISC124 - Prof. McLeod

9 Defining Exceptions, Cont.
Inside a method that detects an illegal parameter: throw new IllegalHalloweenException("Illegal number of kids."); At the end of the method header that contains the above line of code, you need this annotation: … throws IllegalHalloweenException { Fall 2018 CISC124 - Prof. McLeod

10 Defining Exceptions, Cont.
This example contains a few Java keywords that we have not yet discussed: extends super throw throws The exact meaning and usage of extends and super will become clear when we discuss object hierarchies. For now, just follow the pattern. Fall 2018 CISC124 - Prof. McLeod

11 Assigning Private Attributes - Constructors
Constructors are special methods that have the same name as the class in which they reside, but have no return type (not even void). They must be public. They are only executed once, when the object is being instantiated. You can’t invoke them later. Fall 2018 CISC124 - Prof. McLeod

12 Halloween2 Halloween2.java uses a constructor to set all parameters.
It also throws an exception if illegal values are encountered. See how this object is used in TestHalloween2.java. Note that this class is not complete – it only has the one constructor, for now. Fall 2018 CISC124 - Prof. McLeod

13 Aside - Information Privacy
Having private attributes is a good start. But is temperatures (an array of int) really private? Can I change the contents of the private array from outside the class? (How can I tell…?) How do I fix this? Fall 2018 CISC124 - Prof. McLeod

14 What Does .clone() Do? Suppose you want to make a completely separate copy of an int array called stuff: int[] clonedStuff = stuff.clone(); Or: int[] clonedStuff = new int[stuff.length]; for(int i = 0; i < stuff.length; i++) clonedStuff[i] = stuff[i]; Does the same thing. Fall 2018 CISC124 - Prof. McLeod

15 More on .clone() The only method that an array has.
But many other objects have a clone() method of their own. Ours should too! (In fact clone() is a member of Object, so all objects in Java inherit clone(), but this method may not do what you want for all objects.) Fall 2018 CISC124 - Prof. McLeod

16 Assigning Private Attributes - Constructors, Cont.
Note that once you have written a constructor with parameters, you can no longer use a constructor without any parameters, called the default constructor. If you want an empty constructor, you must write one yourself, in addition to the parameterized constructor(s). Why would you want an empty constructor? Fall 2018 CISC124 - Prof. McLeod

17 Aside – Preventing Instantiation
Provide only the default constructor and make it private. Pretty sneaky!! Used by the Math class, for example. There is no need to instantiate the Math class since all its methods and attributes are static. Fall 2018 CISC124 - Prof. McLeod

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 Accessors, Privacy Leaks, Cont.
Better: public int[] getTemperatures() { return temperatures.clone(); } // end getTemperature Accessor Fall 2018 CISC124 - Prof. McLeod

28 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

29 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

30 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

31 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


Download ppt "Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,"

Similar presentations


Ads by Google