CISC124 Quiz 1 marking nears completion!

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
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.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Topic: Classes and Objects
Creating and Using Objects, Exceptions, Strings
7. Inheritance and Polymorphism
objects CIS 421 Kutztown University
Chapter 3: Using Methods, Classes, and Objects
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
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…
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC101 Reminders Slides have changed from those posted last night…
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
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.
CISC124 Assignment 4 on Inheritance due today at 7pm.
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 3 due tomorrow at 7pm.
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC124 Labs start this week in JEFF 155. Fall 2018
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,
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 3 due next Friday.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Review of Previous Lesson
CMPE212 – Reminders Quiz 1 marking underway. Assignment 1 due today.
Review of Previous Lesson
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Object-Oriented PHP (1)
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due in a week.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Chapter 7 Objects and Classes
Presentation transcript:

CISC124 Quiz 1 marking nears completion! Fall 2018 CISC124 11/28/2018 CISC124 Quiz 1 marking nears completion! TAs are busy grading assn 1. Assn 2 due this Friday, the 12th. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

Today What is an Object? Start Encapsulation. Defining Exceptions. Fall 2018 CISC124 - Prof. McLeod

What Is An Object? A definition that would apply to any programming language: An entity that exists in an operating computer program that has: State Behaviour Identity Fall 2018 CISC124 - Prof. McLeod

What Is An Object?, Cont. The State of an Object is the collection of information held in that object. This information may change over time, as a result of operations carried out on the Object. The Behaviour is the collection of operations that an Object supports. The Identity of an Object allows the program access to a specific Object. Fall 2018 CISC124 - Prof. McLeod

What Is An Object?, Cont. An Object represents real or abstract entities. An Object representing a real entity, for example: The State is awake and curious. The Behavior is helping. The Identity is minx. An instance of Cat. Fall 2018 CISC124 - Prof. McLeod

What is a Class? You can have many (possibly infinite!) different Objects with different values for each State category (or attribute!). But if each of these Objects has the same set of possible behaviours then you can group these Objects together into a Class. For the example, the class would be “Cat”. ginger, felix, sylvester, belle, etc. are all Objects and are instances of Cat. Most (but not all) OO programming languages support the definition of classes. Fall 2018 CISC124 - Prof. McLeod

What is a Class?, Cont. A class is defined in the source code of a program. It defines: The operations that are allowed on instances of this class (the methods). The possible categories of state that are allowed for instances of this class (the attributes). Instances of a class are created when the program is running. Fall 2018 CISC124 - Prof. McLeod

What is a Class?, Cont. The State of an Object instance can be partly or completely defined when the instance is created. The State will likely change when operations are carried out on the instance. However, attributes cannot be added or removed and behavior cannot be added or removed. These are defined in the Class. Fall 2018 CISC124 - Prof. McLeod

Object Categories In a program, Objects will probably fall into one of these general categories: Tangible things (ex: Cat) Agents (StringTokenizer) Events and transactions (MouseEvent) Users and roles (Administrator) Systems (MailSystem) System interfaces and devices (File) Foundational classes (String) Fall 2018 CISC124 - Prof. McLeod

Object Extremes Two extremes of object structure: Fall 2013 CISC124 Object Extremes Two extremes of object structure: Utility classes: All static methods and attributes The Math class, for example. You do not instantiate these classes – there is no point. Customizable classes: All non-static methods and attributes. Attribute values (some or all) must be set at the time of instantiation before the class can be used. Scanner class for example. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

Objects, Cont. And many classes fall in-between these two extremes: A mix of static and non-static methods. static methods have nothing to do with the attributes and so can be used without instantiation of the class. Non-static methods depend on the attributes which must be set through instantiation. Wrapper classes for example: Double, Integer, etc. Fall 2018 CISC124 - Prof. McLeod

Encapsulation Encapsulation is the process of defining a Class that has at least one customizable attribute. Encapsulation is about the abstraction or containment of the attributes defined in the class. In Java, methods and attributes must be encapsulated or contained in a class definition. We have not yet built any classes with attributes that must be set upon instantiation. Time to start! Fall 2018 CISC124 - Prof. McLeod

