Advertisement
Guest User

Untitled

a guest
May 27th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. /**
  2.  * This is the basis for the third task in Lab 12,
  3.  * Applications. The task is to count the different
  4.  * kinds of numbers. The numbers between zero and
  5.  * nine, inclusive, are divided into the following
  6.  * categories:
  7.  * <li>Identity - the additive identity element, zero;
  8.  * and the multiplicative identity element, one.
  9.  * <li>Prime - a number that can only be evenly divided
  10.  * by itself and one.
  11.  * <li>Composite - a number that is the product of primes,
  12.  * two or more, not necessarily different prime numbers.
  13.  * </ul>
  14.  * The numbers outside of the range 0-9 will simply
  15.  * be counted as other.test.
  16.  * <p>
  17.  * You might recognize this as an adaptation of the example
  18.  * showing how to write a switch statement. This now has
  19.  * been updated to include user input, using Scanner.
  20.  */
  21. public class CountNumbers {
  22.  
  23.   /**
  24.   * The application method
  25.   */
  26.   public static void main(String[] args) {
  27.   CountNumbers numbers = new CountNumbers();
  28.   System.out.println("Please enter an integer:");  
  29.  
  30.    // Checking for an integer  
  31.     if (numbers.in.hasNextInt()) {
  32.       // Read the input  
  33.       int input = numbers.in.nextInt();
  34.       numbers.switchCounter(input);
  35.     }
  36.     else  {
  37.        // Clear scanner if it was not an integer and give an error
  38.        numbers.in.next();
  39.        System.err.println("Non-integer input ignored.");
  40.     }
  41.     numbers.printSummary();
  42.   }
  43.  
  44.   // The fields to keep track of the counts
  45.   private int identity;
  46.   private int prime;
  47.   private int composite;
  48.   private int other;
  49.  
  50.   // The scanner
  51.   private java.util.Scanner in;
  52.  
  53.   /**
  54.    * The constructor initializes the fields.
  55.    */
  56.   public CountNumbers() {
  57.     identity = 0;
  58.     prime = 0;
  59.     composite = 0;
  60.     other = 0;
  61.     in = new java.util.Scanner(System.in);
  62.   }
  63.  
  64.   /**
  65.    * This is the switch version of the counting routine.
  66.    * @param input The value read from the user input
  67.    */
  68.   public void switchCounter(int input) {
  69.     // the switch statement, based on the int value input
  70.     switch(input) {
  71.       // count the identity elements: 0 and 1
  72.       case 1:
  73.       case 0:
  74.         identity ++;
  75.         break;
  76.       // count the prime numbers  
  77.       case 2:
  78.       case 3:
  79.       case 5:
  80.       case 7:
  81.         prime ++;
  82.         break;
  83.       // count the composite numbers  
  84.       case 4:
  85.       case 6:
  86.       case 8:
  87.       case 9:
  88.         composite ++;
  89.         break;
  90.     }
  91.   }
  92.  
  93.   /**
  94.    * Print out the summary statistics, the counts.
  95.    */
  96.   public void printSummary() {
  97.     System.out.println();
  98.     System.out.println("Here are the counts:");
  99.     System.out.println("   " + identity + " identity elements");
  100.     System.out.println("   " + prime + " prime numbers");
  101.     System.out.println("   " + composite + " composite numbers");
  102.     System.out.println("   " + other + " values out of range");
  103.   }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement