package p5_SortedArrayAccess; import java.util.*; public class sortedArrayAccess
ID: 3745746 • Letter: P
Question
package p5_SortedArrayAccess;
import java.util.*;
public class sortedArrayAccess { public static double[] arr; private int arraySize;
public sortedArrayAccess(int scale) { arr = new double[scale]; arraySize = 0; }
public final double get(int i) { return arr[i]; }
public final int BinarySearch(double Key) { int k = 0; int lower = 0; int upper = arraySize - 1; while (lower < upper) { k = (lower + upper + 1) / 2; if (Key == arr[k]) { break; } if (Key < arr[k]) { upper = k - 1; } else { lower = k + 1; } } if (lower == upper) { k = lower; } if (Key == arr[k]) { return k; } else { System.out.println("The item cannot be found!"); return -1; } }
public final void insertion(double Key, double Item) { if (arraySize == 0) { arr[0] = Item; } /* find the position for interting the given item */ int position = 0; while (Key > arr[position] && position < arraySize) { position++; } for (int i = arraySize; i > position; i--) { arr[i] = arr[i - 1]; } arr[position] = Item; arraySize = arraySize + 1;
}
public final void deletion(double Key) { /* find the given item */ int position = BinarySearch(Key); if (position != -1) { for (int i = position; i < arraySize - 1; i++) { arr[i] = arr[i + 1]; } arraySize = arraySize - 1; }; }
public final void display() { if (arraySize != 0) { for (int i = 0; i < arraySize; i++) { System.out.println(arr[i]); } };
System.out.println("The number of items is " + arraySize); } } package p5_SortedArrayAccess;
import java.util.*;
public class sortedArrayAccess { public static double[] arr; private int arraySize;
public sortedArrayAccess(int scale) { arr = new double[scale]; arraySize = 0; }
public final double get(int i) { return arr[i]; }
public final int BinarySearch(double Key) { int k = 0; int lower = 0; int upper = arraySize - 1; while (lower < upper) { k = (lower + upper + 1) / 2; if (Key == arr[k]) { break; } if (Key < arr[k]) { upper = k - 1; } else { lower = k + 1; } } if (lower == upper) { k = lower; } if (Key == arr[k]) { return k; } else { System.out.println("The item cannot be found!"); return -1; } }
public final void insertion(double Key, double Item) { if (arraySize == 0) { arr[0] = Item; } /* find the position for interting the given item */ int position = 0; while (Key > arr[position] && position < arraySize) { position++; } for (int i = arraySize; i > position; i--) { arr[i] = arr[i - 1]; } arr[position] = Item; arraySize = arraySize + 1;
}
public final void deletion(double Key) { /* find the given item */ int position = BinarySearch(Key); if (position != -1) { for (int i = position; i < arraySize - 1; i++) { arr[i] = arr[i + 1]; } arraySize = arraySize - 1; }; }
public final void display() { if (arraySize != 0) { for (int i = 0; i < arraySize; i++) { System.out.println(arr[i]); } };
System.out.println("The number of items is " + arraySize); } }
Explanation / Answer
Question 1
package unsortedArrayAccess;
import java.util.Scanner;
/**
* Menu driven program to test unsortedArrayAccess
* @author
*/
public class TestUnSortedArray {
private static final int MAX_ARRAYSIZE=20;//Size of the array to be created
public static void main(String[] args) {
unsortedArrayAccess arr=new unsortedArrayAccess(MAX_ARRAYSIZE);//create an unsorted array of max size
int choice;
Scanner in=new Scanner(System.in);
while(true)
{
System.out.println("0.Get Item at:");//display menu
System.out.println("1.Search");//display menu
System.out.println("2.Append");
System.out.println("3.Remove");
System.out.println("4.Deletion");
System.out.println("5.Display");
System.out.println("6.Exit");
System.out.println("Enter your option:(0-6)");//get option
choice=in.nextInt();
switch(choice)
{
case 0:
System.out.println("Enter loc of item:");
int pos=in.nextInt();
System.out.println("Item:"+arr.get(pos));
break;
case 1:
System.out.println("Enter key to be searched:");
int loc=arr.search(in.nextDouble());//returns loc at which item is found
if(loc>-1)
System.out.println("Found at location:"+loc);
break;
case 2:
System.out.println("Enter item to be appended:");
arr.append(in.nextDouble());
break;
case 3:
double item=arr.remove();//removes from end of array
if(item>-1)
System.out.println("Item removed:"+item);
break;
case 4:
System.out.println("Enter item to be deleted:");
arr.deletion(in.nextDouble());
break;
case 5:
arr.display();
break;
case 6:
System.exit(0);
default:
System.out.println("Enter a valid option");
}//switch end
}//while end
}
}
Sample Output Question 1
run:
0.Get Item at:
1.Search
2.Append
3.Remove
4.Deletion
5.Display
6.Exit
Enter your option:(0-6)
0
Enter loc of item:
7
Item:0.0
0.Get Item at:
1.Search
2.Append
3.Remove
4.Deletion
5.Display
6.Exit
Enter your option:(0-6)
1
Enter key to be searched:
23
There is no such item!
0.Get Item at:
1.Search
2.Append
3.Remove
4.Deletion
5.Display
6.Exit
Enter your option:(0-6)
2
Enter item to be appended:
34
0.Get Item at:
1.Search
2.Append
3.Remove
4.Deletion
5.Display
6.Exit
Enter your option:(0-6)
2
Enter item to be appended:
23
0.Get Item at:
1.Search
2.Append
3.Remove
4.Deletion
5.Display
6.Exit
Enter your option:(0-6)
2
Enter item to be appended:
12
0.Get Item at:
1.Search
2.Append
3.Remove
4.Deletion
5.Display
6.Exit
Enter your option:(0-6)
2
Enter item to be appended:
3
0.Get Item at:
1.Search
2.Append
3.Remove
4.Deletion
5.Display
6.Exit
Enter your option:(0-6)
3
Item removed:3.0
0.Get Item at:
1.Search
2.Append
3.Remove
4.Deletion
5.Display
6.Exit
Enter your option:(0-6)
4
Enter item to be deleted:
12
0.Get Item at:
1.Search
2.Append
3.Remove
4.Deletion
5.Display
6.Exit
Enter your option:(0-6)
5
34.0
23.0
array size is 2
0.Get Item at:
1.Search
2.Append
3.Remove
4.Deletion
5.Display
6.Exit
Enter your option:(0-6)
6
BUILD SUCCESSFUL (total time: 56 seconds)
Question 2
package SortedArrayAccess;
import java.util.Scanner;
/**
* Menu driven program to test sortedArrayAccess
* @author
*/
public class TestSortedArray {
private static final int MAX_ARRAYSIZE=20;//Size of the array to be created
public static void main(String[] args) {
sortedArrayAccess arr=new sortedArrayAcess(MAX_ARRAYSIZE);//create an array of max size
int choice;
Scanner in=new Scanner(System.in);
while(true)
{
System.out.println("0.Get Item at:");
System.out.println("1.Search");//display menu
System.out.println("2.Append");
System.out.println("3.Deletion");
System.out.println("4.Display");
System.out.println("5.Exit");
System.out.println("Enter your option:(0-5)");//get option
choice=in.nextInt();
switch(choice)
{
case 0:
System.out.println("Enter loc of item:");
int pos=in.nextInt();
System.out.println("Item:"+arr.get(pos));
break;
case 1:
System.out.println("Enter item to be searched:");
int loc=arr.BinarySearch(in.nextDouble());//returns loc at which item is found
if(loc>-1)
System.out.println("Found at location:"+loc);
break;
case 2:
double key,item;
System.out.println("Enter key of item to be inserted:");
key=in.nextDouble();
System.out.println("Enter item to be inserted:");
item=in.nextDouble();
arr.insertion(key,item);
break;
case 3:
System.out.println("Enter item to be deleted:");
arr.deletion(in.nextDouble());
break;
case 4:
arr.display();
break;
case 5:
System.exit(0);
default:
System.out.println("Enter a valid option");
}//switch end
}//while end
}
}
Sample Output Question 2
run:
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
1
Enter key to be searched:
3
The item cannot be found!
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
2
Enter key of item to be inserted:
23
Enter item to be inserted:
34
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
3
Enter item to be deleted:
23
The item cannot be found!
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
34
Enter a valid option
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
3
Enter item to be deleted:
34
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
4
The number of items is 0
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
2
Enter key of item to be inserted:
20
Enter item to be inserted:
34
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
2
Enter key of item to be inserted:
10
Enter item to be inserted:
12
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
4
12.0
34.0
The number of items is 2
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
2
Enter key of item to be inserted:
23
Enter item to be inserted:
4
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
4
12.0
4.0
34.0
The number of items is 3
0.Get Item at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(1-5)
5
Question 3
package SortedArrayAccess;
import java.util.*;
public class sortedArrayAccess
{
public static employee[] arr;
private int arraySize;
public sortedArrayAccess(int scale)
{
arr = new employee[scale];
arraySize = 0;
}
public final employee get(int i)
{
return arr[i];
}
public final int BinarySearch(double Key)
{
int k = 0;
int lower = 0;
int upper = arraySize - 1;
while (lower < upper)
{
k = (lower + upper + 1) / 2;
if (arr[k]!=null &&Key == arr[k].id )
{
break;
}
if (arr[k]!=null &&Key < arr[k].id)
{
upper = k - 1;
}
else
{
lower = k + 1;
}
}
if (lower == upper)
{
k = lower;
}
if (arr[k]!=null && Key == arr[k].id )
{
return k;
}
else
{
System.out.println("The item cannot be found!");
return -1;
}
}
public final void insertion(int Key, employee Item)
{
if (arraySize == 0)
{
arr[0] = Item;
}
/* find the position for interting the given item */
int position = 0;
while (position < arraySize &&Key > arr[position].id )
{
position++;
}
for (int i = arraySize; i > position; i--)
{
arr[i] = arr[i - 1];
}
arr[position] = Item;
arraySize = arraySize + 1;
}
public final void deletion(int Key)
{
/* find the given item */
int position = BinarySearch(Key);
if (position != -1)
{
for (int i = position; i < arraySize - 1; i++)
{
arr[i] = arr[i + 1];
}
arraySize = arraySize - 1;
};
}
public final void display()
{
if (arraySize != 0)
{
for (int i = 0; i < arraySize; i++)
{
System.out.println(arr[i]);
}
};
System.out.println("The number of items is " + arraySize);
}
public static void main (String [] args){
}
}
Test Class for Q 3
package SortedArrayAccess;
import java.util.Scanner;
/**
* Menu driven program to test sortedEmployee
* @author
*/
public class TestSortedEmployee {
private static final int MAX_ARRAYSIZE=20;//Size of the array to be created
public static void main(String[] args) {
sortedArrayAccess arr=new sortedArrayAccess(MAX_ARRAYSIZE);//create an array of max size
int choice;
Scanner in=new Scanner(System.in);
while(true)
{
System.out.println("0.Get Employee at:");
System.out.println("1.Search");//display menu
System.out.println("2.Append");
System.out.println("3.Deletion");
System.out.println("4.Display");
System.out.println("5.Exit");
System.out.println("Enter your option:(0-5)");//get option
choice=in.nextInt();
switch(choice)
{
case 0:
System.out.println("Enter loc of employee:");
int pos=in.nextInt();
System.out.println("Employee:"+arr.get(pos));
break;
case 1:
System.out.println("Enter id of employee to be searched:");
int loc=arr.BinarySearch(in.nextInt());//returns loc at which item is found
if(loc>-1)
System.out.println("Found at location:"+loc);
break;
case 2:
employee emp=new employee();
emp.Input();
arr.insertion(emp.id,emp);
break;
case 3:
System.out.println("Enter id of employee to be deleted:");
arr.deletion(in.nextInt());
break;
case 4:
arr.display();
break;
case 5:
System.exit(0);
default:
System.out.println("Enter a valid option");
}//switch end
}//while end
}
}
Sample Output Q 3
run:
0.Get Employee at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(0-5)
2
Enter name:
sdd
Enter id:
12
Enter salary:
11233
0.Get Employee at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(0-5)
2
Enter name:
sewe
Enter id:
23
Enter salary:
124344
0.Get Employee at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(0-5)
0
Enter loc of employee:
0
Employee:employee{id=12, name=sdd, salary=11233.0}
0.Get Employee at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(0-5)
1
Enter id of employee to be searched:
12
Found at location:0
0.Get Employee at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(0-5)
4
employee{id=12, name=sdd, salary=11233.0}
employee{id=23, name=sewe, salary=124344.0}
The number of items is 2
0.Get Employee at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(0-5)
3
Enter id of employee to be deleted:
12
0.Get Employee at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(0-5)
4
employee{id=23, name=sewe, salary=124344.0}
The number of items is 1
0.Get Employee at:
1.Search
2.Append
3.Deletion
4.Display
5.Exit
Enter your option:(0-5)
5
BUILD SUCCESSFUL (total time: 46 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.