Encapsulation, Cont. The Best Object Design: Supports the re-usability of code. Provides a well-defined interface to other objects and the user(s). Builds into an Object the code that ensures the integrity of the data stored in the Object by: Making sure that initial data values are “legal”. Controlling (or preventing) changes to data. Preventing “privacy leaks” when data is returned out of an Object. Works well with modular design practices making code easier to write, test and debug. Fall 2018 CISC124 - Prof. McLeod

Encapsulation, Cont. This is the driving principle for the design of all Objects in the Java API! So, maybe you should design your own Objects using the same principle? Your class Java API Fall 2018 CISC124 - Prof. McLeod

An Example Consider the general model (or “pattern”...) of a database: Database User Interface Collection record 1 Add Delete Edit Sort Find Save Load ... Edit Forms Queries Reports ... record 2 User  record 3 record 4 record 5 record 6 .... Each record is an instance of an Object, too. Fall 2018 CISC124 - Prof. McLeod

An Example, Cont. For now, we will focus on just how to define one of the many objects needed for the database – the object that will be used to define the record structure – a “Foundational” Object. If each record only held one piece of data, we would not need an object for each record – the Collection could just be an array. But – more typically – each database record will hold several pieces of data. Fall 2018 CISC124 - Prof. McLeod

An Example, Cont. 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”. Fall 2018 CISC124 - Prof. McLeod

An Example, Cont. What would you do without objects?: You could create four arrays, one for each of the fields. Difficult to code when moving records around. Suppose some record swap or delete is messed up and the records get out of sync – how would you know? a record: year number temp condition 2D array? Fall 2018 CISC124 - Prof. McLeod

An Example, Cont. It would be easier to design a Class that contains (or encapsulates) these four fields and then design another class to collect instances of this Class. No way the fields could get out of sync since moving a record moves all fields at once. Collection record 1 record 2 temp array year record 3 record 4 number record 5 condition record 6 .... Fall 2018 CISC124 - Prof. McLeod

Halloween1 Class public class Halloween1 { public int year; Fall 2013 CISC124 Halloween1 Class public class Halloween1 { public int year; public int numMunchkins; public int[] temperatures; public String weatherCondition; } // end Halloween1 We can define the State of an instance of Halloween1, but it does not yet have any Behavior (methods)! Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

Halloween1, Cont. In a method in some other class: Halloween1[] hwDB = new Halloween1[100]; hwDB[0] = new Halloween1(); hwDB[0].year = 1992; hwDB[0].numMunchkins = 200; int[] temp = {10, 9, 7, 6}; hwDB[0].temperatures = temp; hwDB[0].weatherCondition = "rain"; instantiation Identity define the State Fall 2018 CISC124 - Prof. McLeod

Why not declare the attributes in Halloween1 static? Halloween1, Cont. A question: Why not declare the attributes in Halloween1 static? Every instance of a class shares static members – there is only one copy of each static member in memory. If the attributes were static then each instance of Halloween1 would have the same values! Fall 2018 CISC124 - Prof. McLeod

Halloween1, Cont. Another question: What is wrong with the following?: hwDB[1] = new Halloween1(); hwDB[1].year = 3011; hwDB[1].numMunchkins = -200; int[] temp = {100, 200}; hwDB[1].temperature = temp; hwDB[1].weatherCondition = "frogs"; Fall 2018 CISC124 - Prof. McLeod

Halloween1, Cont. The object, hwDB[1] is not particularly realistic. How can we prevent the creation of an object like this? Who is responsible for checking for realistic, or “legal” values for the attributes? The Halloween1 class or the class that uses it? Where is it easiest to put these checks? Fall 2018 CISC124 - 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 Fall 2018 CISC124 - 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. Fall 2018 CISC124 - 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. (Please ignore indentation and line wrapping…) Fall 2018 CISC124 - 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 Fall 2018 CISC124 - 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, you need this annotation: … throws IllegalHalloweenException { Fall 2018 CISC124 - 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. For now, just follow the pattern. Fall 2018 CISC124 - 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? Fall 2018 CISC124 - 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. Fall 2018 CISC124 - Prof. McLeod