Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. /*Part 1: Written Exercises (7 pts.)
  2.  * 1
  3.  * A. Valid
  4.  * B. Invalid
  5.  * C. Invalid
  6.  * D. Valid
  7.  * 2
  8.  * A. 0
  9.  * B. 7
  10.  * C. 0
  11.  * D. 8
  12.  * E. 0
  13. */
  14. import java.util.Scanner;
  15. import java.text.*;
  16.  public class Assignment7
  17.  {
  18.   public static void main(String[] args)
  19.   {   //declare variables
  20.       int number, size;
  21.       String choice;
  22.       char command;
  23.  
  24.       Scanner keyboard = new Scanner(System.in);
  25.       // ask a user for a array size
  26.       System.out.println("Please enter a size for the array.\n");
  27.       size = keyboard.nextInt();
  28.  
  29.       // instantiate an IntCollection object
  30.       Collection collection = new Collection(size);
  31.  
  32.       // print the menu
  33.       printMenu();
  34.  
  35.       do
  36.        {
  37.            // ask a user to choose a command
  38.            System.out.println("\nPlease enter a command or type ?");
  39.            choice = keyboard.next().toLowerCase();
  40.            command = choice.charAt(0);
  41.  
  42.            switch (command)
  43.             {
  44.                  case 'a': // add a number
  45.                       System.out.println("\nPlease enter an integer to add.");
  46.                       number = keyboard.nextInt();
  47.                       if(collection.add(number))
  48.                         System.out.println("\n" + number + " successfully added.");
  49.                       else
  50.                         System.out.println("\n"  + number + " is already in the array. " + number + " was not added.");
  51.                       break;
  52.                  case 'b': //remove from collection
  53.                       System.out.println("Please enter an integer to remove.");
  54.                       number = keyboard.nextInt();
  55.                       if(collection.remove(number))
  56.                             System.out.println("\n" + number + " successfully removed.");
  57.                       else
  58.                             System.out.println("\n"  + number + " is not in the array. " + number + " was not removed.");
  59.                       break;
  60.                  case 'c': //display
  61.                       System.out.println(collection.toString());
  62.                       break;
  63.                  case 'd': //find largest
  64.                        System.out.println("The largest number is: " + collection.findLargest());
  65.                        break;
  66.  
  67.                  case 'e': // count of positive
  68.                       System.out.println("\nThe count of positive numbers is: " + collection.countPositive());
  69.                       break;
  70.                   case 'f': // count of even numbers
  71.                       // Round the output to three decimal places
  72.                       DecimalFormat fmt = new DecimalFormat ("0.###");
  73.                       System.out.println("\nThe average is: " + fmt.format(collection.computeAvg()));
  74.                       break;
  75.  
  76.                  case '?':
  77.                       printMenu();
  78.                       break;
  79.                  case 'q':
  80.                       break;
  81.                  default:
  82.                       System.out.println("Invalid input!");
  83.             }
  84.  
  85.         } while (command != 'q');
  86.  
  87.     }  //end of the main method
  88.  
  89.  
  90.   // this method prints out the menu to a user
  91.   public static void printMenu()
  92.    {
  93.     System.out.print("\nCommand Options\n"
  94.                    + "-----------------------------------\n"
  95.                    + "a: add an integer in the array\n"
  96.                    + "b: remove an integer from the array\n"
  97.                    + "c: display the array\n"
  98.                    + "d: find largest\n"
  99.                    + "e: count positive numbers\n"
  100.                    + "f: compute average\n"
  101.                    + "?: display the menu again\n"
  102.                    + "q: quit this program\n\n");
  103.  
  104.     } // end of the printMenu method
  105.  } // end of the Assignment7 class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement