SHARE
TWEET

Untitled

a guest Oct 21st, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************************************************************************************************************
  2.   * Lab 1
  3.   * Author: Jocelyn Smith
  4.   * CIS 129 - Programming and Problem Solving I
  5.   ******************************************************************************************************************/
  6. import java.util.*;
  7. public class CIS129_JocelynSmith_Lab1 {
  8.  
  9.   static Scanner keyboard = new Scanner(System.in);
  10.      
  11.   public static void main(String[] args) {
  12.  
  13.   //Declare variables and constants
  14.     final int HIGH = 1000;
  15.     final int LOW = 0;
  16.     int offByAmount;
  17.  
  18.    //Display welcome message to user that explains what the program does
  19.      displayWelcomeMessage();
  20.    
  21.    //Call functions to prompt and receive information from user
  22.      String beekeeperName = getString ("What is your name O'Mighty Bee-Keeper?");
  23.      int beeAmountGuess = getInteger (beekeeperName + " , how many Bees do you think are in the hive?");
  24.    
  25.    //Call function to receive random number of bees within parameters
  26.      int numOfBees = getRandomNumber(LOW, HIGH);
  27.      
  28.    //Calculate difference between the guess and the actual amount of bees
  29.       offByAmount = numOfBees - beeAmountGuess;
  30.        
  31.    //Call functions to display results and goodbye message to user
  32.      System.out.println ("There are " + numOfBees + " Bees in the hive, " + beekeeperName + " .");
  33.      System.out.println ("You were off by " + (Math.abs(offByAmount)) + " .");
  34.      displayGoodbyeMessage();
  35.    
  36.       }//end Main
  37.    
  38.    
  39.    //Create module to display Welcome message to user
  40.    private static void displayWelcomeMessage() {
  41.       System.out.println("**********************************************");
  42.       System.out.println("            Guess the number of Bees!");
  43.       System.out.println("");
  44.       System.out.println("In this program, you will try to guess the number of Bees in a hive.");
  45.       System.out.println("The program will then tell you how many Bees you were off by.");
  46.       System.out.println("**********************************************");
  47.       System.out.println("");
  48.    
  49.    }//end module
  50.    
  51.    //Create function to receive name from user
  52.    private static String getString(String msg) {
  53.       System.out.println(msg);
  54.       String newValue = keyboard.nextLine(); //gets a the entire line as a String
  55.  
  56.       // Replaces all spaces with null and checks if entered value is null
  57.       if (newValue.replaceAll(" ", "").equals("")) {
  58.          System.err.println("Error: No input. Ending Program.");
  59.          System.exit(-1);
  60.       }
  61.       return newValue;
  62.    }//end function
  63.    
  64.    //Create function to receive the user's guess of amount of bees
  65.    private static int getInteger(String msg) {
  66.       System.out.println(msg);
  67.       if (!keyboard.hasNextInt()) {  // gets input from the user and asks if it is an integer
  68.          System.err.println("Error: Invalid number. Ending Program");
  69.          System.exit(-1);
  70.       }
  71.       int newValue = keyboard.nextInt();  // gets an integer
  72.       return newValue;
  73.    }//end function
  74.    
  75.    //Create function to generate random number of bees between 0 and 1000
  76.    public static int getRandomNumber (int low, int high) {
  77.       return (int)(Math.random() * ((high + 1) - low)) + low;
  78.    }//end function
  79.    
  80.    //
  81.    
  82.    //Create module to display goodbye message to user
  83.    private static void displayGoodbyeMessage() {
  84.       System.out.println("");
  85.       System.out.println("Thank you for gracing us with your beekeeping presence!");
  86.       System.out.println("Until we meet again!");
  87.    }//end module
  88.    
  89.    private static void closeScanner() {
  90.       if(keyboard != null) {
  91.          keyboard.close() ;  
  92.       }
  93.    }}//end class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top