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

Lab2_OOP

This document is a tutorial for an Object-Oriented Programming lab focusing on Java, specifically on arrays and object creation. It explains how to declare and initialize arrays, provides examples of using arrays and the BigDecimal class, and discusses the differences between static and non-static methods. Additionally, it includes exercises for students to practice their skills in working with arrays and BigDecimal objects.

Uploaded by

hungpro123b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab2_OOP

This document is a tutorial for an Object-Oriented Programming lab focusing on Java, specifically on arrays and object creation. It explains how to declare and initialize arrays, provides examples of using arrays and the BigDecimal class, and discusses the differences between static and non-static methods. Additionally, it includes exercises for students to practice their skills in working with arrays and BigDecimal objects.

Uploaded by

hungpro123b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ton Duc Thang University

Faculty of Information Technology

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;

for (int i = 0; i < a.length; i++) {


System.out.print(a[i] + " ");
}
System.out.println();
int sum = 0;
for (int i = 0; i < b.length; i++) {
sum = sum + b[i];
}
System.out.println("sum = " + sum);
}
}

[email protected] | Object Oriented Programming (503005) – 2024 1/5


Ton Duc Thang University
Faculty of Information Technology

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);
}
}

III. Classes and objects in Java


Java is an Object-Oriented Programming (OOP) language. Everything you work in Java is through
classes and objects. In this lab, we only learn how to create an object from the available class in Java,
we will learn about this topic more carefully in Lab 4.
BigDecimal is a class, which is defined in java.math package. This class provides operations for
arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. Besides that,
the BigDecimal class gives its user complete control over rounding behaviour. You can read more about
this class here.
Object is an instance of a Class.

[email protected] | Object Oriented Programming (503005) – 2024 2/5


Ton Duc Thang University
Faculty of Information Technology

Example:

import java.math.BigDecimal;

public class Test {


public static void main(String[] args) {
BigDecimal num = new BigDecimal(1);
BigDecimal num1 = new BigDecimal(4);
BigDecimal x = num;
// BigDecimal y;
System.out.println(num);
System.out.println(num1);
System.out.println(x);
// System.out.println(y);
}
}

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, …])

Let's observe another example:

import java.math.BigDecimal;

public class Test1 {


public static void main(String[] args) {
BigDecimal x = new BigDecimal(4);
BigDecimal y = new BigDecimal(4);

System.out.println(x == y);
System.out.println(x.equals(y));
}
}

Output:

false
true

[email protected] | Object Oriented Programming (503005) – 2024 3/5


Ton Duc Thang University
Faculty of Information Technology

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);

//abs() is a non-static method of the BigDecimal class


BigDecimal y = x.abs();

//valueOf() is a static method of the BigDecimal class


BigDecimal z = BigDecimal.valueOf(20.22);

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.

[email protected] | Object Oriented Programming (503005) – 2024 4/5


Ton Duc Thang University
Faculty of Information Technology

8. Write a function public static BigDecimal findMax(BigDecimal []arr) to find


the maximum value of a BigDecimal object array.
9. Write a function public static int[] divisibleNumbers(int arr[], int k) to
find the elements divisible by k in an array. (Hint: You can use two loops to solve it. The first
loop is used to count how many possible elements. Create a new array with a length equal to the
number counted in the first loop. The second loop is used to put all possible elements into the
array. Ex: a = [1,2,3,4,5,6,7] with k = 2 → [2,4,6]).
10. Write a function to find the third largest element in an array.

-- END --

[email protected] | Object Oriented Programming (503005) – 2024 5/5

You might also like