Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Inheritance Inheritance Reserved word protected Reserved word super
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Road Map Introduction to object oriented programming. Classes
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Dictionaries Chapter 17 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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.
Classes, Interfaces and Packages
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
More About Objects and Methods
Creating Your Own Classes
3 Introduction to Classes and Objects.
More About Objects and Methods
Chapter 3: Using Methods, Classes, and Objects
CSC240 Computer Science III
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
Slides by Steve Armstrong LeTourneau University Longview, TX
Slides by Steve Armstrong LeTourneau University Longview, TX
Java Classes and Objects 3rd Lecture
Defining Classes and Methods
More About Objects and Methods
Object Oriented Programming in java
Classes, Objects and Methods
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Chapter Contents Introduction Objects and Classes Using the Methods in a Java Class  References and Aliases Defining a Java Class Method Definitions  Arguments and Parameters  Passing Arguments  A Definition of the Class Name  Constructors  The Method toString ...

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Chapter Contents... Method Definitions – ctd.  Methods That Call Other Methods  Methods That Return an Instance of Their Class  Static Fields and Methods  Overloading Methods Enumeration as a Class Packages  The Java Class Library

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Organizing Our Lives For each of the above examples, consider how the objects are organized

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Organizing Computer Data Computer stores/organizes items in similar manners as the examples Ways of organizing data are represented by Abstract Data Types (ADTs) An ADT specifies  data that is stored  operations that can be done on the data

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X ADT Terminology Data structure: implementation of an ADT within a programming language Collection: an ADT that contains a group of objects Container: a class that implements the collection These last two terms are sometimes used interchangeably

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Types of ADTs Bag  Unordered collection, may contain duplicates List  A collection that numbers its items Stack  Orders items chronologically  Last In, First out Queue  Orders items chronologically  First in, First out Match each of these to the pictures ? Click here to return to pictures

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Types of ADTs Dictionary  Pairs of items – one is a key  Can be sorted or not Tree  Arranged in a hierarchy Graph  Generalization of a tree Match each of these to the pictures ? Click here to return to pictures

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Objects and Classes 1 An object is a program construct  Contains data  Performs actions Objects interact to solve problems Actions performed by objects are defined by methods

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Objects and Classes A class is a kind of object A class definition is a general description of  what the object is  what it can do

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Objects and Classes All objects in the same class have  the same kinds of data  the same methods

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Using the Methods in a Java Class 2 Given a class called Name  Declare a variable Name joe;  Create an instance of Name joe = new Name();  Alternatively Name joe = new Name();

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X void methods are used to do a task such as set the first or last names valued methods return a single value Using the Methods in a Java Class 3

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X References and Aliases 4 Primitive data types  byte  short  int  long All other data types are reference or class types A reference variable contains address of (reference to) location in memory of an object  float  double  char  boolean

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X References and Aliases Consider the results of the code below:

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Defining a Java Class 5 Data members Access or visibility modifiers Specifies where a class, data field, or method can be used. Access or visibility modifiers Specifies where a class, data field, or method can be used.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Defining a Java Class Methods that classes often use: Accessor (query) methods – return value of a data field Mutator methods – change the value of a data field Methods that classes often use: Accessor (query) methods – return value of a data field Mutator methods – change the value of a data field

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Method Definitions 7 General form of method definition Examples

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Method Definitions 10 Note incorrect, ambiguous use of identifier first Solvable by use of this  this.first refers to data member  Note: possible but not typical public void setFirst(String first) { first = first; } // end setFirst public void setFirst(String first) { first = first; } // end setFirst data object first parameter first public void setFirst(String first) { this.first = first; } // end setFirst public void setFirst(String first) { this.first = first; } // end setFirst

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Arguments and Parameters 12 Consider statements: Arguments/parameters in call match in number and type to formal parameters in definition Name joe = new Name(); joe.setFirst("Joseph"); joe.setLast("Brown"); Name joe = new Name(); joe.setFirst("Joseph"); joe.setLast("Brown"); public void setFirst(String firstName) { first = firstName; } // end setFirst public void setFirst(String firstName) { first = firstName; } // end setFirst

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Passing Arguments 13 When formal parameter is primitive type  parameter in method initialized by value  can be constant or variable public void setMiddleInitial(char middleInitial) { initial = middleInitial; } // end setMiddleInitial public void setMiddleInitial(char middleInitial) { initial = middleInitial; } // end setMiddleInitial joe.setMiddleInitial('Q');

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Passing Arguments 14 When a formal parameter has a class type  Formal parameter initialized with memory address of object passed to it. public void giveLastNameTo(Name child) { child.setLast(last); } // end giveLastNameTo public void giveLastNameTo(Name child) { child.setLast(last); } // end giveLastNameTo jamie.giveLastNameTo(jane);

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Passing Arguments However, a method cannot replace an object passed to it as an argument public void giveLastNameTo2(Name child) { String firstName = child.getFirst(); child = new Name(); child.setFirst(firstName); child.setLast(last); } // end giveLastNameTo2 public void giveLastNameTo2(Name child) { String firstName = child.getFirst(); child = new Name(); child.setFirst(firstName); child.setLast(last); } // end giveLastNameTo2 child is considered local. It will disappear when the method finishes child is considered local. It will disappear when the method finishes

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X A Definition of the Class Name 16 View definition of full classView definition Note  Constructors  set methods – mutators  get methods – accessors  toString method Note demonstration programdemonstration program

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Constructors Tasks of a constructor  Allocate memory for object  Initialize data fields Properties  Same name as class  No return type (not even void )  Can have any number of parameters (including no parameters)  Note constructors of Nameconstructors of Name

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X The Method toString 21 Note the toString method of class Name toString method  Returns a string with value of person's name For any class, toString method invoked automatically for command System.out.println (someObject);

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Methods That Call Other Methods 22 Note setName method in class Name setName method  Invokes setFirst and setLast  setName invokes them without preceding the method name with object variable and dot Consider the getName method  Calls toString  Thus both methods always give same result

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Methods That Return an Instance of Their Class 26 Consider a different version of setName The return this; returns a reference to the invoking object. public Name setName(String firstName, String lastName) { setFirst(firstName); setLast(lastName); return this; } // end setName public Name setName(String firstName, String lastName) { setFirst(firstName); setLast(lastName); return this; } // end setName

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Static Fields and Methods 27 A static data field does not belong to any one object  Also called a class variable  Only one instance of the variable exists for all instances of the class Note that a static data field is not a constant (final)

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Static Fields and Methods All instances of the class reference that one variable

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Static Fields and Methods Consider the need of a method that does not belong to an object of any type Examples  A method to find the max or min of two or more numbers  A square root method

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Static Fields and Methods When specified static, a method is still a member of the class  However, does not need an object as a prefix to the call Call with the name of the class int maximum = Math.max(2, 3); double root = Math.sqrt(4.2); int maximum = Math.max(2, 3); double root = Math.sqrt(4.2);

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Overloading Methods 29 Multiple methods within the same class can have the same name Java distinguishes them by noting the parameters  Different numbers of parameters  Different types of parameters This is called the signature of the method

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Enumeration as a Class 30 When an enumeration is defined  A class is created  Default methods include toString equals ordinal valueOf Also possible to provide additional methods

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Enumeration as a Class Consider the following example of an enumeration of card suits View definition of class SuitView definition  Note constructor, getColor View demonstration programdemonstration program

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Enumeration as a Class 33 View class LetterGradeclass LetterGrade  An enumeration class  Note class elements Consider the following code LetterGrade myGrade = LetterGrade.B_PLUS; Discuss the results of calls to various LetterGrade methods

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Packages 34 Packages enable grouping together multiple related classes Specify a class to be part of a package with first line package myStuff; Place all classes in same directory which is named with the name of the package In your program which uses the package import myStuff.*;

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X The Java Class Library 35 The Java language has many classes defined  Recall the Math class with max and sqrt Collection known as Java Class Library or Java Application Programming Interface