DOC PREVIEW
UMD CMSC 131 - Lecture 9: Objects and Classes in Java

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 9 Objects and Classes in Java Note This set of lecture slides will most likely take more than one day to complete and it is closely tied to a Student class and driver developed during the lecture Last time 1 Pseudo code from last lecture 2 Objects and classes 3 Heaps 4 Garbage Collection 5 Aliasing 6 Object equality Today 1 2 3 4 5 More about Creating Objects and classes in Java Methods Constructors Accessors Mutators Equality CMSC 131 Spring 2007 Printing an object Jan Plane adapted from Bonnie Dorr Classes in Java Class declarations have the following form in Java Visibility modifier more later in class class keyword class name public class Student class body instance variables methods When you create a class in Eclipse it generates this template for you CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 1 Anatomy of an Instance Variable Declaration Visibility modifier Normal variable ceclaration public int IDNum CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 2 Method Declarations in Java Methods are operations like etc They can take inputs They can return values They can modify instance variables The form varies slightly depending on whether values are returned or not CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 3 Anatomy of a Method Declaration 1 for methods that do not return values Visibility modifier void keyword method name parameter list public void acceptTokens int tokensPassedIn tokenLevel tokenLevel tokensPassedIn body CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 4 void return type Parameter List If a method does not return a value use the void keyword The parameter list describes the form of inputs type name for use in body Parameter lists may be empty Multiple parameters are separated by CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 5 Anatomy of a Method Declaration 2 for methods that return values Visibility modifier return type method name parameter list public int lastFour return id 10000 body CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 6 Return Type Methods that return values must specify the type of the value to be returned The bodies of these methods use return to indicate when a value is to be returned The value being returned must have the same type as the return type CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 7 Object Creation Once a class is defined objects based on that class can be created using new new Student To assign an object to a variable the variable s type must be the class of the object Student s new Student Each object has its own copies of all the instance variables in the class except for certain kinds we ll study later Instance variables and methods in an object can be accessed using s IDNum 123456789 s setIDNum 123456789 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 8 Constructors Special methods in class definitions to specify how objects are created Form of a constructor definition Student String nameDesired int IDDesired int tokensDesired name nameDesired id IDdesired tokenLevel tokensDesired Can have more than one constructor provided argument lists are different Student int IDDesired id IDDesired Java includes default constructor no arguments which you can redefine override Student tokenLevel 3 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 9 Equality Testing Need to defined what it means for two students to be equal You can define it however you like public boolean equals Student otherStudent return id otherStudent id CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 10 Objects to Strings What happens if we try to print a Student object invoke println using a Student object as an argument Student s1 new Student System out println s1 Something like this prints Student 82ba41 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 11 Java Knows How To Print Any Object Why Every class has a default toString method toString converts objects into strings System out println calls this method to print an object Default object type and address toString can be overridden The method for converting Students to strings public String toString return name id CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 12 Static Data Members and Static Methods Not contained in or associated with an object of that type Accessed by the ClassName variableName or by ClassName methodName rather than by objectName variableName or by objectName methodName CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 13 for Loops Three kinds of loops in Java while do while for A common programming idiom int i 0 while i 10 j i i Equivalent for loop for int i 0 i 10 i j i CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 14 for Loop Form for init continue increment stmt Equivalent to init while continue stmt increment Any of the three init continue increment components may be omitted for Runs forever CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 15 Comments allow you to insert explanations in your code Two form blah blah blah blah blah blah Every Class declaration Instance variable declaration Method declaration must have a comment explaining its purpose CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 16 Set Get Methods We have been using to modify instance variables and accessing variables directly to read values Generally this is not good practice because it imposes restrictions on class implementation Better set methods to set values mutators get methods to read values accessors CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 17 Set Methods Mutators public void setID int newID id newID Can also do consistency checking public void setTokenLevel int newTokenLevel if newTokenLevel 3 tokenLevel newMonth else System out println Bad argument to setTokenLevel newTokenLevel CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 18 Get Methods Accessors Sole purpose is to return values of state public int getID return id Why use them The state information may not always be stored in a single instance variable since implementations may change You give designers option of changing instance variables CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 19


View Full Document

UMD CMSC 131 - Lecture 9: Objects and Classes in Java

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 9: Objects and Classes in Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 9: Objects and Classes in Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 9: Objects and Classes in Java and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?