2.22 Lab 2b: Evaluating Word Problems A brief note from your instructor: SUBMISS
ID: 3731821 • Letter: 2
Question
2.22 Lab 2b: Evaluating Word Problems
A brief note from your instructor:
SUBMISSION HELP
Please do not spend loads of time trying to get floating-point values to work out with complete precision. We will examine your program for correctness visually.
The best way to let us know you have the "right answer but are not getting the points" is to FOR EACH OCURRANCE, MAKE A COMMENT IN THE CODE TO EXPLAIN THE SITUATION. Then the grader will know exactly where to examine and can award missing points.
Introduction
Now that you've had practice translating some simple equations, let's give some context to the math and solve some word problems. Don't forget you'll need to use a Scanner to get the values for the variables. Refer back to the previous lab if you need some help writing the code for it.
Task
Problem A
Jimmy and 4 of his friends are at the movies and he was asked to purchase the snacks. The prices are as follows:
Calculate the price of buying p bags of popcorn, c candy bars, s sodas, and w waters.
They all agreed to split the price evenly, calculate the amount each person should pay.
Problem B
Jane is planning to put a new fence around her flower garden. She wants to change the shape, but doesn't want to change the amount of space it occupies. It is currently a square that has sides of length squareSide. She wants to make it a rectangle with one set of parallel sides twice the length of the other set of parallel sides. What should the length of the sides be?
Data Type Hint: Can you purchase half a bag of popcorn/cand bar/soda/water? Does it make sense to have decimals for these variables? What about for the price or the length of a fence side?
public class WordProblems {
public static void main(String[] args) {
//ID comment goes here!!!!!
/* Your variables go here! Including the Scanner*/
/*Solution to Problem A the numbers are read in the order of price listing (p,c,s,w)*/
System.out.println("Price of the snack: $" + /*your variable(s) name*/);
System.out.println("Individual price: $" + /*your variable(s) here*/);
/*Solution to Problem B
Note: the squareroot function is Math.sqrt(variable)
*/
System.out.println("Length of long side: " + /*your variable here*/);
System.out.println("Length of short side: " + /*your variable here*/);
}
}
Explanation / Answer
Please find my answer for Q2 (Part B)
Please repost other in separate post.
Below is your code... There were few issues in your code..
first one is that you were not calculating area properly. You were calculating it as Math.pow(squareSide,squareSide)
it means 55 i.e. 5*5*5*5*5 ..... but it should be Math.pow(squareSide,2)
Second was some selecting the correct formula for both length and width.
I have corrected the code and output is coming properly. below is your code.
double squareSide = 0;
double rectangleLength = 0;
double rectangleWidth = 0;
double area = 0;
squareSide = scan.nextDouble();
area = Math.pow(squareSide, 2);
rectangleWidth = squareSide/Math.sqrt(0.5);
rectangleLength = squareSide / Math.sqrt(2.0);
rectangleWidth = Math.sqrt(area / 2);
// rectangleLength = rectangleWidth * 2;
rectangleLength = Math.sqrt(area / 2) * 2;
// rectangleWidth = Math.sqrt(2 * rectangleLength) / 2;
// rectangleLength = Math.sqrt(squareSide);
System.out.print("Length of long side: " + rectangleLength);
System.out.print(" ");
System.out.printf("Length of short side: " + rectangleWidth);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.