Classes, Objects, and Methods Chapter 4

advertisement

Chapter 4

Chapter 4

Classes, Objects, and Methods

Class and Method Definitions

Information Hiding and Encapsulation

Objects and Reference

Parameter passing

Java: an Introduction to Computer Science & Programming - Walter Savitch 1

Learn by Doing

 All programs in the book are available on the CD that comes with the book

 It is a good idea to run the programs as you read about them

 Do not forget that you will need the

SavitchIn.java

file for keyboard input (for almost all book programs for a while)

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 2

Classes in Java

Classes are used to define objects and provide methods to act on the objects

Since Java is a “pure” object-oriented language …

» Our main program is also part of a class (we ignored that – but it was in the code that Forte put in for us).

» The main program may declare objects and process them to solve the problem

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 3

Classes

Class —definition of a kind of object

Like an outline or plan for constructing specific objects

» see next slide or diagram in text

Example: an Automobile class

» Object that satisfies the Automobile definition instantiates the Automobile class

 Class specifies what kind of data objects of that class have

» Each object has the same data items but can have different values

Chapter 4

Class specifies what methods each object will have

» All objects of the same class have the exact same methods

Java: an Introduction to Computer Science & Programming - Walter Savitch 4

Class as an Outline

Class

Definition

Objects that are instantiations of the class

First Instantiation:

Object name: patsCar amount of fuel: 10 gallons speed: 55 miles per hour license plate: “135 XJK”

Class Name: Automobile

Data: amount of fuel ________ speed ________ license plate ________

Methods (actions): increaseSpeed:

How: stop:

How:

Chapter 4

Press on gas pedal.

Press on brake pedal.

Second Instantiation:

Object name: suesCar amount of fuel: 14 gallons speed: 0 miles per hour license plate: “SUES CAR”

Third Instantiation:

Object name: ronsCar amount of fuel: 2 gallons speed: 75 miles per hour license plate: “351 WLF”

Java: an Introduction to Computer Science & Programming - Walter Savitch 5

Objects

 Objects are variables that are named instances of a class

» the class is their type

 Objects have both data and methods

 Both the data items and methods of a class are members of the object

 Data items are also called fields or instance variables

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 6

Another Example Class: Student

Data Items: name, SSN, year, major, …

Methods: register for a section, change major, …

 Objects of the class: all of you

 E.g. use in part of main

Student student1 = new Student(); // create

// find out student name etc

// to register for a section: student1.register(

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 7

Objects in Use - syntax

In a program function (such as main), create objects

Syntax: class_Name instance_Name = new class_Name();

Note the keyword new – actually creates space in memory for instance variables

Invoking a method means to call the method, i.e. execute the method

» Syntax for invoking an object's method (e.g. in main): the dot operator object_Variable_Name.method()

» object_Variable_Name is the calling object

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 8

Another Example Class: (Bank) Account

Data Items: account number, PIN, balance …

Methods: deposit, withdraw, …

 Objects of the class: all individual accounts at the bank

 E.g. (); // create

Account myAccount = new Account();

// find out initial account info

// to deposit money into myAccount: myAccount .deposit ( 100);

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 9

Class Files

Each Java class definition should be a separate file

Use the same name for the class and the file, except add

".java" to the file name

Good programming practice:

Start the class (and file) name a capital letter and capitalize inner words upper case

» e.g. MyClass.java for the class MyClass

For now put all the classes you need to run a program in the same directory

 When you compile, the bytecodes will be in a classname.class file

» E.g. Student.java compiles into Student.class

 <SHOW ACCOUNT CLASS>

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 11

Instance Variables

 Account class has three instance variables: acctNum,

PIN, and balance : protected String acctNum; protected String PIN; protected double balance;

 protected means that there are restrictions on how these instance variables are used.

13 Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch

Chapter 4

Instantiating (Creating) Objects

<SKIP>

Syntax: class_Name instance_Name = new class_Name();

Note the keyword new

 For example, the text defines a class named SpeciesFirstTry

