SlideShare a Scribd company logo
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
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
3
Syntax
The general syntax for defining a class is:
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
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)
6 6
Syntax: Value-Returning Method
7 7
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
8 8
Syntax
• Syntax: Formal Parameter List
-The syntax of the formal parameter list is:
• Method Call
-The syntax to call a value-returning method is:
9 9
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:
10 10
Equivalent Method Definitions
public static double larger(double x, double y)
{
double max;
if (x >= y)
max = x;
else
max = y;
return max;
}
11 11
Equivalent Method Definitions
(continued)
public static double larger(double x, double y)
{
if (x >= y)
return x;
else
return y;
}
12 12
Equivalent Method Definitions
(continued)
public static double larger(double x, double y)
{
if (x >= y)
return x;
return y;
}
13 13
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
14 14
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;
}
15 15
Sample Runs: Palindrome Number
16 16
Sample Runs: Palindrome Number
(continued)
17 17
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
18 18
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
19 19
Solution: Largest Number
static 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);
}
20 20
Sample Run: Largest Number
• Sample Run:
Enter 10 numbers:
10.5 56.34 73.3 42 22 67 88.55 26 62 11
The largest number is 88.55
21 21
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
22 22
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();
23 23
Void Methods with Parameters: Syntax
24 24
Void Methods with Parameters:
Syntax (continued)
25 25
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
26 26
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
27 27
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
28 28
Reference Variables as Parameters:
type String
29 29
Example 7-11
public 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)
30 30
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)
31 31
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)
32 32
Reference Variables as Parameters:
type String (continued)
33 33
num1 = 10; //Line 5
num2.setNum(15);
//Line 6 ch = 'A';
//Line 7
str = new StringBuffer("Sunny"); //Line 8
Reference Variables as Parameters:
type String (continued)
34 34
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)
35 35
int num; //Line 12
int len;
//Line 13 num = b.getNum();
//Line 14
Reference Variables as Parameters:
type String (continued)
36 36
num = b.getNum(); //Line 14
Reference Variables as Parameters:
type String (continued)
37 37
a++; //Line 15
Reference Variables as Parameters:
type String (continued)
38 38
b.addToNum(12); //Line 16
Reference Variables as Parameters:
type String (continued)
39 39
v = 'B'; //Line 17
Reference Variables as Parameters:
type String (continued)
40 40
len = pStr.length(); //Line 18
Reference Variables as Parameters:
type String (continued)
41 41
pStr.delete(0, len); //Line 19
Reference Variables as Parameters:
type String (continued)
42 42
pStr.append("Warm"); //Line 20
Reference Variables as Parameters:
type String (continued)
43 43Java 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)
44 44Java Programming: From Problem Analysis to Program Design, 3e
Reference Variables as Parameters:
type String (continued)
45 45
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)
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()

More Related Content

What's hot

Java™ (OOP) - Chapter 5: "Methods"
Java™ (OOP) - Chapter 5: "Methods"Java™ (OOP) - Chapter 5: "Methods"
Java™ (OOP) - Chapter 5: "Methods"
Gouda Mando
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
Intro C# Book
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
Intro C# Book
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
Intro C# Book
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
Intro C# Book
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
Intro C# Book
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
Intro C# Book
 
DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
Ferdin Joe John Joseph PhD
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
Intro C# Book
 
10. Recursion
10. Recursion10. Recursion
10. Recursion
Intro C# Book
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
Intro C# Book
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
Intro C# Book
 
Unbounded Error Communication Complexity of XOR Functions
Unbounded Error Communication Complexity of XOR FunctionsUnbounded Error Communication Complexity of XOR Functions
Unbounded Error Communication Complexity of XOR Functions
cseiitgn
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
Infinity Tech Solutions
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
Intro C# Book
 
DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5
Ferdin Joe John Joseph PhD
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Palak Sanghani
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
Intro C# Book
 
Computer java programs
Computer java programsComputer java programs
Computer java programs
ADITYA BHARTI
 

What's hot (20)

Java™ (OOP) - Chapter 5: "Methods"
Java™ (OOP) - Chapter 5: "Methods"Java™ (OOP) - Chapter 5: "Methods"
Java™ (OOP) - Chapter 5: "Methods"
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
 
DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
10. Recursion
10. Recursion10. Recursion
10. Recursion
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
06slide
06slide06slide
06slide
 
