Laboratory Exercise: 1.Write a main class for this class that will demonstrate a
ID: 3752603 • Letter: L
Question
Laboratory Exercise:
1.Write a main class for this class that will demonstrate array using ordered algorithm, using insert, delete and display methods.
Note: Include all the necessary error-checking for this program.
The program should have the following menu:
Menu
[1] Insert value
[2] Display arrays values
[3] Delete a value
[4] Exit
Enter your choice:
public class OrdArray {
private long [] a;
private int nElems;
public OrdArray(int max)
{
a= new long[max];
nElems = 0;
}
public int size (){ return nElems;
}
public int find( long searchKey)
{
int lowerBound = 0;
int upperBound = nElems-1;
int curIn;
while(true)
{
curIn = (lowerBound + upperBound ) / 2;
if (a[curIn] == searchKey)
return curIn;
else if (lowerBound > upperBound)
return nElems;
else
{
if(a[curIn] < searchKey)
lowerBound = curIn+1;
else
upperBound = curIn-1;
}
}
}
public void insert(long value)
{
int j;
for(j=0; j<nElems; j++)
if (a[j] > value )
break;
for ( int k = nElems; k>j; k--)
a[k] = a[k-1];
a[j] = value;
nElems++;
}
public boolean delete (long value)
{
int j = find (value);
if(j==nElems)
return false;
else
{
for(int k=j; k<nElems; k++)
a[k] = a[k+1];
nElems--;
return true;
}
}
public void display()
{
for(int j=0; j<nElems; j++)
System.out.print(a[j]+ " ");
System.out.println("");
}
}
Explanation / Answer
Below is the solution for the OrdArray.java class
create a class OrdArrayMain.java:
import java.util.Scanner;
public class OrdArrayMain {
public static void main(String[] args) {
int maxSize = 100; // array size
long random = 0; //create random variable
Scanner in = new Scanner(System.in); //scanner object for input
OrdArray arr; // reference to array
arr = new OrdArray(maxSize); //call the OedArray constructor with maxSize variable
do { //call do while until user enter the 4 option to exit
//Menu
System.out.println(" [1] Insert value " + "[2] Display arrays values " + "[3] Delete a value " + "[4] Exit");
System.out.println("Enter your choice:"); //enter choice
int option = in.nextInt(); //choice variable
//check if the user enter is 1 to insert into array using random generate variable of 1-100 number
if (option == 1) {
System.out.print("Inserted Array element is: ");
for (int i = 0; i < 15; i++) {
random = (long) (Math.random() * 100);
arr.insert(random); //call the insert method of OrdArray class
}
}
//check if the user enter is 2 display the iserted array element
else if (option == 2) {
System.out.print("Array is:");
arr.display(); //display array
//check if the user enter is 3 to delete the array variable
} else if (option == 3) {
//ask the user to enter the element to delete
System.out.println("Enter element to delete");
int del = in.nextInt();
arr.delete(del); //call the delete method of the OrdArray class
//check if the user enter is 4
} else if (option == 4) {
System.exit(0); //exit the code
}
//check if the user enter other than 1,2,3,4
else {
System.out.print("Wrong Choice!"); //print the message that wrong choice
}
} while (true);
} // end main()
}
sample output:
[1] Insert value
[2] Display arrays values
[3] Delete a value
[4] Exit
Enter your choice:
1
Inserted Array element is: 11 93 59 10 79 70 48 58 68 12 29 61 21 88 38
[1] Insert value
[2] Display arrays values
[3] Delete a value
[4] Exit
Enter your choice:
2
Array is:10 11 12 21 29 38 48 58 59 61 68 70 79 88 93
[1] Insert value
[2] Display arrays values
[3] Delete a value
[4] Exit
Enter your choice:
3
Enter element to delete
10
[1] Insert value
[2] Display arrays values
[3] Delete a value
[4] Exit
Enter your choice:
2
Array is:11 12 21 29 38 48 58 59 61 68 70 79 88 93
[1] Insert value
[2] Display arrays values
[3] Delete a value
[4] Exit
Enter your choice:
4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.