CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 5 Last time: –Method Overloading –Arrays –A few useful classes in Java: System Wrapper classes String.
4.1 Instance Variables, Constructors, and Methods.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
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.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Written by: Dr. JJ Shepherd
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
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 Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
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.
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.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Inheritance and Polymorphism
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
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
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.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
Inheritance Basics Programming with Inheritance
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 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Quiz 1 marking nears completion!
slides created by Ethan Apter
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.
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.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Computer Programming with JAVA
slides created by Ethan Apter
CISC/CMPE320 - Prof. McLeod
Barb Ericson Georgia Institute of Technology Oct 2005
CISC124 Assignment 4 on Inheritance due next Friday.
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.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 sample solution is posted.
CMPE212 – Reminders Assignment 3 due next Friday.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
CMPE212 – Reminders Quiz 1 marking underway. Assignment 1 due today.
Review of Previous Lesson
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due in a week.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
slides created by Ethan Apter and Marty Stepp
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
CS 240 – Advanced Programming Concepts
Presentation transcript:

CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday. Winter 2019 CMPE212 4/26/2019 CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Today Encapsulation, Cont. Winter 2019 CMPE212 - Prof. McLeod 4/26/2019 Today Encapsulation, Cont. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Last Time: The Halloween Class Suppose you want to create a database to store records of Halloween visits. (Maybe we are going to build a model to predict the number of visitors for given weather conditions so we can predict how much candy to buy!) For each year, you wish to record: The year. Number of munchkins banging at your door. Outdoor temperatures (one reading per hour) in deg C. Weather condition – “rain”, “snow”, or “clear”. Winter 2019 CMPE212 - Prof. McLeod

Encapsulation, Cont. Attributes must be declared private, so that the class that owns them can control how they are set. If attributes are private then how can a user of the class assign the values of the attributes? Through methods, of course! Constructor(s) Mutator(s) when instantiated only anytime after instantiation Winter 2019 CMPE212 - Prof. McLeod

Encapsulation, Cont. Within these methods, you can write code to check the parameters for validity. What do you do if the values are not legal? Throw an exception! We need to start adding Behaviour to our Class. Winter 2019 CMPE212 - Prof. McLeod

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. Winter 2019 CMPE212 - Prof. McLeod

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 Winter 2019 CMPE212 - Prof. McLeod

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: … throws IllegalHalloweenException { This part of the method header is called a “decoration”. Winter 2019 CMPE212 - Prof. McLeod

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 or inheritance. For now, just follow the pattern. Winter 2019 CMPE212 - Prof. McLeod

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. Constructors are often overloaded. This means you can have many constructors with different parameter lists. Why do that? Winter 2019 CMPE212 - Prof. McLeod

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. Winter 2019 CMPE212 - Prof. McLeod

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? Winter 2019 CMPE212 - Prof. McLeod

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. Winter 2019 CMPE212 - Prof. McLeod

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.) Winter 2019 CMPE212 - Prof. McLeod

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? Winter 2019 CMPE212 - Prof. McLeod

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. Winter 2019 CMPE212 - 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). Winter 2019 CMPE212 - 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? Winter 2019 CMPE212 - 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? Winter 2019 CMPE212 - 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? Winter 2019 CMPE212 - 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. this means “myself”. Another use – suppose I used the same attribute name as a constructor parameter – year, for example. In the constructor, I would need to say: this.year = year; Winter 2019 CMPE212 - 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? Winter 2019 CMPE212 - 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 Winter 2019 CMPE212 - 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. Winter 2019 CMPE212 - 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 Winter 2019 CMPE212 - Prof. McLeod

Accessors, Privacy Leaks, Cont. Better: public int[] getTemperatures() { return temperatures.clone(); } // end getTemperature Accessor Winter 2019 CMPE212 - 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. Winter 2019 CMPE212 - 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. Winter 2019 CMPE212 - 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. Winter 2019 CMPE212 - 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. Winter 2019 CMPE212 - Prof. McLeod