Unbounded Error Communication Complexity of XOR Functions
Unbounded Error Communication Complexity of XOR FunctionsUnbounded Error Communication Complexity of XOR Functions
Unbounded Error Communication Complexity of XOR Functions
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
 
Computer java programs
Computer java programsComputer java programs
Computer java programs
 

Viewers also liked

Android app development hybrid approach for beginners - Tools Installations ...
Android app development  hybrid approach for beginners - Tools Installations ...Android app development  hybrid approach for beginners - Tools Installations ...
Android app development hybrid approach for beginners - Tools Installations ...
Khirulnizam Abd Rahman
 
Topik 4 Teknologi Komputer: Hardware, Software dan Heartware
Topik 4 Teknologi Komputer: Hardware, Software dan HeartwareTopik 4 Teknologi Komputer: Hardware, Software dan Heartware
Topik 4 Teknologi Komputer: Hardware, Software dan Heartware
Khirulnizam Abd Rahman
 
Mobile Web App development multiplatform using phonegap-cordova
Mobile Web App development multiplatform using phonegap-cordovaMobile Web App development multiplatform using phonegap-cordova
Mobile Web App development multiplatform using phonegap-cordova
Khirulnizam Abd Rahman
 
Android app development Hybrid approach for beginners
Android app development  Hybrid approach for beginnersAndroid app development  Hybrid approach for beginners
Android app development Hybrid approach for beginners
Khirulnizam Abd Rahman
 
Html5 + Bootstrap & Mobirise
Html5 + Bootstrap & MobiriseHtml5 + Bootstrap & Mobirise
Html5 + Bootstrap & Mobirise
Khirulnizam Abd Rahman
 
Topik 3 Masyarakat Malaysia dan ICT
Topik 3   Masyarakat Malaysia dan ICTTopik 3   Masyarakat Malaysia dan ICT
Topik 3 Masyarakat Malaysia dan ICT
Khirulnizam Abd Rahman
 
Tips menyediakan slaid pembentangan berkesan - tiada template
Tips menyediakan slaid pembentangan berkesan - tiada templateTips menyediakan slaid pembentangan berkesan - tiada template
Tips menyediakan slaid pembentangan berkesan - tiada template
Khirulnizam Abd Rahman
 
Chapter 5 Class File
Chapter 5 Class FileChapter 5 Class File
Chapter 5 Class File
Khirulnizam Abd Rahman
 
Chapter 6 Java IO File
Chapter 6 Java IO FileChapter 6 Java IO File
Chapter 6 Java IO File
Khirulnizam Abd Rahman
 

Viewers also liked (9)

Android app development hybrid approach for beginners - Tools Installations ...
Android app development  hybrid approach for beginners - Tools Installations ...Android app development  hybrid approach for beginners - Tools Installations ...
Android app development hybrid approach for beginners - Tools Installations ...
 
Topik 4 Teknologi Komputer: Hardware, Software dan Heartware
Topik 4 Teknologi Komputer: Hardware, Software dan HeartwareTopik 4 Teknologi Komputer: Hardware, Software dan Heartware
Topik 4 Teknologi Komputer: Hardware, Software dan Heartware
 
Mobile Web App development multiplatform using phonegap-cordova
Mobile Web App development multiplatform using phonegap-cordovaMobile Web App development multiplatform using phonegap-cordova
Mobile Web App development multiplatform using phonegap-cordova
 
Android app development Hybrid approach for beginners
Android app development  Hybrid approach for beginnersAndroid app development  Hybrid approach for beginners
Android app development Hybrid approach for beginners
 
Html5 + Bootstrap & Mobirise
Html5 + Bootstrap & MobiriseHtml5 + Bootstrap & Mobirise
Html5 + Bootstrap & Mobirise
 
Topik 3 Masyarakat Malaysia dan ICT
Topik 3   Masyarakat Malaysia dan ICTTopik 3   Masyarakat Malaysia dan ICT
Topik 3 Masyarakat Malaysia dan ICT
 
Tips menyediakan slaid pembentangan berkesan - tiada template
Tips menyediakan slaid pembentangan berkesan - tiada templateTips menyediakan slaid pembentangan berkesan - tiada template
Tips menyediakan slaid pembentangan berkesan - tiada template
 
Chapter 5 Class File
Chapter 5 Class FileChapter 5 Class File
Chapter 5 Class File
 