//instantiate an object of this class

SpeciesFirstTry speciesOfTheMonth = new SpeciesFirstTry();

 Public instance variables can be accessed using the dot operator:

SpeciesOfTheMonth.name = “Klingon ox”;

Java: an Introduction to Computer Science & Programming - Walter Savitch 14

Return Type of Methods

Some methods perform an action and return a single value

Some methods just perform an action (e.g. print a message or read in a value from the keyboard) and do not return a value

All methods must be defined with a return type

Return types may be:

» a primitive data type, such as char , int , double , etc.

» a class, such as String , SpeciesFirstTry , Account,

Student, etc.

» void if no value is returned

15 Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch

Chapter 4

Calling Methods

You can use a method anyplace where it is legal to use its return type , for example the readLineInt() method of

SavitchIn returns an integer, so this is legal: int next = SavitchIn.readLineInt();

For methods that don’t return a value (void), putting the call on a line by itself is appropriate

System.out.println(“This method is used for its action”);

Which do you think is legal to to the opposite on?

Does it make sense?

Java: an Introduction to Computer Science & Programming - Walter Savitch 16

Defining Methods

In Java, ALWAYS defined inside a class definition

Methods that return a value must execute a

return

statement that includes the value to return

 For example: public double getBal() { return balance;

}

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 17

More Interesting …

Using some of the capabilities we have been learning throughout the semester public boolean withdraw(double amt) { // given an amount to withdraw if (amt <= 0) {

System.out.println("PROGRAM ERROR - cannot withdraw a negative amount"); return false;

} else if (amt > balance) {

System.out.println("ERROR - cannot withdraw an amount more than in account"); return false;

} else {

// can withdraw balance = balance - amt; return true;

}

Java: an Introduction to Computer Science & Programming - Walter Savitch 18

void

Method Example

 The definition of the writeOutput method of SpeciesFirstTry : public void writeOutput()

{

System.out.println("Name = " + name);

System.out.println("Population = " + population);

System.out.println("Growth = " + growthRate + "%");

}

 Assuming instance variables name , population , and growthRate have been defined and assigned values, this method performs an action (writes values to the screen) but does not return a value

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 19

The

main

Method

Chapter 4

 A class written to solve a problem (rather than define objects) is written as a class with one method, main

Invoking the class name invokes the main method

See the example: AccountDemo

Note the basic structure: public class AccountDemo

{ public static void main(String[] args)

{

<statements that define the main method>

}

}

Java: an Introduction to Computer Science & Programming - Walter Savitch 21

Chapter 4

Local Variables and Blocks

A block (a compound statement) is the set of statements between a pair of matching braces (curly brackets)

A variable declared inside a block is known only inside that block

» it is local to the block, therefore it is called a local variable

» when the block finishes executing, local variables disappear

» references to it outside the block cause a compile error

Java: an Introduction to Computer Science & Programming - Walter Savitch 24

Example

Suppose a Student class has a calcGPA method, which loops through all the finished courses on a student’s transcript and calculates the GPA public double calcGPA ( ) { double totalGradePts = 0; // this var is local to the method for (int cnt = 0; cnt < numbCourses; cnt++) { // cnt is local to the loop double currGradePt; // local to the loop

}

// any reference to cnt or currGradePt out here would be an error

}

If any of these variables is declared and used in another method, that is legal

BUT they are TOTALLY DIFFERENT variables (different memory locations, and different values)

25 Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch

Chapter 4

Local Variables and Blocks

Some programming languages (e.g. C and C++) allow the variable name to be reused outside the local block

» it is confusing and not recommended, nevertheless, it is allowed

However, a variable name in Java can be declared only once for a method

» although the variable does not exist outside the block, other blocks in the same method cannot reuse the variable's name

Java: an Introduction to Computer Science & Programming - Walter Savitch 26

When and Where to Declare Variables

 Declaring variables outside all blocks but within the method definition makes them available within all the blocks

