Module in CC 103 Second Sem AY 2023 24
Module in CC 103 Second Sem AY 2023 24
Arduo
NISULC-BSIT Associate Professor I
Second Semester AY 2023-24
Module 1, Lesson 1
Multi-dimensional Arrays
Introduction
Read the discussion and answer the programming problem exercises in this
manual.
Learning Outputs:
Upon completion of this Module, you shall be able to:
1. Test multi-dimensional arrays sample programs.
2. Answer and create multi-dimensional array programs as solutions to
programming problems.
Multidimensional Arrays
Multidimensional arrays are useful when you want to store data in a tabular form, like
a table with rows and columns.
To create a two-dimensional array, add each array within its own set of curly
braces:
Example
Access Elements
To access the elements of the myNumbers array, specify two indexes: one for the
array, and one for the element inside that array. This example accesses the third
element (2) in the second array (1) of myNumbers:
Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
System.out.println(myNumbers[1][2]); // Outputs 7
Remember that: Array indexes start with 0: [0] is the first element. [1] is the second
element, etc.
Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
myNumbers[1][2] = 9;
System.out.println(myNumbers[1][2]); // Outputs 9 instead of 7
We can also use a for loop inside another for loop to get the elements of a two-
dimensional array (we still have to point to the two indexes):
Example
System.out.println(myNumbers[i][j]);
}
}
Programming Exercises
1. Print the values 8, 3, 87, and 34 by accessing them from the given two-
dimensional array. Complete the program.
}
}
2. Create a program that displays all the possible products from the given values of
the two-dimensional array.
int[][] myNumbers = { {2, 5, 3}, {5, 6, 7} };
For example (2x5=10, 2x6=12, 2x7=14…)
3. From the sample array below, create a program that displays 3 friends in the two
sections.
String[][] friend = { {“Section A”,”Section B”}, {“Jen”, “Jill”, “Jake”,”Joy”,”Jerry”,”Jeff”} };
Module 1, Lesson 1
Java Methods
Introduction
In this module, you shall be introduced to Java Methods. You will test sample
programs in Java methods. You will be able to difference between a parameter and
an argument. You will also answer programming problems and create your programs
using different methods.
Read the discussion and answer the programming problem exercises in this
manual.
Learning Outputs:
Upon completion of this Module, you shall be able to:
1. Test sample programs that contain methods.
2. Describe the methods with different return types.
3. Explain the parameters and arguments.
4. Create methods with different return types.
Java Method
Methods are used to perform certain actions, and they are also known as functions.
Why use methods? To reuse code: define the code once, and use it many times.
Create a Method
A method must be declared within a class. It is defined with the name of the method,
followed by parentheses (). Java provides some pre-defined methods, such
as System.out.println(), but you can also create your own methods to perform certain
actions:
Example
// code to be executed
}
Example Explained
Call a Method
In the following example, myMethod() is used to print a text (the action), when it is
called:
Example
myMethod();
Example
myMethod();
myMethod();
myMethod();
Activity
Programming Exercises
1. Create a method that displays your name, age, and address. Call the method
in the program.
Java Method Parameters
Parameters and Arguments
Information can be passed to methods as a parameter. Parameters act as variables
inside the method.
Parameters are specified after the method name, inside the parentheses. You can
add as many parameters as you want, just separate them with a comma.
The following example has a method that takes a String called fname as parameter.
When the method is called, we pass along a first name, which is used inside the
method to print the full name:
Example
myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes
When a parameter is passed to the method, it is called an argument. So, from the
example above: fname is a parameter, while Liam, Jenny and Anja are arguments.
Multiple Parameters
Example
myMethod("Liam", 5);
myMethod("Jenny", 8);
myMethod("Anja", 31);
// Liam is 5
// Jenny is 8
// Anja is 31
Note that when you are working with multiple parameters, the method call must have
the same number of arguments as there are parameters, and the arguments must be
passed in the same order.
Activity:
Add a fname parameter of type String to myMethod, and output "John Doe":
Return Values
The void keyword, used in the examples above, indicates that the method should not
return a value. If you want the method to return a value, you can use a primitive data
type (such as int, char, etc.) instead of void, and use the return keyword inside the
method:
Example
return 5 + x;
System.out.println(myMethod(3));
// Outputs 8 (5 + 3)
Example
return x + y;
// Outputs 8 (5 + 3)
Example
public class Main {
return x + y;
System.out.println(z);
}// Outputs 8 (5 + 3)
Example
public class Main {
} else {
Programming Exercises
1. Write a Java method to find the smallest number among three numbers.
2. Write a Java method to check whether a year (integer) is a leap year or not.
CC 103: Computer Programming 2 Prof. Rene D. Arduo
NISULC-BSIT Associate Professor I
Second Semester AY 2023-24
Module 1, Lesson 1
String Methods
Introduction
In this module, you shall be introduced to Java String Methods. You will test
sample programs in Java String methods. You will also answer programming
problems and create your programs using different string methods.
Read the discussion and answer the programming problem exercises in this
manual.
Learning Outputs:
Upon completion of this Module, you shall be able to:
1. Test sample programs that contain String methods.
2. Apply String methods in answering programming exercises.
In Java, string is basically an object that represents sequence of char values. An array of
characters works same as Java string. For example:
char[] ch={'j','a','v','a','t','p','o','i','n','t'};
String s=new String(ch);
is same as:
String s="javatpoint";
Java String class provides a lot of methods to perform operations on strings such as
compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(),
substring() etc.
Method Description
codePointBefore() Returns the Unicode of the character before the specified index
codePointCount() Returns the number of Unicode values found in a string.
contentEquals() Checks whether a string contains the exact same sequence of characters
of the specified CharSequence or StringBuffer
copyValueOf() Returns a String that represents the characters of the character array
equals() Compares two strings. Returns true if the strings are equal, and false if not
format() Returns a formatted string using the specified locale, format string, and
arguments
getBytes() Encodes this String into a sequence of bytes using the named charset,
storing the result into a new byte array
indexOf() Returns the position of the first found occurrence of specified characters
in a string
lastIndexOf() Returns the position of the last found occurrence of specified characters
in a string
matches() Searches a string for a match against a regular expression, and returns
the matches
offsetByCodePoints() Returns the index within this String that is offset from the given index
by codePointOffset code points
replace() Searches a string for a specified value, and returns a new string where
the specified values are replaced
replaceFirst() Replaces the first occurrence of a substring that matches the given regular
expression with the given replacement
replaceAll() Replaces each substring of this string that matches the given regular expression
with the given replacement
The Java String class charAt() method returns a char value at the given index
number.
The index number starts from 0 and goes to n-1, where n is the length of the string. It
returns StringIndexOutOfBoundsException, if the given index number is greater
than or equal to this string length or a negative number.
Let's see Java program related to string in which we will use charAt() method that
perform some operation on the give string.
FileName: CharAtExample.java
Activity
if (Character.isUpperCase(c.charAt(x)))
System.out.println(c.charAt(x));
}//outputs WCP
Programming Exercises
1. Using the string “Welcome to Computer Programming 2”, display only the
numbers. Use for loop and if-statement.