n Java Part 1 Written Exercises 5 points Consider the follo
n Java :-
Part 1: Written Exercises: (5 points)
Consider the following array:
int[] a = { 3, 5, , 8, 10 , 12};
Write the contents of the array a after the following loops. Use the original data above for each question. (1 Point each)
a) for (int i = 1; i < 6; i++) { a[i] = a[i - 1]; }
b) for (int i = 5; i > 0; i--) { a[i] = a[i - 1]; }
c) for (int i = 0; i < 5; i++) { a[i] = a[i + 1]; } d) for (int i = 4; i >= 0; i-=2) { a[i] = a[i + 1]; } e) for (int i = 1; i < 6; i++) { a[i] = a[5 - i]; }
Part 2: Programming: (15 points)
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.
The class Collection must include the following constructor and methods. (If your class does not contain any of the following methods, points will be deducted.)
Method
Description of the Method
It constructs an empty Collection object with an array capacity specified by the integer parameter \"arraySize\".
It returns the index of the number specified by the parameter is located. If the number is not found, it returns -1. It is a service (helper) method.
The method checks if the integer specified by the parameter exists in the array (This can be done using the search method to see if it returns -1 or not) and also checks if the array has not reached its capacity. If both are satisfied, the number is added to the array at the smallest available index. If the array reached its capacity, double its size by calling the method doubleArrayCapacity() and add the number. If the number is added successfully, then the method returns true. If the number already exists in the array, the new number will not be added, and the method returns false.
The method checks if the integer specified by the parameter exists in the array (This can be done using the search method to see if it returns -1 or not) and if it does, it deletes the number by shifting numbers to the left and it returns true. Otherwise, it returns false.
Returns the number of elements added so far to the array numArray.
The method takes an integer rotation amount, n. The method creates a new array with the items of numArray moved forward by n positions. Elements that are rotated off the array will appear at the end. For example, suppose numArray contains the following items in sequence:
1 2 3 4 5 67
After rotating by 3, the elements in the new array will appear in this sequence: 4 5 6 7 1 23
It is a service (helper) method and doubles the capacity of the numArray. Please see the example in page 338, the method increaseSize() as a reference.
The method returns an array by swapping the smallest integer element of an array referenced by numArray into index 0. For example if numArray is
{ 5, 7, 3, 9, 2, 11, 1, 6 }
After calling the method, it returns a new array with smallest element stored at index 0
{ 1, 7, 3, 9, 2, 11, 5, 6 }
Returns a String containing a list of numbers stored in the numArray. An example of such string can be:
{3, 6, -1, 3, 23, -50, 43}
The string should start with a \'{\' and end with a \'}\'.
Save the Collection class in a file called Collection.java and use the following program stored in Assignment7.java, which has the main method to create new Collection objects and to test your class. You need to modify Assignment7.java by adding your code(s) for the cases \'a\', \'b\', \'c\', \'d\', and e\'.
The program will ask a user to enter a size for the array as an integer. Then it will show the following menu to a user:
Command Options -----------------------------------
a: add an integer in the array
b: remove an integer from the array c: display the array
d: swap with the smallest
e: rotate the array
?: display the menu again
Then it will ask a user to enter one of the above commands. Based on the user\'s choice, the program needs to perform corresponding operation. This will be done by using a method you define in the Collection class. The program will terminate when a user enters \'q\'.
Note that you will NOT be removing or modifying the existing codes in the Assignment7.java. All you need to do is to add more codes.
After compiling Collection.java file and Assignment7.java file, you need to execute Assignment7 class.
Sample Output: (the inputs entered by a user are shown in bold)
(Make sure that your program works at least with this scenario.)
Please enter a size for the array.
5
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.
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
{12, 20, 30}
Please enter a command or type ?
b
Please enter an integer to remove.
12
12 successfully removed.
Please enter a command or type ?
c
{20, 30}
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.
40
40 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
{20, 30, 10, 40, 1}
Please enter a command or type ?
d
1 30 10 40 20
Please enter a command or type ?
e
Enter rotation count:
3
The rotated array is: 40 1 20 30 10
Please enter a command or type ?
c
{20, 30, 10, 40, 1}
Please enter a command or type ?
e
Enter rotation count:
2
The rotated array is: 10 40 1 20 30
Please enter a command or type ?
q
*********************************************************************************
Assignment file -
| Method | Description of the Method |
public Collection(int arraySize) | It constructs an empty Collection object with an array capacity specified by the integer parameter \"arraySize\". |
private int search(int searchingNum) | It returns the index of the number specified by the parameter is located. If the number is not found, it returns -1. It is a service (helper) method. |
public boolean add (int numberToAdd) | The method checks if the integer specified by the parameter exists in the array (This can be done using the search method to see if it returns -1 or not) and also checks if the array has not reached its capacity. If both are satisfied, the number is added to the array at the smallest available index. If the array reached its capacity, double its size by calling the method doubleArrayCapacity() and add the number. If the number is added successfully, then the method returns true. If the number already exists in the array, the new number will not be added, and the method returns false. |
public boolean remove(int numberToRemove) | The method checks if the integer specified by the parameter exists in the array (This can be done using the search method to see if it returns -1 or not) and if it does, it deletes the number by shifting numbers to the left and it returns true. Otherwise, it returns false. |
public int getCount() | Returns the number of elements added so far to the array numArray. |
public int[] rotate (int n) | The method takes an integer rotation amount, n. The method creates a new array with the items of numArray moved forward by n positions. Elements that are rotated off the array will appear at the end. For example, suppose numArray contains the following items in sequence: 1 2 3 4 5 67 |
Solution
Part 1:
Given array
int[] a = { 3, 4,5, 8, 10 , 12};
a)
//This code will replace every element in the array,a
//with the first element 3 since first element is copied
//till end of the array,a
for (int i = 1; i < 6; i++)
{
a[i] = a[i - 1];
}
Output: 3 3 3 3 3 3
b)
//This below code copies the elemenet at index
/4 to 5, 3 to 4 , 2 to 3 , 2 to 1 and first element remains same
for (int i = 5; i > 0; i--)
{
a[i] = a[i - 1];
}
Output: 3 3 4 5 8 10
c)
//The below code copies the elements at indices
//2 to 1, 3 to 2 , 4 to 3, 5 to 4
//and the last element remains same
for (int i = 0; i < 5; i++)
{
a[i] = a[i + 1];
}
Output: 4 5 8 10 12 12
d)
//The below code that copies values
// at indices 1 to 0, 3 to 2 and 5 to 4
for (int i = 4; i >= 0; i-=2)
{
a[i] = a[i + 1];
}
Output: 4 4 8 8 12 12
e)
//The below code copies the following indices
//4 to 1
//3 to 2
//2 to 3
//1 to 4
for (int i = 1; i < 6; i++)
{
a[i] = a[5 - i];
}
Output:3
10 8 8 10 3
----------------------------------------
//Part-2
//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.
5
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.
1
1 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 ?
a
Please enter an integer to add.
10
10 successfully added.
Please enter a command or type ?
a
Please enter an integer to add.
40
40 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 ?
c
1,30,10,40,20,
Please enter a command or type ?
e
Enter rotation count:
3
The rotated array is:40,20,1,30,10,
Please enter a command or type ?
c
![n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo](/WebImages/14/n-java-part-1-written-exercises-5-points-consider-the-follo-1018333-1761526404-0.webp)
![n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo](/WebImages/14/n-java-part-1-written-exercises-5-points-consider-the-follo-1018333-1761526404-1.webp)
![n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo](/WebImages/14/n-java-part-1-written-exercises-5-points-consider-the-follo-1018333-1761526404-2.webp)
![n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo](/WebImages/14/n-java-part-1-written-exercises-5-points-consider-the-follo-1018333-1761526404-3.webp)
![n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo](/WebImages/14/n-java-part-1-written-exercises-5-points-consider-the-follo-1018333-1761526404-4.webp)
![n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo](/WebImages/14/n-java-part-1-written-exercises-5-points-consider-the-follo-1018333-1761526404-5.webp)
![n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo](/WebImages/14/n-java-part-1-written-exercises-5-points-consider-the-follo-1018333-1761526404-6.webp)
![n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo](/WebImages/14/n-java-part-1-written-exercises-5-points-consider-the-follo-1018333-1761526404-7.webp)
![n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo](/WebImages/14/n-java-part-1-written-exercises-5-points-consider-the-follo-1018333-1761526404-8.webp)
![n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo n Java :- Part 1: Written Exercises: (5 points) Consider the following array: int[] a = { 3, 5, , 8, 10 , 12}; Write the contents of the array a after the follo](/WebImages/14/n-java-part-1-written-exercises-5-points-consider-the-follo-1018333-1761526404-9.webp)