Object-Oriented Programming Lab 5: Review: Ton Duc Thang University Faculty of Information Technology
Object-Oriented Programming Lab 5: Review: Ton Duc Thang University Faculty of Information Technology
OBJECT-ORIENTED PROGRAMMING
LAB 5: REVIEW
I. Objective
After completing this tutorial, we want you:
II. Array
1. Array are used to store multiple values in a single variable, instead of declaring separate
variables for each value:
String [] strings = {"IT", "TDTU", "HCM"};
int[] nums = {10, 20, 30, 40};
4. To find out how many elements an array has, use the length property:
int[] nums = {10, 20, 30, 40};
System.out.println(nums.length); // 4
5. You can loop through the array elements with the for loop, and use the length property to
specify how many times the loop should run.
int[] nums = {10, 20, 30, 40};
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
6. There is also a "for-each" loop, which is used exclusively to loop through elements in array:
III. String
1. Strings are used for storing text. A String variable contains a collection of characters
surrounded by double quotes:
String str = "Ton Duc Thang";
3. You can reference the individual characters in a string by using the method charAt() with
the same index that you would use for an array:
String str = "Ton Duc Thang";
System.out.println(str.charAt(0)); //T
5. You can use the concat() method to concatenate two strings. You can also concatenate two
strings to form another string by using the + operator:
String str1 = "Hello";
str1.concat(" HCM"); // Hello HCM
3. A class can contain the following types of variables: Local variables, instance variables, class
variables.
4. A class can also have methods. Method declarations have some components, in order: Modifiers, the
return type, the parameter list in parenthesis, the method body (the method body must be enclosed in
curly brackets).
5. Every class has a constructor. A constructor must have the same name as the class. A class can have
more than one constructor, but in most cases, you need to define at least three types of the constructor:
Default constructor, with no parameter; Parameterized constructor; Copy constructor.
6. Java provides several access modifiers to set access levels for classes, variables, methods, and
constructors. The four access levels: private, protected, default, public.
7. To achieve encapsulation in Java:
• Provide public getter and setter methods to modify and view the variable’s values.
public Student()
{
this.name = "";
this.gender = "male";
this.age = 0;
}
void studying()
{
System.out.println("studying...");
}
void reading()
{
System.out.println("reading...");
}
V. Exercises
Array
a) Write function public static int maxEven(int[] a) to find the greatest even number
in an array.
b) Write function public static int minOdd(int[] a) to find the smallest odd number in
an array.
c) Write function public static int sumMEMO(int[] a) to calculate the sum of the greatest
even number and the smallest odd number in an array.
d) Write function public static int sumEven(int[] a) to calculate the sum of even
numbers in an array.
e) Write function public static int prodOdd(int[] a) to calculate the product of odd
numbers in an array.
f) Write function public static int idxFirstEven(int[] a) return the position of the
first even number in the array.
g) Write function public static int idxLastOdd(int[] a) return the position of the last
odd number in the array.
h) Write function public static int[] input(int n) return an array with n elements
which input from keyboard.
• Write a function public static String shortName(String str) to first and last name.
• Write a main function public static void main(String []args) to test above
functions.
2) For the following paragraph: “The Edge Surf is of course also a whole lot better, which will
hopefully win Microsoft some converts. It offers time trial, support for other input methods like
touch and gamepads, accessibility improvements, high scores, and remastered visuals.”
• Write function public static int countWord(String paragraph) to count the
number of words in the paragraph.
• Write a main function public static void main(String []args) to test above
functions.
OOP, Class, Encapsulation
• name: String.
• Constructor with no parameter public Club() (name = “”, wins = 0, draws = 0, losses = 0).
• Constructor with parameters public Club(String name, int wins, int draws, int losses).
• public int numMatchesPlayed(): return the number of matches that club played
numMatches = win + draw + lose.
• public int isFinish(): Check if the club has finished the league yet. It is known that the
league has 10 matches.
• public int getPoints(): Return the number of points the club has received
points = win*3 + draw*1 + lose*0.
• name: String.
egdeAmount a
3 0.433
4 1
5 1.72
6 2.595
If a polygon has the number of edges greater than 6 return area = -1.
Write a test program (called TestRegularPolygon) to test all the methods defined.