Fall 2018 CISC124 12/3/2018 CISC124 Email or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 5 Last time: –Method Overloading –Arrays –A few useful classes in Java: System Wrapper classes String.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Inheritance and Access Control CS 162 (Summer 2009)
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
CISC124 - Notices Assn 2 due this Friday. You already know what you need to do this assignment: –Another procedural program. –Uses text file I/O (see Exercise.
OOP: Encapsulation &Abstraction
CISC/CMPE320 - Prof. McLeod
Winter 2018 CMPE212 9/18/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Classes and Objects 2nd Lecture
Object Oriented Programming
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Quiz 1 marking nears completion!
CMPE212 – Stuff… Assn 2 due this Friday. Winter 2018
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Building Java Programs
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 3 due tomorrow at 7pm.
Defining Classes and Methods
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
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,
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
CISC101 Reminders All assignments are now posted.
CMPE212 – Reminders Assignment 2 sample solution is posted.
CMPE212 – Reminders Assignment 3 due next Friday.
Interfaces.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
Last Time Some discussion of program “efficiency”. Arrays – 1D and 2D
CMPE212 – Reminders Quiz 1 marking underway. Assignment 1 due today.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due in a week.
Classes and Objects Reusing Classes with Composition
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Review for Midterm 3.
CS 240 – Advanced Programming Concepts
Presentation transcript:

Fall 2018 CISC124 12/3/2018 CISC124 Email or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

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

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

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

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

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

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

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

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

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

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

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

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

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

toString() Method As it is, if we try to print a Halloween object, we will just get “gobbldeygook”: Halloween2@923e30 (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

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

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

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

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

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

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

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

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

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