each b for int i 5 i 0 i ali ali 1 c for int i 0 i 0 i2 a
Solution
//Collection.java
public class Collection {
//declare instance variables
private int arr[];
private int size;
private int index;
//Constructor that takes array size
public Collection(int arrSize) {
size=arrSize;
arr=new int[size];
index=0;
}
//Method to add number to array
public boolean addNumber(int number) {
if(index<size)
{
arr[index]=number;
index++;
return true;
}
else
return false;
}
//Method to remove element from the array
public boolean remove(int element)
{
int pos=-1;
boolean found=false;
for (int i = 0; i < arr.length &&!found; i++)
{
if(arr[i]==element)
{
pos=i;
found=true;
}
}
if(found)
{
for (int i = pos; i < arr.length-1; i++)
{
arr[i]=arr[i+1];
}
index--;
}
return found;
}
//Method to rotate right by count
public void rotate(int count)
{
for (int i = 0; i < count; i++)
{
int first=arr[0];
for (int j = 0; j < index-1; j++)
{
arr[j]=arr[j+1];
}
arr[index-1]=first;
}
}
//method to return count of the elements in array
public int getCount() {
return index;
}
//Method that swap the first element with smallest
public int[] swapSmallest() {
int[] temp=new int[index];
int smallest=arr[0];
for (int i = 0; i < temp.length; i++)
{
temp[i]=arr[i];
}
for (int i = 1; i < temp.length; i++)
{
if(temp[i]<smallest)
smallest=temp[i];
}
temp[0]=smallest;
return temp;
}
//overrides the toString method that returns
//the string description of array
@Override
public String toString() {
String list=\"\";
for (int i = 0; i < index; i++)
{
list+=arr[i]+\",\";
}
return list;
}
}
------------------------------------------------------------------------------------------------------------------------------------
//Assignment7.java
import java.util.Scanner;
public class Assignment7
{
public static void main(String[] args)
{
int number, size;
String choice;
char command;
Scanner keyboard = new Scanner(System.in);
// ask a user for a array size
System.out.println(\"Please enter a size for the array.\ \");
size = Integer.parseInt(keyboard.nextLine());
// instantiate a NumberCollection object
Collection collection = new Collection(size);
// print the menu
printMenu();
do
{
// ask a user to choose a command
System.out.println(\"\ Please enter a command or type ?\");
choice = keyboard.nextLine().toLowerCase();
command = choice.charAt(0);
switch (command)
{
case \'a\': // add a number
System.out.println(\"\ Please enter an integer to add.\");
number = Integer.parseInt(keyboard.nextLine());
if(collection.addNumber(number))
System.out.println(\"\ \" + number + \" successfully added.\");
else
System.out.println(\"\ \" + number + \" is already in the array. \" + number + \" was not added.\");
break;
case \'b\':
System.out.println(\"Please enter an integer to remove.\");
int element=Integer.parseInt(keyboard.nextLine());
if(collection.remove(element))
System.out.println(element+\" successfully removed.\");
else
System.out.println(\"Not found\");
break;
case \'c\': //display
System.out.println(collection.toString());
break;
case \'d\': //swap the first element with smallest
int[] temp = collection.swapSmallest();
for (int i=0; i< collection.getCount(); i++)
System.out.print(temp[i] + \" \" );
System.out.println();
break;
case \'e\':
System.out.println(\"Enter rotation count:\");
//prompt for rotation value
int rotation=Integer.parseInt(keyboard.nextLine());
//Calling rotat method
collection.rotate(rotation);
System.out.println(\"The rotated array is:\"+collection.toString());
break;
case \'?\':
printMenu();
break;
case \'q\':
System.out.println(\"Thank you!\");
//exit from the program
System.exit(0);
break;
default:
System.out.println(\"Invalid input!\");
}
} while (command != \'q\');
} //end of the main method
// this method prints out the menu to a user
public static void printMenu()
{
System.out.print(\"\ Command Options\ \"
+ \"-----------------------------------\ \"
+ \"a: add an integer in the array\ \"
+ \"b: remove an integer from the array\ \"
+ \"c: display the array\ \"
+ \"d: swap with smallest\ \"
+ \"e: rotate\ \"
+ \"?: display the menu again\ \"
+ \"q: quit this program\ \ \");
} // end of the printMenu method
} // end of the Assignment7 class
------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Please enter a size for the array.
3
Command Options
-----------------------------------
a: add an integer in the array
b: remove an integer from the array
c: display the array
d: swap with smallest
e: rotate
?: display the menu again
q: quit this program
Please enter a command or type ?
a
Please enter an integer to add.
12
12 successfully added.
Please enter a command or type ?
a
Please enter an integer to add.
13
13 successfully added.
Please enter a command or type ?
a
Please enter an integer to add.
14
14 successfully added.
Please enter a command or type ?
c
12,13,14,
Please enter a command or type ?
d
12 13 14
Please enter a command or type ?
e
Enter rotation count:
2
The rotated array is:14,12,13,
Please enter a command or type ?
b
Please enter an integer to remove.
12
12 successfully removed.
Please enter a command or type ?
c
14,13,
Please enter a command or type ?
![each) b) for (int i- 5: i 0 i--) ali ali 1 c) for (int i - 0: i _ 0; i--2) { a[i] . a[i + 1); } main method in this class). A class Collection has an array of each) b) for (int i- 5: i 0 i--) ali ali 1 c) for (int i - 0: i _ 0; i--2) { a[i] . a[i + 1); } main method in this class). A class Collection has an array of](/WebImages/17/each-b-for-int-i-5-i-0-i-ali-ali-1-c-for-int-i-0-i-0-i2-a-1031396-1761534576-0.webp)
![each) b) for (int i- 5: i 0 i--) ali ali 1 c) for (int i - 0: i _ 0; i--2) { a[i] . a[i + 1); } main method in this class). A class Collection has an array of each) b) for (int i- 5: i 0 i--) ali ali 1 c) for (int i - 0: i _ 0; i--2) { a[i] . a[i + 1); } main method in this class). A class Collection has an array of](/WebImages/17/each-b-for-int-i-5-i-0-i-ali-ali-1-c-for-int-i-0-i-0-i2-a-1031396-1761534576-1.webp)
![each) b) for (int i- 5: i 0 i--) ali ali 1 c) for (int i - 0: i _ 0; i--2) { a[i] . a[i + 1); } main method in this class). A class Collection has an array of each) b) for (int i- 5: i 0 i--) ali ali 1 c) for (int i - 0: i _ 0; i--2) { a[i] . a[i + 1); } main method in this class). A class Collection has an array of](/WebImages/17/each-b-for-int-i-5-i-0-i-ali-ali-1-c-for-int-i-0-i-0-i2-a-1031396-1761534576-2.webp)
![each) b) for (int i- 5: i 0 i--) ali ali 1 c) for (int i - 0: i _ 0; i--2) { a[i] . a[i + 1); } main method in this class). A class Collection has an array of each) b) for (int i- 5: i 0 i--) ali ali 1 c) for (int i - 0: i _ 0; i--2) { a[i] . a[i + 1); } main method in this class). A class Collection has an array of](/WebImages/17/each-b-for-int-i-5-i-0-i-ali-ali-1-c-for-int-i-0-i-0-i2-a-1031396-1761534576-3.webp)
![each) b) for (int i- 5: i 0 i--) ali ali 1 c) for (int i - 0: i _ 0; i--2) { a[i] . a[i + 1); } main method in this class). A class Collection has an array of each) b) for (int i- 5: i 0 i--) ali ali 1 c) for (int i - 0: i _ 0; i--2) { a[i] . a[i + 1); } main method in this class). A class Collection has an array of](/WebImages/17/each-b-for-int-i-5-i-0-i-ali-ali-1-c-for-int-i-0-i-0-i2-a-1031396-1761534576-4.webp)