User-Defined Classes and ADTs

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Data Structures Using Java1 Chapter 1 Software Engineering Principles and Java Classes.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 11: Classes and Data Abstraction.
Chapter 8 User-Defined Classes and ADTs. Chapter Objectives Learn about classes Learn about private, protected, public, and static members of a class.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
Chapter 11: Classes and Data Abstraction
Software Engineering Principles and C++ Classes
UML Class Diagram: class Rectangle
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Chapter 8: User-Defined Classes and ADTs
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
© 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)
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 12: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Data Structures Using Java1 Chapter 1 Software Engineering Principles and Java Classes.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
1 CS 132 Spring 2008 Chapter 1 Software Engineering Principles and C++ Classes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming: Guided Learning with Early Objects
Creating Your Own Classes
Java Programming: Guided Learning with Early Objects
Chapter 11: Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Java Programming: From Problem Analysis to Program Design,
UML Class Diagram: class Rectangle
User-Defined Classes and ADTs
Object Based Programming
Defining Your Own Classes
CSC 113: Computer programming II
Classes & Objects: Examples
Chapter 8: User-Defined Classes and ADTs
CSE 1030: Implementing GUI Mark Shtern.
Defining Classes and Methods
Chapter 8 Classes User-Defined Classes and ADTs
Method of Classes Chapter 7, page 155 Lecture /4/6.
Presentation transcript:

User-Defined Classes and ADTs Chapter 8 User-Defined Classes and ADTs

Chapter Objectives Learn about classes Learn about private, protected, public, and static members of a class Explore how classes are implemented Learn about the various operations on classes

Chapter Objectives Examine constructors and finalizers Examine the method toString Learn about the abstract data type (ADT)

Classes class: reserved word; collection of a fixed number of components Components: members of a class Members accessed by name Class categories/modifiers private protected public

Classes private: members of class are not accessible outside class public: members of class are accessible outside class Class members: can be methods or variables Variable members declared like any other variables

Syntax The general syntax for defining a class is: modifier(s) class ClassIdentifier modifier(s) { classMembers } The general syntax for using the operator new is: new className() //Line 1 OR new className(argument1, argument2, ..., argumentN) //Line 2

Classes The syntax to access a data member of a class object or method is: referenceVariableName.memberName Shallow copying: two or more reference variables of the same type point to the same object Deep copying: each reference variable refers to its own object

Constructors Guarantee that data members are initialized when object is declared Automatically execute when class object created Name of constructor is name of class More than one constructor can be present in one class Default constructor: constructor without parameters

The Copy Constructor Executes when an object is instantiated Initialized using an existing object Syntax: public ClassName(ClassName otherObject)

UML Diagram

UML Diagram Top box: name of class Middle box: data members and their data types Bottom box: member methods’ names, parameter list, return type of method + means public method - means private method # means protected method

Example: class Clock myClock = new Clock(); yourClock = new Clock(9,35,15);

Example: class Clock

Example: class Clock

The Method toString public value-returning method Takes no parameters Returns address of String object Output using print and println methods Default definition creates String with name of object’s class name followed by hash code of object

The Modifier static In the method heading, specifies that the method can be invoked by using the name of the class If used to declare data member, data member invoked by using the class name Static data members of class exist even when no object of class type instantiated Static variables are initialized to their default values

static Data Members of a Class Illustrate illusObject1 = new Illustrate(3); Illustrate illusObject2 = new Illustrate(5);

Finalizers Automatically execute when class object goes out of scope Have no parameters Only one finalizer per class Name of finalizer: finalize

Creating Packages You can create packages using reserved word package Define the class to be public (If class is not public it can only be used within package) Choose name for package Organize package (create subdirectories)

Creating Package for class Clock package jpfpatpd.ch08.clockPackage; public class Clock { //put instance variables and methods, as before, here } import jpfpatpd.ch08.clockPackage.Clock;

The Reference this Refers to instance variables and methods of a class Used to implement cascaded method calls

Inner Classes Defined within other classes Can be either a complete class definition or anonymous inner class definition Used to handle events

Abstract Data Types Definition: A data type that specifies the logical properties without the implementation details

Programming Example: Candy Machine (Problem Statement) A new candy machine is bought for the gym, but it is not working properly. The machine sells candies, chips, gum, and cookies. In this programming example, we write a program to create a Java application program for this candy machine so that it can be put into operation. We divide this program in two parts. In the first part we design a non-GUI application program. In the second part we design an application program that will create a GUI as described in the second part. The non-GUI application program should do the following: 1. Show the customer the different products sold by the candy machine. 2. Let the customer make the selection. 3. Show the customer the cost of the item selected. 4. Accept money from the customer. 5. Release the item.

Programming Example: Candy Machine (Input and Output) Input: The item selection and the cost of the item Output: The selected item

Programming Example: Candy Machine Components Cash Register Dispenser Machine

Programming Example: Candy Machine

Programming Example: Candy Machine

Programming Example: Candy Machine

Programming Example: Candy Machine

Programming Example: Candy Machine

Programming Example: Candy Machine

Chapter Summary Creating classes Members of a class private protected public Static Implementing classes Various operations on classes

Chapter Summary Constructors Finalizers Method toString Abstract Data Types