Part 1 Written Exercises 5 points Consider the following arr
Solution
Part 1:
a) 3 3 3 3 3 3
b) 3 3 5 6 8 10
c) 5 6 8 10 12 12
d) 5 5 8 8 12 12
e) 3 10 8 8 10 3
Part 2:
import java.util.*;
//Class collection defination
public class Collection
{
//Class Instance variable
int numArray[];
int size;
//Parameterized Constructor to create an array of specified size
public Collection(int arraySize)
{
int c;
size = arraySize;
numArray = new int[arraySize];
}
//Searches for an numebr and returns the index position of searched number otherwise returns -1
private int search(int searchinNum)
{
for(int c = 0; c < size; c++)
if(searchinNum == numArray[c])
return c;
return -1;
}
//Displays the array as per the specified format
public String toString()
{
String temp = \"{ \";
for(int c = 0; c < getCount(); c++)
{
if(c == getCount() - 1)
temp = temp + numArray[c] + \" } \";
else
temp = temp + numArray[c] + \", \";
}
return temp;
}
//Displays menu
public static void printMenu()
{
System.out.println(\"\ 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 \ \ \");
}
//Doubles the capacity of the array if it is insufficient to add number
private void doubleCapacity()
{
int temp [] = new int[size];
int newSize = size;
for(int c = 0; c < size; c++)
temp[c] = numArray[c];
size = size * 2;
numArray = new int[size];
for(int c = 0; c < newSize; c++)
numArray[c] = temp[c];
}
//Returns the number of numbers are availabe in the array
public int getCount()
{
int c = 0, count = 0;
while(numArray[c] != 0)
{
count++;
c++;
}
return count;
}
//Adds a number to the array if it is not available otherwise returns false
//If size is not sufficient to add number then it doubles the size of the array
public boolean add(int numberToAdd)
{
//Calculates length
int len = getCount();
//Checks for the number availability
if(search(numberToAdd) == -1)
{
//Checks for the size, adds the number and returns true
if(len < size - 1)
{
numArray[len] = numberToAdd;
}
//If size is insufficient doubles the capacity of the array and adds the number and returns true
else
{
doubleCapacity();
numArray[len] = numberToAdd;
}
return true;
}
else
return false;
}
//Removes a number from the array if it is available otherwise returns false
public boolean remove(int numberToRemove)
{
//Checks for the number availability
if(search(numberToRemove) != -1)
{
//Repositions the numbers after delete
for(int c = search(numberToRemove); c < size - 1; c++)
{
numArray[c] = numArray[c+1];
}
return true;
}
else
return false;
}
//Rotates the array from the specified position and returns an array
public int [] rotate(int n)
{
int tp [] = new int[size];
int c, d = 0;
//Stores the number from specified position till end
for(c = n; c < getCount(); c++, d++)
tp[d] = numArray[c];
//Stores rest numbers
for(c = 0; c < n; c++, d++)
tp[d] = numArray[c];
return tp;
}
//Finds the smallest number and swaps it with the zero index position and returns the new array
public int [] swapSmallest()
{
//Assumes zero index position is the smalles one
int small = numArray[0];
int pos = 0;
//Creates a new array
int te [] = new int[size];
te = numArray;
//Loops till the number found in the array
for(int c = 1; c < getCount(); c++)
{
//Checks for the smalles number
if(te[c] < small)
{
small = te[c];
pos = c;
}
}
te[pos] = te[0];
te[0] = small;
return te;
}
public static void main(String ss[])
{
int sz, number;
String choice;
char command;
Scanner keyboard = new Scanner(System.in);
System.out.println(\"\ Please Enter the size of Array: \");
sz = keyboard.nextInt();
Collection collection = new Collection(sz);
printMenu();
do
{
System.out.println(\"\ Please enter a command or type ? \");
choice = keyboard.next().toLowerCase();
command = choice.charAt(0);
switch(command)
{
case \'a\':
System.out.println(\"\ Please enter an integer to add \");
number = keyboard.nextInt();
if(collection.add(number))
System.out.println(\"\ \" + number + \" Successfully added \");
else
System.out.println(\"\ \" + number + \" is already in the array \");
break;
case \'b\':
System.out.println(\"\ Please enter a Number to Remove \");
number = keyboard.nextInt();
if(collection.remove(number))
System.out.println(\"\ \" + number + \" Successfully removed \");
else
System.out.println(\"\ \" + number + \" is not available in the array \");
break;
case \'c\':
System.out.println(collection.toString());
break;
case \'d\':
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 the rotation count: \");
number = keyboard.nextInt();
int [] temp1 = collection.rotate(number);
System.out.println(\"\ The rotated array is: \ \");
for(int i = 0; i < collection.getCount(); i++)
System.out.print(temp1[i] + \" \");
System.out.println();
break;
case \'?\':
printMenu();
break;
case \'q\':
break;
default:
System.out.println(\"Invalid Input\");
}
}while(command != \'q\');
}
}
Output:
Please Enter the size of Array:
2
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
10
10 Successfully added
Please enter a command or type ?
a
Please enter an integer to add
20
20 Successfully added
Please enter a command or type ?
a
Please enter an integer to add
30
30 Successfully added
Please enter a command or type ?
c
{ 10, 20, 30 }
Please enter a command or type ?
b
Please enter a Number to Remove
20
20 Successfully removed
Please enter a command or type ?
c
{ 10, 30 }
Please enter a command or type ?
?
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
11
11 Successfully added
Please enter a command or type ?
a
Please enter an integer to add
1
1 Successfully added
Please enter a command or type ?
c
{ 10, 30, 11, 1 }
Please enter a command or type ?
d
1 30 11 10
Please enter a command or type ?
a
Please enter an integer to add
501
501 Successfully added
Please enter a command or type ?
c
{ 1, 30, 11, 10, 501 }
Please enter a command or type ?
e
Enter the rotation count:
3
The rotated array is:
10 501 1 30 11
Please enter a command or type ?
?
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 ?
b
Please enter a Number to Remove
100
100 is not available in the array
Please enter a command or type ?
q






