Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 12.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 8.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Road Map Introduction to object oriented programming. Classes
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
CSC 142 Computer Science II Zhen Jiang West Chester University
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
Object Oriented Programming
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
CompSci 230 S Programming Techniques
Examples of Classes & Objects
Agenda Warmup AP Exam Review: Litvin A2
Object Oriented Programming using Java - Class Instance Variables
Chapter 3: Using Methods, Classes, and Objects
CS 302 Week 11 Jim Williams, PhD.
The University of Texas Rio Grande Valley
CSC240 Computer Science III
Object Based Programming
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
IFS410: Advanced Analysis and Design
Classes & Objects: Examples
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Implementing Non-Static Features
Object Oriented Programming
Packages From Deitel & Deitel.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look UTPA – Fall 2012 This set of slides is revised from lecture.
CS360 Client/Server Programming Using Java
Method of Classes Chapter 7, page 155 Lecture /4/6.
CIS 199 Final Review.
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
SPL – PS3 C++ Classes.
Presentation transcript:

Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12

Note Set 12 Overview Objects in depth Interface of a class Review of Access to members the this reference Overloaded Constructors Default and No Arg constructors

Time Class public class Time1 { private int hour; private int minute; private int second; public void setTime(int h, int m, int s) { hour = ((h >= 0 && h < 24) ? h : 0); minute = ((m >= 0 && m < 60) ? m : 0); second = ((s >= 0 && s < 60) ? m : 0); } public String toUniversalString() { return String.format(“%02d:%02d:%02d”, hour, minute, second); } public String toString () { return String.format(“%d:%02d:%02d %s”, ((hour == 0 || hour == 12) ? 12 : hour % 12 ), minute, second, (hour < 12 ? “AM” : “PM”) ); } Public Interface Time is a class. Objects of type Time provide a service. The services are indicated by the public interface

Time Class public class Time1 { private int hour; private int minute; private int second; public void setTime(int h, int m, int s) { hour = ((h >= 0 && h < 24) ? h : 0); minute = ((m >= 0 && m < 60) ? m : 0); second = ((s >= 0 && s < 60) ? m : 0); } public String toUniversalString() { return String.format(“%02d:%02d:%02d”, hour, minute, second); } public String toString () { return String.format(“%d:%02d:%02d %s”, ((hour == 0 || hour == 12) ? 12 : hour % 12 ), minute, second, (hour < 12 ? “AM” : “PM”) ); } methods that modify private instance variables should perform error checking – keeps object in consistent state more elegant error checking to come methods that modify private instance variables should perform error checking – keeps object in consistent state more elegant error checking to come

Time Class public class Time1 { private int hour; private int minute; private int second; public void setTime(int h, int m, int s) { hour = ((h >= 0 && h < 24) ? h : 0); minute = ((m >= 0 && m < 60) ? m : 0); second = ((s >= 0 && s < 60) ? m : 0); } public String toUniversalString() { return String.format(“%02d:%02d:%02d”, hour, minute, second); } public String toString () { return String.format(“%d:%02d:%02d %s”, ((hour == 0 || hour == 12) ? 12 : hour % 12 ), minute, second, (hour < 12 ? “AM” : “PM”) ); } Every object has a toString() method (implicitly or explicity) Called implicitly whenever a Time object needs to be converted to a string, such as in a printf. Every object has a toString() method (implicitly or explicity) Called implicitly whenever a Time object needs to be converted to a string, such as in a printf.

Time1Test public class Time1Test { public static void main (String [] args) { Time1 time = new Time1(); time.setTime(13, 27,6); System.out.print(“Initial Universal Time: “); System.out.println(time.toUniversalString()); System.out.print(“Initial Standard Time: “); System.out.println(time.toString()); System.out.println(); time.setTime(99, 99, 99); System.out.print(“Universal Time: “); System.out.println(time.toUniversalString()); System.out.print(“Standard Time: “); System.out.println(time.toString()); } Initial Universal Time: 13:27:06 Initial Standard Time: 1:27:06 PM Universal Time: 00:00:00 Standard Time: 12:00:00 AM Initial Universal Time: 13:27:06 Initial Standard Time: 1:27:06 PM Universal Time: 00:00:00 Standard Time: 12:00:00 AM

Time1Test public class Time1Test { public static void main (String [] args) { Time1 time = new Time1(); time.setTime(99, 99, 99); System.out.print(“Universal Time: “); System.out.println(time.toUniversalString()); System.out.print(“Standard Time: “); System.out.println(time.toString()); } Classes simplify the programming here. We don’t care how this happens, we just know it does. Details are abstracted away in the object’s implementation Clients usually care about what the class does but not how it does it Clients are not affected by a change in implementation. Interfaces change less frequently than implementation You might find a more efficient way of doing this or that later, but you don’t want to break code that depends on the fact that you can do this or that in some wha

Time1Test public class Time1Test { public static void main (String [] args) { Time1 time = new Time1(); time.hour = 7; time.minute = 15; time.second = 30; } Illegal – cannot access the private data members directly. Must go through the interface methods that are supplied by the class.

The this object reference Every object can access a reference to itself with keyword this. When a non-static method is called, the calling object is passed implicitly to the method Time t = new Time(); t.setTime( 12, 13, 14); System.out.println( t.toString() ); t is passed to the toString method implicitly. in toString, it can be refferenced through the this reference variable.

this public void setTime(int h, int m, int s) { hour = ((h >= 0 && h < 24) ? h : 0); minute = ((m >= 0 && m < 60) ? m : 0); second = ((s >= 0 && s < 60) ? m : 0); } How can these variables be accessed if they have no declaration? They were declared when the object that called this method was instantiated. They are referring to those variables that are part of the invoking object. public void setTime(int h, int m, int s) { this.hour = ((h >= 0 && h < 24) ? h : 0); this.minute = ((m >= 0 && m < 60) ? m : 0); this.second = ((s >= 0 && s < 60) ? m : 0); } Equivalent

Using this to avoid shadowing public class Test { private int a, b, c; public void foo(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } Accesses the instance data members instead of the parameters.

Overloaded constructors public class Test { private int a; public Test() { this(0); } public Test(int x) { a = x; } Call another constructor using the this reference. Calling another constructor with this must be the 1 st line in the body of the constructor Concentrate init logic in one method See Fig 8.5 – Time2.java

Special constructor public class Time2 { //Declarations public Time2 (Time2 time){ this(time.getHour(), time.getMinute(), time.getSecond()); } }

Accessing sets and gets in the class Reusability – concentrate logic to set value and get value in one area – the accessors and mutators. Consider if time was represented as an int holding number of seconds since midnight rather than 3 ints. if you have many time objects, this can be a space saver – from 12 bytes down to 4. If all conversion is done in set and get, then don’t have to worry about modifying the same logic twice (or getting it wrong twice).

Set and Get vs. public data Why not just make the instance variables public?