Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CS212T: DATA STRUCTURE TERM: 1ST TERM(2022)

LAB: 03

General notes:

• Each exercise is a separate project and should be saved in a different folder. The name of the

folder is the exercise number.

• Write your name, ID, and PC number as a comment on the main class.

• Zip your folders into one folder and upload your answer to the blackboard before the end of

the class. Late submission is considered absent


Exercise 1: Palindrome

palindrome means a word, phrase, or sequence that can be read the same backwards as forward,
so it well be the same sequence after reverse e.g. 123454321 , 9876789 , mom, ABCBA. Your
program have to check if array elements are palindrome.

Write a Java program that:

a. Ask the user to input the size of an integer array


b. Then read array elements from user using a recursion method named readElement(int[]a)
c. Then call recursion method named isPalindrome
• in this method you have to check whether the element from starting is same as the
element from the end , otherwise the array is not a palindrome.
d. For example:
arr[] = {3, 6, 0, 6, 3} ->Palindrome

arr[] = {1, 2, 3, 4, 5} -> Not Palindrome

Lab Exercise 2: Balanced Parenthesis

Balanced Parenthesis or valid Parenthesis occur when all the parenthesis match up correctly and
each open parenthesis '(' is closed by ')' , as shown in the following example: x*((x+x)-y). But in
case open parenthesis are more than close parenthesis or vice versa then parenthesis are not balanced
e.g. x+((y-z) , (x-z))+y . Your program should check if the parenthesis are balanced in an array of
characters.

Write a Java program that:

a. Ask the user to input the size of a character array


b. Then read array elements from user. Notice that your characters array could contains
parenthesis '(' or ')' for example your array elements could be : CS(212)t
c. Then call method numberofParenthesis to check if array is Balanced
• numberofParenthesis: a recursive method that take array of characters and last index
int numberofParenthesis(char[] a, int index).

2
• The method should calculate number of parenthesis, use counter to keep tracking number
of parenthesis, so increment the counter if character is '('. But if the character is the
closing ')' decrement the counter
▪ if a[index]==’(‘ then counter incremented
▪ if a[index]==’)‘ then counter decremented
• The method should return number of parenthesis in the array, if the number=0 then array
is balanced, otherwise array is not balanced
▪ if the counter==0 this means all open Parenthesis are closed
▪ if the counter>0 this means open Parenthesis are more than close Parenthesis
▪ if the counter<0 this means close Parenthesis are more than open Parenthesis

Lab Exercise 3: Calculate Average GPA


A. Write a class Student which contains the following data and methods
(use the class you created in LAB2)

Data:
• Name (string)
• ID (integer)
• Level (integer)
• GPA (double)
Methods:
• Get and set for each data item
• Print all students

B. Main class contain :


• BinarySum Method:
o Binary recursion method which receives an array of students, and calculate the sum of the
GPA of all students then returns the sum.

• Main Method:
o Create an array of 4 students then assign values for each student (no need to read
information from user).
o Compute the GPA sum of all students using method BinarySum
o Compute the average GPA of all students

You might also like