Lab2_OOP
Lab2_OOP
OBJECT-ORIENTED PROGRAMMING
LAB 2: JAVA, HOW TO PROGRAM (CONT.)
I. Objective
In this second tutorial, you will:
• Practice with an array in Java.
• Have basic knowledge about the object, how to create the object in Java, and practice it with the
sample class defined by Java.
II. Array
Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of
the same type.
To declare an array, you can use this statement:
dataType[] arrayName;
You can also place the brackets after the array's name:
dataType arrayName[];
But we encourage you should use the first form to declare an array.
There are two basic ways to initialize an array. You can use the new operator or use the braces to list
the values:
dataType[] arrayName = new dataType[arraySize];
dataType[] arrayName = {value0, value1, ..., valueK};
Example:
public class MyProgram {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
int[] b = new int[3];
b[0] = 1;
b[1] = 2;
b[2] = 3;
Another way, you can use this statement to create an anonymous array:
new dataType[] {value0, value1, ..., valueK};
Example:
public class MyProgram {
public static int sum(int[] arr) {
int result = 0;
for(int i = 0; i < arr.length; i++) {
result += arr[i];
}
return result;
}
public static void main(String[] args) {
System.out.println("sum = " + sum(new int[] {2,4,6,8}));
}
}
We have a special for loop in the above sample called enhanced for loop. The enhanced for loop is
mainly used to traverse a collection of elements including arrays. The syntax is as follows.
Example:
public class MyProgram {
public static void main(String[] args) {
int[] a = {1, 3, 5, 7, 9};
for (int x : a) {
System.out.println(x);
}
int sum = 0;
for (int x : a) {
sum = sum + x;
}
System.out.println("sum = " + sum);
}
}
Example:
import java.math.BigDecimal;
Output:
1
4
1
With the above example, the variables: num, num1, x, and y were created from the BigDecimal class. In
other words, the variables: num, num1, x, and y are the pointers to the objects. However, variable y hasn't
been initialized. Therefore, the value of that variable will be undermined until an object is created and
assigned to it. If you try using an uninitialized variable, you will get a compiler error.
The format of code when you want to create an object from the class is:
ClassName instanceName = new ClassConstructor([paramater1, parameter2, …])
import java.math.BigDecimal;
System.out.println(x == y);
System.out.println(x.equals(y));
}
}
Output:
false
true
With objects, we don’t use the operator "==" to compare values, because this operator will compare the
addresses and there are the pointers so they have different addresses. BigDecimal supports the method
called equals to compare the value of two BigDecimal objects.
Next, we will learn how to invoke the non-static method and static method from a class. To invoke a
static method, you do not need to create an object. Instead, you can invoke directly from the class name.
Contrarily, with the non-static method, you need to create an object to invoke it.
Example:
import java.math.BigDecimal;
class Test2 {
public static void main(String[] args) {
BigDecimal x = new BigDecimal(-4);
System.out.println(y);
System.out.println(z);
}
}
Output:
4
20.22
IV. Exercises
1. Write a function public static int findMax(int arr[]) to find the maximum value
of an array.
2. Write a function to find the minimum value of an array.
3. Write a function to sum all even numbers of an array.
4. Write a function to count how many specific elements are in an array.
5. Write a function to count how many prime numbers are in an array.
6. Write a function public static int find(int arr[], int k) to find the index of an
element k in an array, if the element doesn’t exist in an array return -1. (the first element index
is 0)
7. Write a function public static void square(int arr[]) to square all elements of an
array.
-- END --