CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 8.
Road Map Introduction to object oriented programming. Classes
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look - modified by Eileen Kraemer.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Object Oriented Software Development
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object Oriented Programming
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
Classes, Interfaces and Packages
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
CompSci 230 S Programming Techniques
Object-Oriented Concepts
Classes and Objects: A Deeper Look
Object-Oriented Programming: Classes and Objects
Object Oriented Programming using Java - Class Instance Variables
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Object-Oriented Programming: Classes and Objects
The University of Texas Rio Grande Valley
Object Based Programming
Object-Oriented Programming: Polymorphism
IFS410: Advanced Analysis and Design
د.سناء الصايغ الفصل الأول البرمجة الشيئية
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
CIS 199 Final Review.
Chapter 8 Classes and Objects: A Deeper Look
Chapter 7 Objects and Classes
Chapter 5 Classes.
Presentation transcript:

CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look

Time Class Case Study Two classes: Time1 (Fig 8.1) and Time1Test (Fig. 8.2) Time1 has three private instance variables and three public methods (a.k.a., public services or the public __________). Time1 has a default ___________ that sets instance variables to 0. Note the code hour = ((h>=0 && h<24) ? h:0); It means if(h>=0 && h<24) hour=h; else hour=0; The Time1 object will always have __________ data (always in range), but not necessarily correct data. interface constructor consistent

Time Class Case Study Note the code String.format(“%02d:%02d:%02d", hour, minute, second); in the Time1 class. format is a method of the __________ class The %02d means “format is leading 0, two decimal integers” (this formatted output is new in J2SE 5.0) The three formats (in “” ) will be applied to hour, minute, second Likewise, note the code String.format( "%d:%02d:%02d %s", ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ), minute, second, ( hour < 12 ? "AM" : "PM" ) ); The hour % 12 means hour ______ 12 String mod

Time Class Case Study Note Time1 time = new Time1(); in the Time1Test class. Variable time is assigned the address of a ________ object ( time is a reference variable). The code System.out.println( time.toUniversalString() ); prints the string returned when the object time calls the method toUniversalString() in the class _________. The code time.setTime( 13, 27, 6 ); is the object time calling the setTime method in Time1. The class MemberAccessTest in Fig. 8.3 shows that a client cannot directly access _________ members. Time1 private

5 8.4 The this Reference Note the file ThisTest.java in Fig ThisTest.java has two classes (but only one can be public, which contains the ________ method here). Compiling ThisTest.java will create two _______ files. Parameter names for a constructor should NOT be the same as the instance variable (______) names. Variables in a method with the same names as fields will shadow (hide) the fields. In that case, you will need to refer to fields with the keyword _______. Accessing members within the same class does not require the this reference (see Line 35). main.class field this

6 8.5 Overloaded Constructors Note class Time2 in Fig A class can have several (__________) constructors if the ________ (parameter list) of each is different. public Time2() is a no-__________ constructor. this can be used as the first statement in a constructor’s body to call another constructor. This use of this allows for code ______ with less maintenance if a constructor needs to be changed. The methods setTime, toUniversalString, and toString use set and get methods instead of accessing instance variables directly. overloaded signature argument reuse

7 8.6 Default and No-Argument Constructors If a class has no constructors, the compiler creates a default constructor that sets numeric fields to 0, Boolean fields to false, and reference fields to ____. If a class has constructors, the compiler will not create a default constructor. The class will need a no-argument constructor. A no-argument constructor with no _____ will set fields to their default values. A constructor is identified by Java because it has the same name as the class and has no _______ type. null body return

8 8.7 Set and Get Methods Classes provide public methods to allow clients to set and get ________ instance variables. Set and get methods should only be used as needed. Set methods are also called mutator methods and get methods are called _________ methods. Set and get methods make private fields accessible, but not _______. Get methods can hide the actual data format from the client when they return formatted data. Set methods can protect fields from careless attempts to modify values by using ______ checking. private accessor public validity

9 8.8 Composition A data member of a class can be a reference to an ______ of another class. This is known as composition or a _____ relationship. Note that the Employee class in Fig. 8.8 uses composition. Employee has two Date objects as instance variables. In UML (Unified _________ Language), composition would be expressed as follows: EmployeeDate object has-a Modeling

Enumerations An enumeration is a special kind of class which declares a set of _________. enum types are final and __________. Do not try to create an object of type enum. An enum can have instance variables, constructors, and methods, much like an ordinary class. Enhanced for loop (new in J2SE 5.0) int scores[] = { 87, 93, 92, 95, 88, 90 }; int var = 0; for(int var : scores) // var holds each array value totalScore += var; See Fig and Fig for example of enum. constants static

static Class Members A static field (class variable) is shared by all objects. Example: numOfItems in a class called Item : public static int numberOfItems = 0; In the constructor Item, include the code numberOfItems++; A public static field can be directly accessed by an object or with the _______ name: anItem.numberOfItems Item.numberOfItems A ________ static field can be accessed by calling a public static method using the class name: Item.aMethod() See Fig and Fig for examples. class private

static Import and final Instance Variables static Import By using code like import static java.lang.Math.*;, you can access the static methods of a class without using the class name. For example, sqrt(900.0) instead of ______.sqrt(900.0) final Instance Variables Principle of _______ Privilege: code should be granted only the amount of privilege and access that it needs. Instance variables can be made final —they can only be set in a constructor and cannot be changed otherwise. private final int INCREMENT; Math Least

Some OO Concepts Software reuse Java is a language, but it is also a ___________ for accessing the Java API (thousands of prewritten classes) Software can be created by using prewritten components Data abstraction and encapsulation Information ______: classes hide their implementation from clients Data abstraction: clients focus on data, not on implementation details An __________ data type (ADT) has data representation and operations that manipulate the data A class is an ADT—it has data and operations framework hiding abstract