Question

In: Computer Science

IN Java please 1. Define a class called ListExercise containing one data field: a list of...

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.

Solutions

Expert Solution

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)


Related Solutions

in java please: Create an ArrayListReview class with one data field of ArrayList and one with...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points)
In java, please Create an ArrayListReview class with one data field of ArrayList with the generic...
In java, please Create an ArrayListReview class with one data field of ArrayList with the generic type passed to the class. (1 point) Create a constructor that populate an array list filled with the generic type through inserting new elements into the specified location index-i in the list. (1 point) Implement mergeSort using a list and recursion. (2 points) Write the main method to test your program and use System.nanoTime() to find out the speed of each step of your...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
Java Define a class called BlogEntry that could be used to store an entry for a...
Java Define a class called BlogEntry that could be used to store an entry for a Web log. The class should have instance variables to store : - the poster’s username, - text of the entry, - and the date of the entry using the Date class Date class is: class Date { private int day, year; private String mon; public Date() { mon=" "; day=0; year=0; } public String toString() { return (mon+"/"+day+"/"+year); } public set_date(String m, int d,...
1. Define a class named Book that contains:  An int data field named pages that...
1. Define a class named Book that contains:  An int data field named pages that stores the number of pages in the book.  A String data field named title that represents the title of the book.  A constructor with parameters for initializing pages and title.  The getter and setter methods for all data fields.  A toString method that returns book information, including the book title and pages.  The equals method that returns true if...
In Java Define the EvenNumber class for representing an even number. The class contains: A data...
In Java Define the EvenNumber class for representing an even number. The class contains: A data field value of the int type that represents the integer value stored in the object. A no-arg constructor that creates an EvenNumber object for the value 0. A constructor that constructs an EvenNumber object with the specified value. A function named getValue() to return an int value for this object. A function named getNext() to return an EvenNumber object that represents the next even...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This method accepts from a user (1) an integer hourly rate and (2) an integer total number of hours worked in a week. It calculates and displays the total weekly wage of an employee. The company pays straight time for the first 40 hours worked by an employee and times and a half for all the hours worked in excess of 40. This class should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT