import java.util.Scanner; public class Assign { public static void main(String[]
ID: 3632155 • Letter: I
Question
import java.util.Scanner;
public class Assign
{
public static void main(String[] args)
{
int number, size;
char command;
Scanner scan = new Scanner(System.in);
// ask a user for a array size
System.out.println("Please enter a size for the array. ");
size = scan.nextInt();
// instantiate a NumberCollection object
NumberCollection collection = new NumberCollection(size);
// print the menu
printMenu();
do
{
// ask a user to choose a command
System.out.println(" Please enter a command or type ?");
String input = scan.next();
command = input.charAt(0);
switch (command)
{
case 'a': // add a number
System.out.println(" Please enter an integer to add.");
number = scan.nextInt();
boolean added;
/*** ADD YOUR CODE HERE ******************************************************/
if (added == true)
System.out.println(number + " was added");
else
System.out.println(number + " was not added");
break;
case 'b': // remove a number
System.out.println(" Please enter an integer to remove.");
number = scan.nextInt();
boolean removed;
/*** ADD YOUR CODE HERE ******************************************************/
if (removed == true)
System.out.println(number + " was removed");
else
System.out.println(number + " was not removed");
break;
case 'c': // display the array
/*** ADD YOUR CODE HERE ******************************************************/
break;
case 'd': // compute and display the maximum
/*** ADD YOUR CODE HERE ******************************************************/
break;
case 'e': // compute and display the minimum
/*** ADD YOUR CODE HERE ******************************************************/
break;
case 'f': // compute and display the sum
/*** ADD YOUR CODE HERE ******************************************************/
break;
case '?':
printMenu();
break;
case 'q':
break;
}
} 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: compute and display the maximum "
+ "e: compute and display the minimum "
+ "f: compute and display the sum "
+ "?: display the menu again "
+ "q: quit this program ");
} // end of the printMenu method
} // end of the Assignment6 class
Number Collection ..... Java program 2
import java.text.NumberFormat;
import java.util.Scanner;
public class NumberCollection
{
private int count;
private int [] numberarray;
public NumberCollection ( int arraySize)
{
count = 0;
numberarray= new int[arraySize];
}
private int indexOf(int searchingNum)
{
for (int i = 0; i < numberarray.length; i++)
if (numberarray[i] == searchingNum)
return i; //return the index number of the desired searching number, if one is available
return -1;
}
public boolean addNumber(int numberToAdd)
{
if (this.indexOf(numberToAdd) == -1)
{
if (count < (numberarray.length - 1))
{
numberarray [count] = numberToAdd;//add the parameter number at the smallest available index
count++;
return true;
}
else if (count == (numberarray.length - 1))
{
//double arrayCapacity();
numberarray [count] = numberToAdd;
count++;
return true;
}
}
return false;//return false by default.
}
public boolean removeNumber(int numberToRemove)
{The method checks if the integer specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and if it does, it moves the number stored in the last index (count-1) to where numberToRemove was found, and changes the content at the last index (count-1) to 0, decrements count, and return true. Otherwise, it returns false.}
private void doubleArrayCapacity()
{It is a helper method and doubles the capacity of the numberArray. To do this, you will need to create another array with the doubled size first, then copy the content of numberArray to this temporary array. Then the address of the temporary array can be copied to numberArray.}
public int findMax()
{It finds the maximum number among the numbers stored so far (at the time when this method is called), and returns it. If the array is empty, return 0.}
public int findMin()
{It finds the minimum number among the numbers stored so far (at the time when this method is called), and returns it. If the array is empty, return 0.}
public int computeSum()
{It computes and returns the sum of numbers stored in the numberArray so far (at the time when this method is called.) If the array is empty, return 0.}
public String toString( )
{Returns a String containing a list of numbers stored in the numberArray. An example of such string can be:
(3, 6, -1, 3, 23, -50, 43)
The string should start with a '(' and end with a ')'.}
Explanation / Answer
Hey friend...i know the answer. I have solved it in my notebook , but could not upload it . I don't know what is happening with upload... Give me lifesaver and write you email id .I'll send it to your email id .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.