LAB PROMPT:
Lab 07
Hello future bakers and artisans of the fine gourmet. You love to throw parties, and what goes better at a party than cake! The problem is that you only have enough flour to bake a limited number of cakes. In this lab, you will write a small program to figure out if you have enough flour to bake the number of cakes needed for the people attending.
Step 1 - using final
At the top of your class, you should declare the following variables as static and final
here an example of declaring the first one to give you an idea:
static final double CUPS_FLOUR_PER_CAKE = 2.3;
These variables are final / constants (meaning you can’t change them) and are available at the class level. This last part means every method in the class has access to them and can be used in every method!
To test this, go to your main, and put in the following code:
System.out.println(CUPS_FLOUR_PER_CAKE); System.out.println(PIECES_PER_CAKE);
Run your program, and it should simply print 2.3 and 8 to the console.
Step 2 - calcNumCakesBaked
For this step you will write the method calcNumCakesBaked. Here are some important things you need to know before writing this method
Write calcNumCakesBaked
Now go to the method section of your file, and write a public
static method
called calcNumCakesBakes that returns an int. It
takes in a double in the parameter that represents the number of
cupsOnHand. For example the method signature could
look like this:
public static int calcNumCakesBaked(double cupsOnHand)
Inside the method you need to do the following:
1) It needs to return - as a whole number - the number of possible cakes one can build based on the cups of flour the baker has on hand.
CUPS_FLOUR_PER_CAKE tells you how many cups it takes to bake a cake
As such, the formula to figure out how many cakes you can bake is:
cakes = cupsOnHand / CUPS_FLOUR_PER_CAKE
2) The problem - you have to return an int, rounded down that represents the number of cakes. Use the Math.floor method, and casting to help you with that.
Test calcNumCakesBaked
Always test each method after writing (ideally while writing). So go down to your main method, and put in the following tests:
System.out.println("#### Testing calcNumCakesBaked"); System.out.println(calcNumCakesBaked(0)); System.out.println(calcNumCakesBaked(2.3)); System.out.println(calcNumCakesBaked(3)); System.out.println(calcNumCakesBaked(42));
Now double check the output. Is it what you think it should be?
Step 3 - getCakeEnthusiasts
There is a little known fact, that no matter how large of the party, only a max of 19 people want cake. This “fact” is even stranger, the number is actually based the remainder of the total guests at a party divided by 20. So if there are 100 guests, no one wants cake, but if there are 117 guests 17 of them want cake. Ok, we made it up, to give you practice with modulo, but let’s get this party started.
Write getCakeEnthusiasts
The public static method getCakeEnthusiasts returns an int. It also needs the parameter of type int that represents the number of party goers (e.g. partySize) to work. What would that method signature look like?
Using the partySize, simply mod it (%) by 20, and return the answer.
Test getCakeEnthusiasts
Go to your main, and add the following tests:
System.out.println("#### Testing getCakeEnthusiasts ####"); System.out.println(getCakeEnthusiasts(21)); System.out.println(getCakeEnthusiasts(3)); System.out.println(getCakeEnthusiasts(20)); System.out.println(getCakeEnthusiasts(42)); System.out.println(getCakeEnthusiasts(100000001));
Did the results come out as you expected?
Step 4 - getMessage
It is very common in applications to have a method return a message based on a message ‘type’ passed to it. This allows for easy language integrations, and one place to change the messages. This method follows the same model with three messages. You will pass in an int value (the message type), and return one of three possible messages.
Write getMessage
You will write a public static method called getMessage that returns a String. The method will have a single int parameter that represents the message type. What would the signature look like?
Now you will have the method signature return a message String based on the type, as follows:
You will notice there are only three message types: less than 0, 0, or greater than 0. This is fairly common in programming.
Test getMessage
You just wrote three to four lines of code - time to test again! Put the following into your main method, and run the program. Did you get what you expected?
System.out.println("### TESTING getMessage ###"); System.out.println(getMessage(-1)); System.out.println(getMessage(0)); System.out.println(getMessage(1));
Step 5 - determineLeftoverCake
This method wants to look at the total number of cakes you can bake and compare it to the number of folks at the party who want cake. Based on the answer, you will return NOT_ENOUGH_FLOUR, EXACT_CAKE, or EXTRA_CAKE.
This method will use:
Write determineLeftoverCake
determineLeftoverCake is a public static method that returns an int value which is either NOT_ENOUGH_FLOUR, EXACT_CAKE, or EXTRA_CAKE. For the method to work, you will need both the number of people, and the number of cakes. Putting that together, our method signature could look like:
public static int determineLeftoverCake(int cakes, int people)
The first thing you will want to do is determine the number of pieces of cake. This is done by multiplying the PIECES_PER_CAKE times the number of cakes. We called the answer variable numPiecesCake.
You will then write the following conditions:
Test determineLeftoverCake
You wrote a few more lines of code, it is best to test it. Put the following in your main method
System.out.println("### Testing determineLeftoverCake ###"); System.out.println(determineLeftoverCake(10, 10)); System.out.println(determineLeftoverCake(10, 80)); System.out.println(determineLeftoverCake(0, 9)); System.out.println(determineLeftoverCake(1, 8));
Is it returning what you think it should?
Step 6 - Gluing it together!
Go ahead and uncomment the randomTests method, and the method call to randomTests in main.
PROVIDED CODE:
import java.util.Random;
public class BakerHelper {
public static final Random RND = new Random(); // do not modify,
but use as an example of a class variable
// declare your class-level variables here
// declare your methods here
public static void main(String[] args) {
// add your own tests here
// randomTests(); // uncomment when mostly done
}
/*remove-when-ready
static void randomTests() {
for(int i = 0; i < 20; i++) {
System.out.println(getMessage(determineLeftoverCake(calcNumCakesBaked(getCupsOnHand()),
getCakeEnthusiasts(getRSVP()))));
}
}
end-comment*/
// ************** don't modify below this line
************************
/**
* Gets a random number of people who are attending the party
* @returns random whole number from 0-99 of party goers
**/
public static int getRSVP() {
return RND.nextInt(100);
}
/**
* Gets a random number of cups of flour to see how much to
have
* @returns random double number between 0-9
**/
public static double getCupsOnHand() {
return RND.nextInt(10) + RND.nextDouble();
}
}
Below is the code in JAVA for the above question:
import java.util.Random;
//BakerHelper class
public class BakerHelper {
public static final Random RND = new Random(); // do not modify, but use as an example of a class variable
// declare your class-level variables here
static final double CUPS_FLOUR_PER_CAKE = 2.3;
static final double PIECES_PER_CAKE = 8;
static final double NOT_ENOUGH_FLOUR = -1;
static final double EXACT_CAKE = 0;
static final double EXTRA_CAKE = 1;
// declare your methods here
//created method calcNumCakesBaked
public static int calcNumCakesBaked(double cupsOnHand)
{
int cakes;
cakes = (int)
(Math.floor(cupsOnHand / CUPS_FLOUR_PER_CAKE));
return cakes;
}
//created method getCakeEnthusiasts
public static int getCakeEnthusiasts(int partySize)
{
int size = partySize % 20;
return size;
}
//created method getMessage
public static String getMessage(int messageType)
{
String message = null;
if (messageType < 0)
message =
"Sorry. You do not have enough flour to bake a cake.";
else if (messageType == 0)
message = "No
left over cake.";
else if (messageType > 0)
message = "Yay,
left over cake!";
return message;
}
//created method determineLeftoverCake
public static int determineLeftoverCake(int cakes, int
people) {
int numPiecesCake;
numPiecesCake = (int) (cakes *
PIECES_PER_CAKE);
if (cakes <= 0 || numPiecesCake
< people)
return (int)
NOT_ENOUGH_FLOUR;
else if (people > 0 &&
(numPiecesCake - people == 0))
return (int)
EXACT_CAKE;
else
return (int)
EXTRA_CAKE;
}
public static void main(String[] args) {
// add your own tests here
//testing each method.
System.out.println(CUPS_FLOUR_PER_CAKE);
System.out.println(PIECES_PER_CAKE);
System.out.println("#### Testing
calcNumCakesBaked");
System.out.println(calcNumCakesBaked(0));
System.out.println(calcNumCakesBaked(2.3));
System.out.println(calcNumCakesBaked(3));
System.out.println(calcNumCakesBaked(42));
System.out.println("#### Testing
getCakeEnthusiasts ####");
System.out.println(getCakeEnthusiasts(21));
System.out.println(getCakeEnthusiasts(3));
System.out.println(getCakeEnthusiasts(20));
System.out.println(getCakeEnthusiasts(42));
System.out.println(getCakeEnthusiasts(100000001));
System.out.println("### TESTING
getMessage ###");
System.out.println(getMessage(-1));
System.out.println(getMessage(0));
System.out.println(getMessage(1));
System.out.println("### Testing
determineLeftoverCake ###");
System.out.println(determineLeftoverCake(10, 10));
System.out.println(determineLeftoverCake(10, 80));
System.out.println(determineLeftoverCake(0, 9));
System.out.println(determineLeftoverCake(1, 8));
//uncommented the calling
method
randomTests(); // uncomment when
mostly done
}
static void randomTests() {
for (int i = 0; i < 20; i++)
{
System.out.println(getMessage(
determineLeftoverCake(calcNumCakesBaked(getCupsOnHand()),
getCakeEnthusiasts(getRSVP()))));
}
}
// ************** don't modify below this line
************************
/**
* Gets a random number of people who are attending the
party
*
* @returns random whole number from 0-99 of party
goers
**/
public static int getRSVP() {
return RND.nextInt(100);
}
/**
* Gets a random number of cups of flour to see how
much to have
*
* @returns random double number between 0-9
**/
public static double getCupsOnHand() {
return RND.nextInt(10) +
RND.nextDouble();
}
}
Refer to the
screenshot attached below to better understand the code and
indentation:
Output:
Output may vary after Testing determineLeftOverCake,
because it uses Random as Input.
If my answer helps
you then please Upvote,
for further queries comment below.
Thank You.
LAB PROMPT: Lab 07 Hello future bakers and artisans of the fine gourmet. You love to...
LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing if statements, and improve your skills with for loops. The methods you will be writing have nothing to do with each other but allow you to use the skills you have gained so far to solve a series of small problems. Power For this method you will practice using methods from the Math class. The first step you must take is to write the...
Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following: The instance variables for the class (recipeName, serving size, and...
I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...
Write four overloaded methods called randomize. Each method will return a random number based on the parameters that it receives: randomize() - Returns a random int between min and max inclusive. Must have two int parameters. randomize() - Returns a random int between 0 and max inclusive. Must have one int parameter. randomize() - Returns a random double between min and max. Must have two double parameters. randomize() - Returns a random double between 0 and max. Must have one...
Let's fix the displayMenu() method! First, edit the method to do the following: We want to also allow the user to quit. Include a "Quit" option in the print statements! Have it get the user input from within the method itself and return an int based on the user's choice. (Make sure to also edit the method header and how it is called back in the main() method!) What is the method's return type now? ...
This is for a java program public class Calculation { public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object //Task 1. Write your code here //Print the prompt and ask the user to enter an...
Hello, Can you please error check my javascript? It should do the following: Write a program that uses a class for storing student data. Build the class using the given information. The data should include the student’s ID number; grades on exams 1, 2, and 3; and average grade. Use appropriate assessor and mutator methods for the grades (new grades should be passed as parameters). Use a mutator method for changing the student ID. Use another method to calculate the...
What will the following program display? public class checkpoint { public static void main(String urgs[]) { int n = 1776; doubled = 110.0901; System .out.println(n + \t + d); myMethod(n, d); System.out.println(n + \t + d); } public static void myMethod(int i, double x) { System.out.printing + \t + x); i = 1250; x= 199.99; System.out.println(i + \t" + x); } } Write the following two methods: i. compute Diameter; This method accepts the radius (r) of a circle, and...
Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this: public static double divide(double operand1, double operand2) { return operand1 / operand2; } The random method is supposed to return a double within a lower limit and...
Consider the following methods’ headers: public static int one(int a, char b, double c, String d) public static double two(double x, double y) public static char three(int r, int s, char t, double u) Answer the following questions: Q1) What is the signature of method one? Answer: Q2) What is the return type of method two? Answer: Q3) What the formal parameters of method three? Answer: Q4) How many actual parameters are needed to call the method three? Answer: Q5)...