Good programming Practice:

 declare variables just before you use them – OR at start of method

 initialize variables when you declare them do not declare variables inside loops – unless there is a reason to

» it takes time during execution to create and destroy variables, so it is better to do it just once for loops) it is ok to declare loop counters in the Initialization field of for loops, e.g.

for( int i=0 ; i <10; i++)…

» the Initialization field executes only once, when the for loop is first entered

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 27

Passing Values to a Method: Parameters

Some methods can be more flexible (therefore useful) if we pass them input values

Input values for methods are called passed values or parameters

Parameters and their data types must be specified inside the parentheses of the heading in the method definition

» these are called formal parameters

The calling object must put values of the same data type, in the same order, inside the parentheses of the method invocation

» these are called arguments , or actual parameters

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 28

Parameter Passing Example

 Lets look back at the withdraw method public boolean withdraw(double amt) { …

 Invocation of the method... somewhere in main...

haveMoney = myAcct.withdraw(currBill);

Having a parameter makes this fuinction so much more useful than it otherwise would be (alternative example – “FastCash” – you can withdraw $60, no more, no less)

What is the formal parameter in the method definition?

» amt

What is the argument ( actual parameter) in the method invocation?

» currBill

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 29

Chapter 4

OK – Let’s Solve a Problem

Let’s say that we are going to pay interest on the money in the accounts. Let’s have our main compare the difference between compounding daily vs compounding monthly. We will want to add a payInterest method to Account, and handle the rest in main.

Let’s start by defining the heading for the method

» public double payInterest (int numDays)

Java: an Introduction to Computer Science & Programming - Walter Savitch 31

Let’s handle the part in main

RevisedAccount testDaily = new RevisedAccount();

// could ask user for the initial amounts but just did the quick way for demo testDaily.setBal(5000); testDaily.setIntRate(0.02); // also need to set interest rate – something new

RevisedAccount testMonthly = new RevisedAccount(); testMonthly.setBal(5000); testMonthly.setIntRate(0.02); // also need to set interest rate

// how accurate do we want this? – lets do it approximately –

// assume 30 days every month

// do 12 months for (int mon = 0; mon < 12; mon++) {

// do 30 days within the month for (int day = 0; day < 30; day++) { testDaily.payInterest(1); // unusual situation – don’t need the returned value

} testMonthly.payInterest(30); // unusual situation - don't need the returned value

}

System.out.println("final balance with daily compounding is: " + testDaily.getBal());

System.out.println("final balance with monthly compounding is: "+testMonthly.getBal());

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 32

Let’s look at the additions to Account

First, we need a new instance variable protected double intRate;

// later in semester (chapt 7), we will do this in a better way

Second, we need a way of setting the interest rate void setIntRate (double rate) { intRate = rate;

} // we can use this in main to set our interest rate

Third, we need payInterest public double payInterest (int numDays) { if (numDays <= 0) {

System.out.println("ERROR - invalid number of days");

// no interest if no days or fewer return 0;

} double intAmt = balance * (intRate * (numDays / 365.0)); balance += intAmt; return intAmt;

Java: an Introduction to Computer Science & Programming - Walter Savitch 33

Pass-By-Value:

Primitive Data Types as Parameters

When the method is called, the value of each argument is copied (assigned) to its corresponding formal parameter

The number of arguments must be the same as the number of formal parameters *

The data types of the arguments must be the same as the formal parameters and in the same order *

Formal parameters are initialized to the values passed

Formal parameters are local to their method

Variables used as arguments cannot be changed by the method

» the method only gets a copy of the variable's value

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 37

Information Hiding and Encapsulation

•Cornerstones of Object Oriented Programming (OOP)

•Both are forms of abstraction

Information hiding

Encapsulation

Chapter 4

In Java, use classes and objects

Objects include both data items and methods to act on the data

One way of doing info hiding

 protect data inside an object do not allow direct access use private or protected modifier for instance variable declarations use public methods to access data

» called accessor (or inspector) methods

» Frequently named get…

» Change data using mutator methods

» Frequently named se t…

 Programmer can use any method without knowing details of instance variables or code for the method

Java: an Introduction to Computer Science & Programming - Walter Savitch 38

Formalized Abstraction: ADTs

ADT: Abstract data type

 An approach used by several languages including Object-Oriented languages

 a container for both data items and methods to act on the data

» In OO languages, supported by class es capability

Implements information hiding and encapsulation

Provides a public user interface so the user knows how to use the class

» descriptions, parameters, and names of its methods

Implementation:

» private instance variables

» method definitions are usually public but always hidden from the user

» the user cannot see or change the implementation

» the user only sees the interface

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 39

Do We Do Anything Special?

In Java, Not really! Just create classes as previously described, except:

Do not give the user (programmer) the class definition file

Do give the user the interface - a file with just the class and method descriptions and headings

» the headings give the names and parameters of the methods

» documentation (comments describing methods) tells the user how to use the class and its methods ( need documentation!!!!!)

» it is all the user needs to know

Chapter 4

Make sure you use the private modifier when declaring instance variables

Make sure you provide accessor and mutator functions for when the user (programmer) will need to get at or change data

Java: an Introduction to Computer Science & Programming - Walter Savitch 40

Gee this is extra work …

Going through accessor and mutator methods to get to instance variables is extra work – but it allows any checking for reasonableness to be located in one place

And it means that the class developer can change instance variables without the user programmer being affected (e.g. decide to just store birth date and skip storing age)

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 41

Javadoc

Comes with Sun’s Java (which we have)

Automatically produces documentation describing the external interface for a class, ASSUMING that your comments follow a certain convention

 Comments before each method in format :

/**

* Comments in here

* The stars on these lines are not needed for Java, but are needed for

* Javadoc

* The begin comment symbols need at least one extra * (typically put a

* whole line of them to make the comment stand out to humans

*/

In Forte do this using menu choice – Tools\Generate JavaDoc

When working on team – the result could be what you give to your partner – to be helpful, but do info hiding to an extent

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 42

OK – Let’s Look at Another Possible Method

Chapter 4

}

Balance transfer – Let’s dream up main code first

// transfer from checking to savings myChecking.transfer(200, mySavings);

