DOC PREVIEW
Penn CIT 591 - Starting Classes and Methods

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

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

Unformatted text preview:

Starting Classes and MethodsObjects have behaviorsObjects have stateExample: a “Rabbit” objectClassesDefining a classDefining fieldsDefining constructorsParametersExample constructorExample use of a constructorA problem with namesSlide 13Slide 14this to the rescueA problem with names—solved!A typical use of thisDefining a methodReturning a result from a methodReturning no result from a methodSending messagesPutting it all togetherUsing our new classnullClasses form a hierarchyWhat is the class hierarchy for?Example of inheritanceClass variables and methodsWhy have class variables and methods?Example use of a class variableVocabulary IVocabulary IIVocabulary IIIVocabulary IVThe EndJan 14, 2019Starting Classes and MethodsObjects have behaviorsIn old style programming, you had:data, which was completely passivefunctions, which could manipulate any dataIn O-O programming, an object contains both data and methods that manipulate that dataAn object is active, not passive; it does thingsAn object is responsible for its own dataBut it can expose that data to other objectsObjects have stateAn object contains dataThe data represent the state of the objectData can also describe the relationship of the object to other objectsExample: a checkingAccount might haveA balance (the internal state of the account)An owner (some object representing a person)An accountNumber (used as an ID number)Example: a “Rabbit” objectYou could create an object representing a rabbitIt would have data:How hungry it isHow healthy it isWhere it isAnd methods:eat, run, dig, hideClassesA class describes a set of objectsThe objects are called instances of the classA class describes:Fields that hold the data for each objectConstructors that tell how to create a new object of this classMethods that describe the actions the object can performIn addition, a class can have data and methods of its own (not part of the objects)For example, it can keep a count of the number of objects it has createdSuch data and methods are called staticDefining a classHere is the simplest syntax for defining a class: class NameOfClass { // the fields (variables) of the object // the constructors for the object // the methods of the object}You can put public, protected, or private before the word classThings in a class can be in any order (I recommend the above order)Defining fieldsAn object’s data is stored in fields (also called instance variables)The fields describe the state of the objectFields are defined with ordinary variable declarations: String name;Double health;int age = 0;Defining constructorsA constructor is code to create an objectYou can do other work in a constructor, but you shouldn’tThe syntax for a constructor is: ClassName(parameters) { …code…}The ClassName has to be the same as the class that the constructor occurs inThe parameters are a comma-separated list of variable declarationsParametersWe usually need to give information to constructors and to methodsA parameter is a variable used to hold the incoming informationA parameter must have a name and a typeYou supply values for the parameters when you use the constructor or methodThe parameter name is only meaningful within the constructor or method in which it occursExample constructor public class Person { String name; int age; boolean male; Person (String aName, boolean isMale) { name = aName; male = isMale; }}ConstructorParametersExample use of a constructorThe constructor looks like: Person (String aName, boolean isMale) {…}aName and isMale are called formal parametersThe formal parameters are used to receive valuesYou can construct a new Person like this: Person john = new Person("John Smith", true);"John Smith" and true are called actual parametersThe actual parameters are used to give values to the formal parametersYou must have the same number of actual parameters as formal parameters, in the same order, and they must have the same typesA problem with namesIt would be nice if we could say:public class Person { String name; boolean male; Person (String name, boolean male) { name = name ; male = male ; }}A problem with namesAnd have it mean:public class Person { String name; boolean male; Person (String name, boolean male) { name = name ; male = male ; }}A problem with namesBut this is what it would really mean:public class Person { String name; boolean male; Person (String name, boolean male) { name = name ; male = male ; }}this to the rescueA parameter may have the same name as an instance variableThe name always refers to the parameterThe keyword this refers to the current objectPutting this in front of a name means that the name is a field of this object (it isn't a parameter)A problem with names—solved!Here is how we do what we want:public class Person { String name; boolean male; Person (String name, boolean male) { this.name = name ; this.male = male ; }}A typical use of thisIf you write a constructor with parameters……and the parameters are used to set fields that have the same meaning……then use the same names! Person (String name, boolean male) { this.name = name ; this.male = male ; }In fact, this is the recommended way to do itDefining a methodA method has the syntax: return-type method-name ( parameters ) { method-variables code}Example: boolean isAdult( ) { int magicAge = 21; return age >= magicAge;}Returning a result from a methodIf a method is to return a result, it must specify the type of the result boolean isAdult ( …You must use a return statement to exit the method with a result of the correct type: return age >= magicAge;This is for methods only (constructors automatically return a result of the correct type)Returning no result from a methodThe keyword void is used to indicate that a method doesn’t return a value void printAge( ) { System.out.println(name + " is " + age + " years old."); return;}The keyword return is not required in a void methodThe method will return automatically when it reaches the end of the method (the final closing brace)Sending messagesWe send a message to an object by:Naming the objectNaming the method we want to useProviding any needed actual parametersExample:


View Full Document

Penn CIT 591 - Starting Classes and Methods

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Starting Classes and Methods
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 Starting Classes and Methods 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 Starting Classes and Methods 2 2 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?