chapter 4 - classes in java

46
1 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

Upload: khirulnizam-abd-rahman

Post on 17-Jul-2015

235 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Chapter 4 - Classes in Java

1

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

Page 2: Chapter 4 - Classes in Java

2

Classes (continued)

• 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

Page 3: Chapter 4 - Classes in Java

3

Syntax

The general syntax for defining a class is:

Page 4: Chapter 4 - Classes in Java

4

Syntax (continued)

• If a member of a class is a named constant, you declare it just like any other named constant

• If a member of a class is a variable, you declare it just like any other variable

• If a member of a class is a method, you define it just like any other method

Page 5: Chapter 4 - Classes in Java

5

Syntax (continued)

• If a member of a class is a method, it can (directly) access any member of the class—data members and methods

- Therefore, when you write the definition of a method (of the class), you can directly access any data member of the class (without passing it as a parameter)

Page 6: Chapter 4 - Classes in Java

66

Syntax: Value-Returning Method

Page 7: Chapter 4 - Classes in Java

77

User-Defined Methods

• Value-returning methods– Used in expressions– Calculate and return a value– Can save value for later calculation or print value

• modifiers: public, private, protected, static, abstract, final

• returnType: type of the value that the method calculates and returns (using return statement)

• methodName: Java identifier; name of method

Page 8: Chapter 4 - Classes in Java

88

Syntax

• Syntax: Formal Parameter List-The syntax of the formal parameter list is:

• Method Call-The syntax to call a value-returning method is:

Page 9: Chapter 4 - Classes in Java

99

Syntax (continued)

• Syntax: return Statement -The return statement has the following syntax:

return expr;

• Syntax: Actual Parameter List-The syntax of the actual parameter list is:

Page 10: Chapter 4 - Classes in Java

1010

Equivalent Method Definitions

public static double larger(double x, double y){ double max;

if (x >= y) max = x; else max = y;

return max;}

Page 11: Chapter 4 - Classes in Java

1111

Equivalent Method Definitions (continued)

public static double larger(double x, double y){ if (x >= y) return x; else return y;}

Page 12: Chapter 4 - Classes in Java

1212

Equivalent Method Definitions (continued)

public static double larger(double x, double y){ if (x >= y) return x; return y;}

Page 13: Chapter 4 - Classes in Java

1313

Programming Example: Palindrome Number

• Palindrome: integer or string that reads the same forwards and backwards

• Input: integer or string

• Output: Boolean message indicating whether integer string is a palindrome

Page 14: Chapter 4 - Classes in Java

1414

Solution: isPalindrome Method

public static boolean isPalindrome(String str){ int len = str.length(); int i, j; j = len - 1;

for (i = 0; i <= (len - 1) / 2; i++) { if (str.charAt(i) != str.charAt(j)) return false; j--; } return true; }

Page 15: Chapter 4 - Classes in Java

1515

Sample Runs: Palindrome Number

Page 16: Chapter 4 - Classes in Java

1616

Sample Runs: Palindrome Number (continued)

Page 17: Chapter 4 - Classes in Java

1717

Flow of Execution• Execution always begins with the first statement

in the method main• User-defined methods execute only when called

• Call to method transfers control from caller to called method

• In method call statement, specify only actual parameters, not data type or method type

• Control goes back to caller when method exits

Page 18: Chapter 4 - Classes in Java

1818

Programming Example: Largest Number

• Input: set of 10 numbers

• Output: largest of 10 numbers

• Solution– Get numbers one at a time– Method largest number: returns the larger of 2

numbers– For loop: calls method largest number on each number

received and compares to current largest number

Page 19: Chapter 4 - Classes in Java

1919

Solution: Largest Numberstatic Scanner console = new Scanner(System.in);

public static void main(String[] args){ double num; double max; int count; System.out.println("Enter 10 numbers."); num = console.nextDouble(); max = num; for (count = 1; count < 10; count++) { num = console.nextDouble(); max = larger(max, num); } System.out.println("The largest number is " + max); }

Page 20: Chapter 4 - Classes in Java

2020

Sample Run: Largest Number

• Sample Run:

Enter 10 numbers:10.5 56.34 73.3 42 22 67 88.55 26 62 11The largest number is 88.55

Page 21: Chapter 4 - Classes in Java

2121

Void Methods

• Similar in structure to value-returning methods

• Call to method is always stand-alone statement

• Can use return statement to exit method early

Page 22: Chapter 4 - Classes in Java

2222

Void Methods: Syntax

• Method Definition-The general form (syntax) of a void method without parameters is as follows:

modifier(s) void methodName(){ statements}• Method Call (Within the Class)

-The method call has the following syntax:methodName();

Page 23: Chapter 4 - Classes in Java

2323

Void Methods with Parameters: Syntax

Page 24: Chapter 4 - Classes in Java

2424

Void Methods with Parameters: Syntax (continued)

Page 25: Chapter 4 - Classes in Java

2525

Primitive Data Type Variables as Parameters

• A formal parameter receives a copy of its corresponding actual parameter

• If a formal parameter is a variable of a primitive data type:– Value of actual parameter is directly stored– Cannot pass information outside the method– Provides only a one-way link between actual

parameters and formal parameters

Page 26: Chapter 4 - Classes in Java

2626

Reference Variables as Parameters

• If a formal parameter is a reference variable:– Copies value of corresponding actual parameter– Value of actual parameter is address of the object

where actual data is stored– Both formal and actual parameter refer to same

object

Page 27: Chapter 4 - Classes in Java

2727

Uses of Reference Variables as Parameters

• Can return more than one value from a method

• Can change the value of the actual object

• When passing address, would save memory space and time, relative to copying large amount of data

Page 28: Chapter 4 - Classes in Java

2828

Reference Variables as Parameters: type String

Page 29: Chapter 4 - Classes in Java

2929

Example 7-11public class Example7_11{ public static void main(String[] args) { int num1; //Line 1 IntClass num2 = new IntClass(); //Line 2 char ch; //Line 3 StringBuffer str; //Line 4 num1 = 10; //Line 5 num2.setNum(15); //Line 6 ch = 'A'; //Line 7 str = new StringBuffer("Sunny"); //Line 8 System.out.println("Line 9: Inside main: " + "num1 = " + num1 + ", num2 = "

+ num2.getNum() + ", ch = " + ch + ", and str = " + str); //Line 9

Reference Variables as Parameters: type String (continued)

Page 30: Chapter 4 - Classes in Java

3030

funcOne(num1, num2, ch, str); //Line 10 System.out.println("Line 11: After funcOne: " + "num1 = " + num1 + ", num2 = "

+ num2.getNum() + ", ch = " + ch + ", and str = " + str); //Line 11 }

Reference Variables as Parameters: type String (continued)

Page 31: Chapter 4 - Classes in Java

3131

public static void funcOne(int a, IntClass b, char v,

StringBuffer pStr) { int num; //Line 12 int len; //Line 13 num = b.getNum(); //Line 14 a++; //Line 15 b.addToNum(12); //Line 16 v = 'B'; //Line 17 len = pStr.length(); //Line 18 pStr.delete(0, len); //Line 19 pStr.append("Warm"); //Line 20 System.out.println("Line 21: Inside funcOne: \n" + " a = " + a + ", b = " + b.getNum() + ", v = " + v + ", pStr = " + pStr + ", len = " + len + ", and num = " + num); //Line 21 }}

Reference Variables as Parameters: type String (continued)

Page 32: Chapter 4 - Classes in Java

3232

Reference Variables as Parameters: type String (continued)

Page 33: Chapter 4 - Classes in Java

3333

num1 = 10; //Line 5 num2.setNum(15); //Line 6 ch = 'A';

//Line 7str = new StringBuffer("Sunny"); //Line 8

Reference Variables as Parameters: type String (continued)

Page 34: Chapter 4 - Classes in Java

3434

System.out.println("Line 9: Inside main: " + "num1 = " + num1 + ", num2 = " + num2.getNum() + ", ch = " + ch + ", and str = " + str); //Line 9

Reference Variables as Parameters: type String (continued)

Page 35: Chapter 4 - Classes in Java

3535

int num; //Line 12 int len; //Line 13 num = b.getNum();

//Line 14

Reference Variables as Parameters: type String (continued)

Page 36: Chapter 4 - Classes in Java

3636

num = b.getNum(); //Line 14

Reference Variables as Parameters: type String (continued)

Page 37: Chapter 4 - Classes in Java

3737

a++; //Line 15

Reference Variables as Parameters: type String (continued)

Page 38: Chapter 4 - Classes in Java

3838

b.addToNum(12); //Line 16

Reference Variables as Parameters: type String (continued)

Page 39: Chapter 4 - Classes in Java

3939

v = 'B'; //Line 17

Reference Variables as Parameters: type String (continued)

Page 40: Chapter 4 - Classes in Java

4040

len = pStr.length(); //Line 18

Reference Variables as Parameters: type String (continued)

Page 41: Chapter 4 - Classes in Java

4141

pStr.delete(0, len); //Line 19

Reference Variables as Parameters: type String (continued)

Page 42: Chapter 4 - Classes in Java

4242

pStr.append("Warm"); //Line 20

Reference Variables as Parameters: type String (continued)

Page 43: Chapter 4 - Classes in Java

4343Java Programming: From Problem Analysis to Program Design, 3e

System.out.println("Line 21: Inside funcOne: \n" + " a = " + a + ", b = " + b.getNum() + ", v = " + v + ", pStr = " + pStr + ", len = " + len + ", and num = " + num); //Line 21

Reference Variables as Parameters: type String (continued)

Page 44: Chapter 4 - Classes in Java

4444Java Programming: From Problem Analysis to Program Design, 3e

Reference Variables as Parameters: type String (continued)

Page 45: Chapter 4 - Classes in Java

4545

System.out.println("Line 11: After funcOne: " + "num1 = " + num1 + ", num2 = " + num2.getNum() + ", ch = " + ch + ", and str = " + str); //Line 11

Reference Variables as Parameters: type String (continued)

Page 46: Chapter 4 - Classes in Java

46

Accessing Objects

• Referencing the object’s data:

objectRefVar.data

e.g., myCircle.radius

• Invoking the object’s method:

objectRefVar.methodName(arguments)

e.g., myCircle.getArea()