Let’s write the method public boolean transfer (double amt, Account toAcct) { if (amt < 0) {

System.out.println(“ERROR”); return false;

} if (amt > balance) {

System.out.println(“ERROR”); return false;

} balance = balance – amt; toAcct.deposit(amt); return true;

Java: an Introduction to Computer Science & Programming - Walter Savitch 44

Variables: Class Type vs. Primitive Type

Chapter 4

What does a variable hold?

» It depends on the type of type, primitive type or class type

 A primitive type variable holds the value of the variable

 Class types are more complicated

» they have methods and instance variables

 A class type variable holds the memory address of the object

» the variable does not actually hold the value of the object

» in fact, as stated above, objects generally do not have a single value and they also have methods, so it does not make sense to talk about its "value"

Java: an Introduction to Computer Science & Programming - Walter Savitch 45

Assignment with

Variables of a Class Type

Account orig = new Account(); orig.setAcctNum(“1234”); orig.setBal(1000);

Account other = new Account(); other.setAcctNum(“7777”); other.setBal(0); other = orig; // this is start of the tricky part

System.out.println(“other: “ + other.toString()); orig.deposit(100); // this is tricky too

System.out.println(“orig: “ + orig.toString());

System.out.println(“other: “ + other.toString());

What will the output be?

(see the next slide)

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 46

Assignment with

Variables of a Class Type

Account orig = new Account(); orig.setAcctNum(“1234”); orig.setBal(1000);

Account other = new Account(); other.setAcctNum(“7777”); other.setBal(0); other = orig; // this is start of the tricky part

System.out.println(“other: “ + other.toString()); orig.deposit(100); // this is tricky too

System.out.println(“orig: “ + orig.toString());

System.out.println(“other: “ + other.toString());

What will the output be? orig and other both print the same thing!!

other: Acct: 1234 PIN: Bal: 1000 orig: Acct: 1234 PIN: Bal: 1100 other: Acct: 1234 PIN: Bal: 1100

Chapter 4

Why do they print the same thing? (see the next slide)

Java: an Introduction to Computer Science & Programming - Walter Savitch 48

Assignment with

Variables of a Class Type

Before the assignment statement, orig and other refer to two different objects.

Account orig = new Account(); orig.setAcctNum(“1234”); orig.setBal(1000); orig “1234”

“ “

1000

Account other = new Account(); other.setAcctNum(“7777”); other other.setBal(0);

“7777”

“ “

0 other = orig; // tricky part

System.out.println(“other: “ + other.toString()); orig.deposit(100); // this is tricky too

System.out.println(“orig: “ + orig.toString()); After the assignment statement, orig and other refer to the

Why do they print the same thing?

The assignment statement makes other refer to the same object .

orig and same object.

orig

When orig is changed via deposit, other

“1234”

“ “ other is changed also.

1000

51 Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch

Chapter 4

Gotcha: Comparing Class Variables

A class variable returns a number, but it is not its value

It returns the memory address where the object with that variable name is stored

If two class variables are compared using ==, it is the addresses, not the values that are compared!

This is rarely what you want to do!

 Use the class's .equals() method to compare the values of class variables

Java: an Introduction to Computer Science & Programming - Walter Savitch 52

Example: Comparing Class Variables

//User enters first string

String firstLine = SavitchIn.readLine();

//User enters second string

String secondLine = SavitchIn.readLine(); if(firstLine == secondLine)

//this compares their addresses

{

<body of if statement>

} if(firstLine.equals(secondLine)

//this compares their values

{

<body of if statement>

}

Use .equals

method (not the double-equals sign) to compare values

Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch 53

Pass the Address:

Class Types as Method Parameters

Chapter 4

In the same way, class variable names used as parameters in a method call copy the argument's address (not the value) to the formal parameter

So the formal parameter name also contains the address of the argument

It is as if the formal parameter name is an alias for the argument name

Any action taken on the formal parameter is actually taken on the original argument!

 Unlike the situation with primitive types, the original argument is not protected for class types!

Java: an Introduction to Computer Science & Programming - Walter Savitch 54

Example: Class Type as a Method Parameter

//Method definition with a DemoSpecies class parameter public void makeEqual(DemoSpecies otherObject)

{ otherObject.name = this.name; otherObject.population = this.population; otherObject.growthRate = this.growthRate;

}

//Method invocation

DemoSpecies s1 = new DemoSpecies("Crepek", 10, 20);

DemoSpecies s2 = new DemoSpecies(); s1.makeEqual(s2);

 The method call makes otherObject an alias for s2 , therefore the method acts on s2 , the DemoSpecies object passed to the method!

Chapter 4

 This is unlike primitive types, where the passed variable cannot be changed.

Java: an Introduction to Computer Science & Programming - Walter Savitch 55

Chapter 4

Summary

Part 1

Classes have instance variables to store data and methods to perform actions

Declare instance variables to be private so they can be accessed only within the same class

There are two kinds of methods: those that return a value and void -methods

 Methods can have parameters of both primitive type and class type

Java: an Introduction to Computer Science & Programming - Walter Savitch 56

Summary

Part 2

Chapter 4

 Parameters of a primitive type work differently than those of a class type

» primitive type parameters are call-by-value, so the calling object's variable is protected within the called method (the called method cannot change it)

» class type parameters pass the address of the calling object so it is un protected (the called method can change it)

 For similar reasons, the operators = and == do not behave the same for class types as they do for primitive types (they operate on the address of object and not its values)

 Therefor you should usually define an equals method for classes you define (to allow the values of objects to be compared)

Java: an Introduction to Computer Science & Programming - Walter Savitch 57

Download