Part 1: Written Exercises: (5 points) Consider the following array: int 3, 5 8,
ID: 3773972 • Letter: P
Question
Part 1: Written Exercises: (5 points) Consider the following array: int 3, 5 8, 10 121; write the contents of the array a after the following loops. Use the original data above for each question. Point each) a) for (int i 1; i 61 i++) ali) 11; b) for (int i 5; i 0; i--) a [i] ali c) for (int i 0; i 5; i++) a [i] ali 1); d) for (int i 4: i 0, i- 2) ali] ali 1] e) for (int i 1: i K 6; it ali ing Your assignment is to create a class called Collection in a file called Collection.java. (there is no main method in this class). A class Collection has an array of integers and a count (integer) as instance variables. The variable count keeps track of how many integers are store in the array. The variable name for the array of integers is numArray. Note: You need to distinguish the array size (capacity) and "count" that keeps track of numbers added to this array so far. constructor and methods. (If your class does not The class Collection must include the following contain any of the following methods, points will be deducted) Method Description of the Method constructs an empty collection object with an array capacity specified by the integer ublic colleetion int array size) is located. If the number is returns the index of the number specified by the rivate int search (int returns -1. It is a service (he fout searehi the array (This can be method checks if the integer specified by the paramet ter exists or not) and also checks if the array has one using the search method to see if it retums ot reached its capacity. If both are satisfied, the number is added to the array at the lic boolean add (int mallest available index. If the array reached its capacity, double its size by calling the umber To Add thod doubleArraycapacity0 and add the number. the number is added successfully, hen the method retums true, Ifthe number already exists in the array, the new number wil be added, and the method returns false. method checks if the integer specified by the parameter exists in the array This can be or not) and if it does, it deletes the lic boolean remove (int one using the search method to see if it returms returns false. umber Remove the left, and it returns true. Cotherwise umber by shifting numbersExplanation / Answer
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