Question
Your assignment is to create class called IntList in a file called IntList.java . (there is no main method in this class). The class IntList
Your assignment is to create class called IntList in a file calledIntList.java. (there is no main method in this class). The class IntList hastwo instance variables, an array of integers and an integer variable, which keeps track of the number of integers in the array. Note: the number of elements is different from the size of the array.
The class IntListmustinclude 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
public IntList (int size)
The constructor instantiates an array of integers using the given size.
private int indexOf(int searchingNum)
This method returns the index where searchingNum is located. It returns -1 if it is not found.
public void addInt (int num)
It checks if the array has not reached its capacity, the num is added to the array at the smallest available index.
private void doubleArrayCapacity()
This is a helper method; it is private and it doubles the size of the array.
public int findMax ()
It returns the largest integer stored in the array, if no element stored in the array, it returns zero
public int findMin ()
It returns the smallest integer stored in the array, if no element stored in the array, it returns zero
public void removeFirst(int num)
Removes the first occurrence of num from the list and moves all elements one spot to the left. It will leave the array the same if num is not in the list to be removed.
public void removeAll (int num)
Removes all the occurrence of num from the array and shifts all the elements to the left by one index.
public int[] countNumbers(int num)
this method returns an array of all the integers that are greater than num
public String toString()
Displays the array of integers. It prints 10 integers per line, in this format [6, 7, 8, 9]
Assignment.Java is given :
mport 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 = keyboard.nextInt(); // instantiate an object IntList myList = new IntList(size); // print the menu printMenu(); do { // ask a user to choose a command System.out.println(" Please enter a command or type ?"); choice = keyboard.next().toLowerCase(); command = choice.charAt(0); switch (command) { case 'a': // add a number System.out.println(" Please enter an integer to add."); number = keyboard.nextInt(); myList.addInt(number); break; case 'b': // display the array System.out.print(myList); break; case 'c': // compute and display the maximum System.out.println(" The maximum is: " + myList.findMax()); break; case 'd': // compute and display the minimum System.out.println(" The minimum is: " + myList.findMin()); break; case 'e': // remove first number System.out.println(" Please enter an integer to remove."); number = keyboard.nextInt(); myList.removeFirst(number); break; case 'f': // remove all numbers System.out.println(" Please enter an integer to remove."); number = keyboard.nextInt(); myList.removeAll(number); break; case 'g': // display the numbers greater than user input System.out.println("Enter an integer: "); number = keyboard.nextInt(); int[] temp = myList.countNumbers(number); System.out.print(" List of all integers greater than " + number + " are: " ); for (int i=0; i< temp.length; i++) System.out.print(temp[i] + " "); break; case '?': printMenu(); break; case 'q': 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: display the array " + "c: compute and display the maximum " + "d: compute and display the minimum " + "e: remove an integer from the array " + "f: remove all integers from the array " + "g: display list of integers that are greater than input value " + "?: display the menu again " + "q: quit this program "); } // end of the printMenu method } // end of the Assignment7 class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started