In: Computer Science
IN Java please
1. Define a class called ListExercise containing one data field: a list of integer. Create two constructor methods, one accessor(getter) method and one mutator(setter) method for the ListExercise class.
2. Implement a reverse(int[] a) method to reverse the elements in the list. For example, reverse(Arrays.asList(new int[ ]{1,2,3})) should return {3,2,1}.
3. Implement a swapValue(List a, int i, int j) to swap the values in index i and j.
4. Implement a pickMost(int[] a) method to pick the most frequent value. For example pickMost(Arrays.asList(new int[] {1, 2, 3})) returns 1 or 2 or 3 and pickMost(Arrays.asList(new int[]{1,1,2})) returns 1.
5. Implement a checkPathenthesis(String exp) method to check if a string has matching pairs of parenthesis. For example, checkPathenthesis(“)))(((“) should return false and checkPathenthesis(“(())“) returns true.
import java.io.*;
import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
public class Main {
public static void main(String[] args)
{
ListExercise obj=new ListExercise(); //create an object of class
ListExercise
obj.setList(new int[] {1,2,3}); //Now using setter set the list to
{1,2,3}
int[] list=obj.getList(); //Now get the list using getter
method
int[] reversedList=reverse(list); //Get the reversed list using
reverse function
System.out.println("Reversed List");
for(int i=0;i<reversedList.length;i++)
{
System.out.println(reversedList[i]); //print the reversed
list
}
int frequent_element=pickMost(list);//pick the most frequent
element using pickMost Function
System.out.println("The most frequent element is " +
frequent_element); //print the most frquent element
System.out.println("Checking Paranthesis....");
System.out.println(checkParanthesis("(())"));//check the string
paranthesis is balanced or not using checkParanthesis
fucntion
}
public static int[] reverse(int[] list)
{
//method logic
int n=list.length;
int[] reverseList = new int[n];
int j = n,i;
for ( i = 0; i < n; i++) {
reverseList[j - 1] = list[i];
j = j - 1;
}
return reverseList;
}
public static void swapValue(int[] list, int i,int j)
{
//method logic
int temp=0;
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
public static int pickMost(int[] list)
{
//method logic
HashMap<Integer, Integer> countmap = new HashMap<Integer,
Integer>();
for (int i : list)
{
if (countmap.containsKey(i))
{
countmap.put(i, countmap.get(i)+1);//If there is an element in the
hashmap then increment its count by 1
}
else
{
countmap.put(i, 1);//If there is no element present then put the
element in the hashmap and initialize its value to 1
}
}
int element=0,frequency= 1;
Set<Entry<Integer, Integer>> Set =
countmap.entrySet();
for (Entry<Integer, Integer> item : Set)
{
if(item.getValue() > frequency)
{
element = item.getKey();
frequency = item.getValue();
}
}
if(frequency > 1) //if frequency is >1 that means there is an
element which occurs more than 1.Hence return that element
{
return element;
}
else //if frequency is 0 that means all the elements are
unique.Hence return any element. Here i choose first element i.e
list[0]
{
return list[0];
}
}
public static boolean checkParanthesis(String expr)
{
int i;
Deque<Character> stack = new ArrayDeque<Character>();
//here deque is used because it is faster when compared to stack.
You can also use stack if you want
for ( i = 0; i < expr.length(); i++) {
char ch = expr.charAt(i);
if (ch == '(' ) {
stack.push(ch); //pushes the element
continue;
}
if (stack.isEmpty()) //stack cannot be empty right now because
there should be right brackets ")" inorder to balance the string.
If you find the stack is empty just return false
return false;
switch (ch) {
case ')':
stack.pop();
break;
}
}
if(stack.isEmpty()) //Now all the brackets are balanced. If stack
is empty i.e string is balanced. now return true
{
return true;
}
return false; //if stack is not empty i.e brackets are not
balanced. then return false
}
}
class ListExercise // declare a class with name
ListExercise
{
int[] list = new int[10];
public int[] getList() { //declare getter method
return list;
}
public void setList(int[] list) { //declare setter method
this.list = list;
}
}
//Output of the program is shown in the image uploaded.
//Please dont forget to give positive rating if you like my work.(This is a comment,you can delete it)