Java Methods: Course Packet
Java Methods: Course Packet
Java Methods
ICTC0423 Computer Programming 2
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();
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
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.
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.
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
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
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.
• 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
• myNumbers
Array Methods
for-each loop
ArrayList Class
https://www.programiz.com/java-programming/enhanced-for-loop
Q&A
Any
Questions???
CP1 Java Methods (Part 2)