Question
Trying to rewrite this code using methods, first is the old code, then the revised version. Wasn't sure what to do next. // //CookieCase //
Trying to rewrite this code using methods, first is the old code, then the revised version. Wasn't sure what to do next.
//
//CookieCase
//
//The purpose of this is to create and practice different loops
//
//Author:Andrew Babilonia
//Date:9/12/2018
//
package cookieCase;
import java.util.Scanner;
public class CookieCase
{
public static void main(String[] args)
{
// declare variables
Scanner input = new Scanner(System.in); // connection to keyboard for input
int numBoxes; // number of boxes of cookies
int numCases; // number of cases of cookies
String cleanUpStr; // clean up the keyboard buffer
final int BOXESPERCASE = 10; // number of boxes of cookies per case
//initialize variables
numBoxes = 0;
numCases = 0;
cleanUpStr = "none yet";
// continue to prompt user to enter a number of boxes until it is valid
do
{
// prompt user for number of boxes of cookies
System.out.print("Enter the number of boxes of cookies\t");
// read in the number of boxes
numBoxes = input.nextInt( );
cleanUpStr = input.nextLine( );
// check to see if number of boxes is invalid
if (numBoxes < 0)
{
// invalid number of boxes
System.out.println(numBoxes + " is invalid");
}
}while(numBoxes < 0);
// calculate the number of full cases required
numCases = numBoxes / BOXESPERCASE;
// check to see if there needs to be a partial case
if (numBoxes % BOXESPERCASE > 0)
{
++numCases;
}
// print out the results
System.out.println("Number of boxes\t" + numBoxes);
System.out.println("Number of cases\t" + numCases);
// close input stream
input.close( );
input.close(); //close Scanner
}//end main
}//end class
REVISED CODE
//CookieCaseConversion
//the purpose of this class is to determine the number of cases required to hold
//a number of boxes of cookies. This solution must be completed using selection
//statements and loops.
//author: Andrew Babilonia
//Date: 9/24/2018
package cookieCase;
import java.util.Scanner;
public class CookieCaseMethod
{
public static void main(String[] args)
{
// declare variables
String name;
int cookieBoxes;
// initialize variables
name = "none yet";
cookieBoxes = 0;
name = inputString("Enter your name\t");
System.out.println(name);
//calculate cookie cases from boxes
do
{
cookieBoxes = inputInteger("Enter in the number of boxes of cookies");
}
}
//
// inputString
//
// the purpose of this method is to input a string from the keyboard
//
// Input: prompt the prompt message for the dialog box
//
// Return: inputStr the string that was entered at the keyboard
//
public static String inputString(String prompt)
{
Scanner input = new Scanner(System.in);
String inputStr;
//prompt user to enter in string
System.out.print(prompt);
//read in value as string from keyboard
inputStr = input.nextLine();
//return input string
return(inputStr);
}//end input string
//
// inputInteger
//
// the purpose of this method is to input an integer value form the keyboard
//
// Input: prompt the prompt message for the user
//
// Return: num the integer entered at the keyboard
//
public static int inputInteger(String prompt)
{
Scanner input = new Scanner(System.in);
String cleanUpStr;
int num;
num = 0;
cleanUpStr = "no string yet";
System.out.println(prompt);
num = input.nextInt();
cleanUpStr = input.nextLine();
// validate integer
while(num < 0)
{
System.out.println(num +" is invalid, please enter a valid number");
num = input.nextInt();
cleanUpStr = input.nextLine();
}// end while loop
return(num);
}//end inputInteger
}
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