0% found this document useful (0 votes)
56 views

Java Methods: Course Packet

Methods allow classes to define functions that can be called on objects of that class. There are two types of methods: user-defined and pre-defined. A method call involves calling a method and the called method. Methods can take parameters and return values. Common string methods include charAt(), equals(), length(), toUpperCase(), substring(), indexOf(), and compareTo().

Uploaded by

Angella Quisan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Java Methods: Course Packet

Methods allow classes to define functions that can be called on objects of that class. There are two types of methods: user-defined and pre-defined. A method call involves calling a method and the called method. Methods can take parameters and return values. Common string methods include charAt(), equals(), length(), toUpperCase(), substring(), indexOf(), and compareTo().

Uploaded by

Angella Quisan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Course Packet 1 Part 2

Java Methods
ICTC0423 Computer Programming 2

Maria Lolita G. Masangcap


Quick Recap
Every class has a In Java, the word method refers
constructor. If we do not Two types methods: User- to the same kind of thing that
explicitly write a constructor Defined and Pre-Defined the word function is used for in
for a class, the java compiler Methods other languages. Specifically, a
builds a default constructor method is a function that
for that class. belongs to a class.

Different Parts of Methods:


Types of User-Defined
Method type, method name, What is Method Call?
Methods (Depending on the
parameter list, variable (Calling method and Called
parameters and returning
declaration, return Method)
value)
expression
Methods

Mutator Method
Method that is
used to write or It is usually
change the value written as
of members set<NameofField>.
fields
Methods

Method Overloading
object-oriented technique that lets
you define several different versions
of a method, all with the same name
but each with a different argument
or parameter list
Classes in Java
A class can contain any of the following variable types.
▪ Local variables - variables defined inside methods, constructors or
blocks are called local variables. The variable will be declared and
initialized within the method and the variable will be destroyed
when the method has completed.
▪ Instance variables - variables within a class but outside any
method. These variables are instantiated when the class is loaded.
Instance variables can be accessed from inside any method,
constructor or blocks of that particular class.
▪ Class variables - Class variables are variables declared within a
class, outside any method, with the static keyword.
Accessing Instance
Variables and Methods
• Instance variables and methods are accessed via created objects. To
access an instance variable the fully qualified path should be as
follows:
/* First create an object */
ObjectName = new Constructor();

/* Now call a variable as follows */


ObjectName.variableName;

/* Now you can call a class method as follows */


ObjectName.MethodName();
String
A string is a group of text.

You can store this group of text in a variable that would then
be known as a String variable.

You can print strings, and you can also use various functions to
perform operations on strings such as returning the string's
length.
String
Syntax:
String nameOfString = "stringValue";
String nameOfString = new String("stringValue");

Example:
String aString = "This is a string";
String aString = new String("This is a string");
String Methods
Example:
String str1 = “Computer Science”;
String str2 = “Information Technology”;
C o m p u t e r S c i e n c e
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

I n f o r m a t i o n T e c h n o l o g y
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
String Methods
The String class has various functions (methods) you can use to work with text.

char charAt(int index) charAt is a method that looks at a particular character in a String according to
its index number

In the given string below, 'C’ corresponds to the index of zero,


and 'p’ corresponds to the index of 3)
C o m p u t e r S c i e n c e
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example: System.out.println(str1.charAt(2)); This line prints 'm‘.


String Methods
String str2 = “Information Technology”;
I n f o r m a t i o n T e c h n o l o g y
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

System.out.println(str2.charAt(12));

System.out.println(str2.charAt(0));

System.out.println(str2.charAt(20));
String Methods
boolean equals(String s) Compares two strings and returns true if they are the same.

String str1 = “Computer Science”;


String str2 = “Information Technology”;

if (str1.equals(str2)){ This program code


System.out.println ("hi");} will print “bye”.
else{
System.out.println("bye");}
String Methods
length() is a method that returns an integer value representing the number of
int length() characters in the String.

I n f o r m a t i o n T e c h n o l o g y
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

System.out.println(str2.length()); // prints 22
C o m p u t e r S c i e n c e
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

System.out.println(str1.length()); // prints 16
String Methods
String
toUpperCase() Converts all letters in a string to upper case.
String
toLowerCase() Converts all letters in a string to lower case.

String str1 = “Computer Science”; System.out.println(str1.toUpperCase());


String str2 = “Information Technology”;
This prints “COMPUTER SCIENCE”.
System.out.println(str2.toLowerCase());
This prints “information technology”.
String Methods
String substring(int Returns a String that starts at the startIndex (index of first character is 0)
startIndex) and ends at the end of the original String.

C o m p u t e r S c i e n c e
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

System.out.println(str1.substring(9)); // prints “Science”

I n f o r m a t i o n T e c h n o l o g y
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

System.out.println(str2.substring(12)); // prints “Technology”


String Methods
String substring (int startIndex, Returns a String that starts at the startIndex and ends at the character
int afterEndingIndex) before afterEndingIndex.

C o m p u t e r S c i e n c e
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

System.out.println(str1.substring(0,7)); // prints “Compute”

I n f o r m a t i o n T e c h n o l o g y
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

System.out.println(str2.substring(12,20)); // prints “Technolo”


String Methods
Returns the integer representing the position number where the s starts
int indexOf(String s)
in the String. If it isn't in the String, indexOf returns –1.

C o m p u t e r S c i e n c e
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

System.out.println(str1.indexOf(“enc”)); // prints 12
System.out.println(str1.indexOf(“end”)); // prints -1
String Methods
int compareTo(String S) Returns the integer based on the result of the comparison.

This method compares two Strings in if (str1.compareTo(str2) < 0)


dictionary order.
System.out.println(str1+" is less than "+str2+".");
• If str1.compareTo(str2) < 0, then else if (str1.compareTo(str2) == 0)
str1 precedes str2 in the dictionary.
• If str1.compareTo(str2) == 0, then System.out.println(str1+" is equal to "+str2+".");
str1 is the same as str2. (Note: This else
is an alternative to
str1.equals(str2).) System.out.println(str1+" is greater to "+str2+".");
• If str1.compareTo(str2) > 0, then
str1 follows str2 in the dictionary.
String Methods
int compareTo(String S) Returns the integer based on the result of the comparison.
ArrayList Class
The ArrayList class is a resizable array, which
can be found in the java.util package.

The difference between a user-defined array


and an ArrayList in Java

• Size of user-defined array cannot be modified while elements can be added and
removed from an ArrayList whenever you want.
ArrayList Class
Array Name

• Cars

Array Methods

• add() – insert the elements


• size() – returns the size of the array
• get() – retrieves the element

Other Array Methods for ArrayList


set() – modify the value of an element using the index (e.g. cars.set(0, “Honda”));
remove() – remove an element using the index (eg. cars.remove(i))
clear() – remove all elements in the array (eg. cars.clear())
ArrayList Class
Array Name

• myNumbers

Array Methods

• sort() – sort the elements alphabetically


or numerically (increasing order)

for-each loop
ArrayList Class

https://www.programiz.com/java-programming/enhanced-for-loop
Q&A

Any
Questions???
CP1 Java Methods (Part 2)

You might also like