Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************************************************
- * Lab 1
- * Author: Jocelyn Smith
- * CIS 129 - Programming and Problem Solving I
- ******************************************************************************************************************/
- import java.util.*;
- public class CIS129_JocelynSmith_Lab1 {
- static Scanner keyboard = new Scanner(System.in);
- public static void main(String[] args) {
- //Declare variables and constants
- final int HIGH = 1000;
- final int LOW = 0;
- int offByAmount;
- //Display welcome message to user that explains what the program does
- displayWelcomeMessage();
- //Call functions to prompt and receive information from user
- String beekeeperName = getString ("What is your name O'Mighty Bee-Keeper?");
- int beeAmountGuess = getInteger (beekeeperName + " , how many Bees do you think are in the hive?");
- //Call function to receive random number of bees within parameters
- int numOfBees = getRandomNumber(LOW, HIGH);
- //Calculate difference between the guess and the actual amount of bees
- offByAmount = numOfBees - beeAmountGuess;
- //Call functions to display results and goodbye message to user
- System.out.println ("There are " + numOfBees + " Bees in the hive, " + beekeeperName + " .");
- System.out.println ("You were off by " + (Math.abs(offByAmount)) + " .");
- displayGoodbyeMessage();
- }//end Main
- //Create module to display Welcome message to user
- private static void displayWelcomeMessage() {
- System.out.println("**********************************************");
- System.out.println(" Guess the number of Bees!");
- System.out.println("");
- System.out.println("In this program, you will try to guess the number of Bees in a hive.");
- System.out.println("The program will then tell you how many Bees you were off by.");
- System.out.println("**********************************************");
- System.out.println("");
- }//end module
- //Create function to receive name from user
- private static String getString(String msg) {
- System.out.println(msg);
- String newValue = keyboard.nextLine(); //gets a the entire line as a String
- // Replaces all spaces with null and checks if entered value is null
- if (newValue.replaceAll(" ", "").equals("")) {
- System.err.println("Error: No input. Ending Program.");
- System.exit(-1);
- }
- return newValue;
- }//end function
- //Create function to receive the user's guess of amount of bees
- private static int getInteger(String msg) {
- System.out.println(msg);
- if (!keyboard.hasNextInt()) { // gets input from the user and asks if it is an integer
- System.err.println("Error: Invalid number. Ending Program");
- System.exit(-1);
- }
- int newValue = keyboard.nextInt(); // gets an integer
- return newValue;
- }//end function
- //Create function to generate random number of bees between 0 and 1000
- public static int getRandomNumber (int low, int high) {
- return (int)(Math.random() * ((high + 1) - low)) + low;
- }//end function
- //
- //Create module to display goodbye message to user
- private static void displayGoodbyeMessage() {
- System.out.println("");
- System.out.println("Thank you for gracing us with your beekeeping presence!");
- System.out.println("Until we meet again!");
- }//end module
- private static void closeScanner() {
- if(keyboard != null) {
- keyboard.close() ;
- }
- }}//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement