package p5_unsortedArrayAccess; import java.util.*; public class unsortedArrayAc
ID: 3745754 • Letter: P
Question
package p5_unsortedArrayAccess;
import java.util.*;
public class unsortedArrayAccess {
public static double[] arr; private int arraySize;
public unsortedArrayAccess(int scale) { arr = new double[scale]; arraySize = 0; }
public double get(int i) { return arr[i]; }
public int search(double Key) { int i = 0; while ((arr[i] != Key) && (i < arraySize)) { i = i + 1; } if (i < arraySize) { return i; } else { System.out.println("There is no such item!"); return -1; } }
public void append(double Item) { arr[arraySize] = Item; arraySize = arraySize + 1; }
public double remove() { if (arraySize == 0) { System.out.println("There is no item in the array!"); return -1; } double x = arr[arraySize - 1]; arraySize = arraySize - 1; return x; }
public void deletion(double Key) { int k = search(Key); if (k != -1) { for (int i = k; i < arraySize; i++) { arr[i] = arr[i + 1]; } arraySize = arraySize - 1; }; }
public void display() { if (arraySize == 0) { System.out.println("Array is empty!"); } else { for (int i = 0; i < arraySize; i++) { System.out.println(arr[i]); } };
System.out.println("array size is " + arraySize); } } package p5_unsortedArrayAccess;
import java.util.*;
public class unsortedArrayAccess {
public static double[] arr; private int arraySize;
public unsortedArrayAccess(int scale) { arr = new double[scale]; arraySize = 0; }
public double get(int i) { return arr[i]; }
public int search(double Key) { int i = 0; while ((arr[i] != Key) && (i < arraySize)) { i = i + 1; } if (i < arraySize) { return i; } else { System.out.println("There is no such item!"); return -1; } }
public void append(double Item) { arr[arraySize] = Item; arraySize = arraySize + 1; }
public double remove() { if (arraySize == 0) { System.out.println("There is no item in the array!"); return -1; } double x = arr[arraySize - 1]; arraySize = arraySize - 1; return x; }
public void deletion(double Key) { int k = search(Key); if (k != -1) { for (int i = k; i < arraySize; i++) { arr[i] = arr[i + 1]; } arraySize = arraySize - 1; }; }
public void display() { if (arraySize == 0) { System.out.println("Array is empty!"); } else { for (int i = 0; i < arraySize; i++) { System.out.println(arr[i]); } };
System.out.println("array size is " + arraySize); } } 4. Programming: .1 Download and study program P-1 Add a testing class that can test all the operations defined in the unsortedArray Access class. Make your testing menu-driven.
Explanation / Answer
Explanation::
Code in JAVA::
import java.util.Scanner;
public class testingUnsortedArray {
public static void main(String[] args) {
/**
* Scanner class is used to take input from user
* */
Scanner sc=new Scanner(System.in);
unsortedArrayAccess uAA=new unsortedArrayAccess(100);
while(true) {
System.out.println("_____________Menu_____________");
System.out.println("1.Get value by position");
System.out.println("2.Add an Item into the Array");
System.out.println("3.Remove last Item");
System.out.println("4.Delete any particular Item");
System.out.println("5.Diplay the Array");
System.out.println("6.Exit");
System.out.print("Your Choice ::");
int ch=sc.nextInt();
if(ch==6) {
break;
}
double val;
switch(ch) {
case 1:
System.out.print(" Enter the position ::");
int position=sc.nextInt();
val=uAA.get(position-1);
System.out.println("Value at "+position+" is "+val);
break;
case 2:
System.out.print(" Enter the Item to append ::");
val=sc.nextDouble();
uAA.append(val);
System.out.println("Item added..");
break;
case 3:
val=uAA.remove();
if(val!=-1) {
System.out.println("Item removed from end is "+val);
}
break;
case 4:
System.out.print("Enter value of Item to delete ::");
val=sc.nextDouble();
uAA.deletion(val);
System.out.println("Item removed..");
break;
case 5:
uAA.display();
break;
default:
System.out.println("Invalid Choice");
break;
}/*Switch case ended*/
System.out.println(" ");
}/*While loop end*/
}/*Main method ends here*/
}/*Class ends here*/
Output::
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::2
Enter the Item to append ::34
Item added..
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::2
Enter the Item to append ::89
Item added..
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::5
34.0
89.0
array size is 2
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::4
Enter value of Item to delete ::89
Item removed..
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::5
34.0
array size is 1
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::3
Item removed from end is 34.0
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::5
Array is empty!
array size is 0
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::2
Enter the Item to append ::1
Item added..
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::2
Enter the Item to append ::30
Item added..
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::2
Enter the Item to append ::70
Item added..
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::1
Enter the position ::2
Value at 2 is 30.0
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::5
1.0
30.0
70.0
array size is 3
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::4
Enter value of Item to delete ::1
Item removed..
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::5
30.0
70.0
array size is 2
_____________Menu_____________
1.Get value by position
2.Add an Item into the Array
3.Remove last Item
4.Delete any particular Item
5.Diplay the Array
6.Exit
Your Choice ::6
Please provide feedback!!
Thank You!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.