Chapter 6 Java IO File
Chapter 6 Java IO FileChapter 6 Java IO File
Chapter 6 Java IO File
 

Similar to Chapter 4 - Classes in Java

Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
Svetlin Nakov
 
Charcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.pptCharcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
PRN USM
 
Parameters
ParametersParameters
Parameters
James Brotsos
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.ppt
Mahyuddin8
 
40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation
Harish Gyanani
 
14method in c#
14method in c#14method in c#
14method in c#
Sireesh K
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
NUST Stuff
 
Array assignment
Array assignmentArray assignment
Array assignment
Ahmad Kamal
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
Kavitha713564
 
static methods
static methodsstatic methods
static methods
Micheal Ogundero
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
YashJain47002
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
BAINIDA
 

Similar to Chapter 4 - Classes in Java (20)

Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Charcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.pptCharcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.ppt
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 
Chap07
Chap07Chap07
Chap07
 
Parameters
ParametersParameters
Parameters
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.ppt
 
40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation
 
14method in c#
14method in c#14method in c#
14method in c#
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
static methods
static methodsstatic methods
static methods
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Bc0037
Bc0037Bc0037
Bc0037
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 

More from Khirulnizam Abd Rahman

Topik 2 Sejarah Perkembanggan Ilmu NBWU1072
Topik 2 Sejarah Perkembanggan Ilmu NBWU1072Topik 2 Sejarah Perkembanggan Ilmu NBWU1072
Topik 2 Sejarah Perkembanggan Ilmu NBWU1072
Khirulnizam Abd Rahman
 
Panduan tugasan Makmal Teknologi Maklumat dalam Kehidupan Insan
Panduan tugasan Makmal Teknologi Maklumat dalam Kehidupan InsanPanduan tugasan Makmal Teknologi Maklumat dalam Kehidupan Insan
Panduan tugasan Makmal Teknologi Maklumat dalam Kehidupan Insan
Khirulnizam Abd Rahman
 
Topik 1 Islam dan Teknologi Maklumat
Topik 1 Islam dan Teknologi MaklumatTopik 1 Islam dan Teknologi Maklumat
Topik 1 Islam dan Teknologi Maklumat
Khirulnizam Abd Rahman
 
Application of Ontology in Semantic Information Retrieval by Prof Shahrul Azm...
Application of Ontology in Semantic Information Retrieval by Prof Shahrul Azm...Application of Ontology in Semantic Information Retrieval by Prof Shahrul Azm...
Application of Ontology in Semantic Information Retrieval by Prof Shahrul Azm...
Khirulnizam Abd Rahman
 
Chapter 1 Nested Control Structures
Chapter 1 Nested Control StructuresChapter 1 Nested Control Structures
Chapter 1 Nested Control Structures
Khirulnizam Abd Rahman
 
Chapter 1 nested control structures
Chapter 1 nested control structuresChapter 1 nested control structures
Chapter 1 nested control structures
Khirulnizam Abd Rahman
 
DTCP2023 Fundamentals of Programming
DTCP2023 Fundamentals of ProgrammingDTCP2023 Fundamentals of Programming
DTCP2023 Fundamentals of Programming
Khirulnizam Abd Rahman
 
Npwu-mpu 3252 Teknologi Maklumat dalam Kehidupan Insan
Npwu-mpu 3252 Teknologi Maklumat dalam Kehidupan InsanNpwu-mpu 3252 Teknologi Maklumat dalam Kehidupan Insan
Npwu-mpu 3252 Teknologi Maklumat dalam Kehidupan Insan
Khirulnizam Abd Rahman
 
Simple skeleton of a review paper
Simple skeleton of a review paperSimple skeleton of a review paper
Simple skeleton of a review paper
Khirulnizam Abd Rahman
 
Rangka kursus pembangunan aplikasi android kuiscell khirulnizam
Rangka kursus pembangunan aplikasi android kuiscell   khirulnizamRangka kursus pembangunan aplikasi android kuiscell   khirulnizam
Rangka kursus pembangunan aplikasi android kuiscell khirulnizam
Khirulnizam Abd Rahman
 
Khirulnizam malay proverb detection - mobilecase 19 sept 2012 - copy
Khirulnizam   malay proverb detection - mobilecase 19 sept 2012 - copyKhirulnizam   malay proverb detection - mobilecase 19 sept 2012 - copy
Khirulnizam malay proverb detection - mobilecase 19 sept 2012 - copyKhirulnizam Abd Rahman
 
Senarai doa al-mathurat sughro - dengan terjemahan
Senarai doa al-mathurat sughro - dengan terjemahanSenarai doa al-mathurat sughro - dengan terjemahan
Senarai doa al-mathurat sughro - dengan terjemahan
Khirulnizam Abd Rahman
 
Al mathurat sughra - ringkas - m-mathurat
Al mathurat sughra - ringkas - m-mathuratAl mathurat sughra - ringkas - m-mathurat
Al mathurat sughra - ringkas - m-mathuratKhirulnizam Abd Rahman
 
Android development beginners faq
Android development  beginners faqAndroid development  beginners faq
Android development beginners faq
Khirulnizam Abd Rahman
 
Khirulnizam - students experience in using blog as a learning tool - world co...
Khirulnizam - students experience in using blog as a learning tool - world co...Khirulnizam - students experience in using blog as a learning tool - world co...
Khirulnizam - students experience in using blog as a learning tool - world co...
Khirulnizam Abd Rahman
 

More from Khirulnizam Abd Rahman (18)

Topik 2 Sejarah Perkembanggan Ilmu NBWU1072
Topik 2 Sejarah Perkembanggan Ilmu NBWU1072Topik 2 Sejarah Perkembanggan Ilmu NBWU1072
Topik 2 Sejarah Perkembanggan Ilmu NBWU1072
 
Panduan tugasan Makmal Teknologi Maklumat dalam Kehidupan Insan
Panduan tugasan Makmal Teknologi Maklumat dalam Kehidupan InsanPanduan tugasan Makmal Teknologi Maklumat dalam Kehidupan Insan
Panduan tugasan Makmal Teknologi Maklumat dalam Kehidupan Insan
 
Topik 1 Islam dan Teknologi Maklumat
Topik 1 Islam dan Teknologi MaklumatTopik 1 Islam dan Teknologi Maklumat
Topik 1 Islam dan Teknologi Maklumat
 
Application of Ontology in Semantic Information Retrieval by Prof Shahrul Azm...
Application of Ontology in Semantic Information Retrieval by Prof Shahrul Azm...Application of Ontology in Semantic Information Retrieval by Prof Shahrul Azm...
Application of Ontology in Semantic Information Retrieval by Prof Shahrul Azm...
 
Chapter 1 Nested Control Structures
Chapter 1 Nested Control StructuresChapter 1 Nested Control Structures
Chapter 1 Nested Control Structures
 
Chapter 1 nested control structures
Chapter 1 nested control structuresChapter 1 nested control structures
Chapter 1 nested control structures
 
DTCP2023 Fundamentals of Programming
DTCP2023 Fundamentals of ProgrammingDTCP2023 Fundamentals of Programming
DTCP2023 Fundamentals of Programming
 
Npwu-mpu 3252 Teknologi Maklumat dalam Kehidupan Insan
Npwu-mpu 3252 Teknologi Maklumat dalam Kehidupan InsanNpwu-mpu 3252 Teknologi Maklumat dalam Kehidupan Insan
Npwu-mpu 3252 Teknologi Maklumat dalam Kehidupan Insan
 
Simple skeleton of a review paper
Simple skeleton of a review paperSimple skeleton of a review paper
Simple skeleton of a review paper
 
Airs2014 brochure
Airs2014 brochureAirs2014 brochure
Airs2014 brochure
 
Rangka kursus pembangunan aplikasi android kuiscell khirulnizam
Rangka kursus pembangunan aplikasi android kuiscell   khirulnizamRangka kursus pembangunan aplikasi android kuiscell   khirulnizam
Rangka kursus pembangunan aplikasi android kuiscell khirulnizam
 
Khirulnizam malay proverb detection - mobilecase 19 sept 2012 - copy
Khirulnizam   malay proverb detection - mobilecase 19 sept 2012 - copyKhirulnizam   malay proverb detection - mobilecase 19 sept 2012 - copy
Khirulnizam malay proverb detection - mobilecase 19 sept 2012 - copy
 
Maklumat program al quran dan borang
Maklumat program al quran dan borangMaklumat program al quran dan borang
Maklumat program al quran dan borang
 
Senarai doa al-mathurat sughro - dengan terjemahan
Senarai doa al-mathurat sughro - dengan terjemahanSenarai doa al-mathurat sughro - dengan terjemahan
Senarai doa al-mathurat sughro - dengan terjemahan
 
Al mathurat sughra - ringkas - m-mathurat
Al mathurat sughra - ringkas - m-mathuratAl mathurat sughra - ringkas - m-mathurat
Al mathurat sughra - ringkas - m-mathurat
 
Kemalangan akibat tumpuan terganggu
Kemalangan akibat tumpuan tergangguKemalangan akibat tumpuan terganggu
Kemalangan akibat tumpuan terganggu
 
Android development beginners faq
Android development  beginners faqAndroid development  beginners faq
Android development beginners faq
 
Khirulnizam - students experience in using blog as a learning tool - world co...
Khirulnizam - students experience in using blog as a learning tool - world co...Khirulnizam - students experience in using blog as a learning tool - world co...
Khirulnizam - students experience in using blog as a learning tool - world co...
 

Recently uploaded

20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 

Recently uploaded (20)

20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 

Chapter 4 - Classes in Java

  • 1. 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
  • 2. 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
  • 3. 3 Syntax The general syntax for defining a class is:
  • 4. 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
  • 5. 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)
  • 7. 7 7 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
  • 8. 8 8 Syntax • Syntax: Formal Parameter List -The syntax of the formal parameter list is: • Method Call -The syntax to call a value-returning method is:
  • 9. 9 9 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:
  • 10. 10 10 Equivalent Method Definitions public static double larger(double x, double y) { double max; if (x >= y) max = x; else max = y; return max; }
  • 11. 11 11 Equivalent Method Definitions (continued) public static double larger(double x, double y) { if (x >= y) return x; else return y; }
  • 12. 12 12 Equivalent Method Definitions (continued) public static double larger(double x, double y) { if (x >= y) return x; return y; }
  • 13. 13 13 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
  • 14. 14 14 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; }
  • 15. 15 15 Sample Runs: Palindrome Number
  • 16. 16 16 Sample Runs: Palindrome Number (continued)
  • 17. 17 17 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
  • 18. 18 18 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
  • 19. 19 19 Solution: Largest Number static 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); }
  • 20. 20 20 Sample Run: Largest Number • Sample Run: Enter 10 numbers: 10.5 56.34 73.3 42 22 67 88.55 26 62 11 The largest number is 88.55
  • 21. 21 21 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
  • 22. 22 22 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();
  • 23. 23 23 Void Methods with Parameters: Syntax
  • 24. 24 24 Void Methods with Parameters: Syntax (continued)
  • 25. 25 25 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
  • 26. 26 26 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
  • 27. 27 27 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
  • 28. 28 28 Reference Variables as Parameters: type String
  • 29. 29 29 Example 7-11 public 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)
  • 30. 30 30 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)
  • 31. 31 31 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)
  • 32. 32 32 Reference Variables as Parameters: type String (continued)
  • 33. 33 33 num1 = 10; //Line 5 num2.setNum(15); //Line 6 ch = 'A'; //Line 7 str = new StringBuffer("Sunny"); //Line 8 Reference Variables as Parameters: type String (continued)
  • 34. 34 34 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)
  • 35. 35 35 int num; //Line 12 int len; //Line 13 num = b.getNum(); //Line 14 Reference Variables as Parameters: type String (continued)
  • 36. 36 36 num = b.getNum(); //Line 14 Reference Variables as Parameters: type String (continued)
  • 37. 37 37 a++; //Line 15 Reference Variables as Parameters: type String (continued)
  • 38. 38 38 b.addToNum(12); //Line 16 Reference Variables as Parameters: type String (continued)
  • 39. 39 39 v = 'B'; //Line 17 Reference Variables as Parameters: type String (continued)
  • 40. 40 40 len = pStr.length(); //Line 18 Reference Variables as Parameters: type String (continued)
  • 41. 41 41 pStr.delete(0, len); //Line 19 Reference Variables as Parameters: type String (continued)
  • 42. 42 42 pStr.append("Warm"); //Line 20 Reference Variables as Parameters: type String (continued)
  • 43. 43 43Java 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)
  • 44. 44 44Java Programming: From Problem Analysis to Program Design, 3e Reference Variables as Parameters: type String (continued)
  • 45. 45 45 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)
  • 46. 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()