Please let with the following. I have the first part of the codd cone but I am s
ID: 3910465 • Letter: P
Question
Please let with the following. I have the first part of the codd cone but I am stuggling getting what is being asked to work. This is what is needed:
Goals
• Use a switch statement.
• Use looping structure to repeatedly prompt the user for more.
• Use a condition and break to exit the loop.
Problem
While the Weight program is helpful, we rarely want to calculate the weight of one piece of steel as a
shipment has many pieces of steel. To make the program more useful, it needs to be changed to
continuously prompt the user for another piece of steel until the user indicates that no more weight
calculations are needed.
Furthermore, feedback from the users is that it is too hard to remember all of thicknesses of the plates that
are fractions of an inch (e.g. 1/8 is .125) and they would rather just type in an acceptable thickness.
User Stories
Story 1 - Thickness as a Fraction Story
Users would like to enter in the following dimensions for a plate:
Enter dimensions: 1/4 4 11 5 7
Furthermore, there are only limited thicknesses of plates that are available which are:
• 1/8 (.125)
• 1/4 (.25)
• 1/2 (.5)
If users enter in any plate thicknesses that is not allowed, an error is presented and the weight will not be
calculated. Here is an example of a user providing a bad thickness:
Enter dimensions: 3/16 4 11 5 7
Error: The plate thickness is not an allowed thickness.
Story 1 - Thickness as a Fraction Tasks
1. Open your Weight project in NetBeans using File > Open Project and selecting the project. This is
important because it is doing this that allows the build and run interfaces to know which project you
want to build and run.
2. Drill down into Source Packages > weight > Weight.java in the left panel under the Weight project to
get at the Java source for the project. Double-left mouse click Weight.java to edit the source.
3. Find the place in your code where you are reading in the thickness as a decimal value and change it to
reading string input.
Change this: float thickness_in = input.nextFloat();
To this: float thickness_in = 0;
String thickness_frac = input.next();
4. Add a switch statement right after you read the thickness fraction and set thickness_in from it.
a. Note that if you are using float for thickness_in, the following statement issues a lossy
warning:
thickness_in = 0.25;
b. Why? To fix, you will need to cast the literal as a float or change thickness_in to double.
thickness_in = (float)0.25;
5. Avoid calculating the weight when a bad thickness is given by wrapping it in a condition. Use
Source > Format to fix the indentation after you added the condition wrapper around you existing
code.
if (thickness_in != 0) {
Your previous weight calculation code here.
}
6. Try running the program with a bad thickness and notice that you get four additional errors. This is
because the four inputs for the width and length are still available to read. For example, if you
provided “3/16 2 1 3 2” you would get bad thickness for the 3/16, 2, 1, 3, and the 2. To avoid this,
you have to move the check below to after all of the inputs.
Your previous input code here.
if (thickness_in != 0) {
Your previous weight calculation code here.
}
Story 2 - Multiple Weights Story
Users would like to have the weight program prompt for many plates until they enter the word “end” like the
following:
Enter dimensions: 1/2 2 5 4 8
Weight: 229.78471
Enter dimensions: 3/16 2 5 4 8
Error: The plate thickness is not an allowed thickness.
Enter dimensions: 1/4 4 11 5 7
Weight: 279.66104
end
Story 2 - Multiple Weights Tasks
1. Go to the section of your code that is prompting for input.
System.out.print("Enter dimensions: ");
2. Add a line to loop forever immediately before the user prompt for dimensions.
while (true) {
System.out.print("Enter dimensions: ");
3. Complete the loop by adding a closing brace immediately after you output the weight calculation.
System.out.println(weight);
}
4. Next, get the indentation correct for the source code by selecting Source > Format.
5. At this point, the program will loop forever as there is nothing that is getting us out of the loop. To
get out of the loop when the user enters end, check the first dimension entered (thickness_frac) to
see if it is “end” and if so, break out of the loop.
if (thickness_frac.equals(“end”)) {
break;
}
Note: Be careful cutting and pasting the above code as the double-quotes in Word are not the same as
those in NetBeans.
Checklist
Use the following as guidance for getting a 7 on this project:
• User can input a fraction instead of a decimal (2 points)
• Supported fractions are used to calculate weight and an error is produced if fraction not supported (2
points)
• User can get the weights of many plates on a single program run (2 points)
• User enters “end” at the dimension prompt to terminate the loop (1 point)
Test Cases
Thickness Width Ft. Width In. Length Ft. Length In. Weight
1/4 5 6 2 3 126.07
1/2 2 1 3 2 134.4184
3/16 2 1 3 2 Error
end
Here is my code so far. Where i am having issues is having the user input the thickness in 1/4 i keep getting an error:
package weight;
import java.util.Scanner;
public class Weight {
public static void main(String[] args) {
double thickness_in;
double thickness_ft;
System.out.print("Enter Dimensions: ");
Scanner input = new Scanner(System.in);
thickness_in = input.nextDouble();
thickness_ft = thickness_in / 12.0;
int width_in_ft = input.nextInt();
int width_in_in = input.nextInt();
double width_ft = width_in_ft + width_in_in / 12.0;
int length_in_ft = input.nextInt();
int length_in_in = input.nextInt();
double length_ft = length_in_ft + length_in_in / 12.0;
System.out.println("Weight of slab is " + (thickness_ft * width_ft * length_ft * 489));
}
}
Please advise THANKS
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Weight.java
import java.util.Scanner;
public class Weight {
public static void main(String[] args) {
double thickness_in;
double thickness_ft;
/**
* looping infinitely
*/
while (true) {
System.out.print("Enter Dimensions: ");
Scanner input = new Scanner(System.in);
thickness_in = 0;
//getting thickness fraction as string
String thickness_frac = input.next();
if (thickness_frac.equals("end")) {
//sentinel value entered, end of the loop
break;
}
/**
* switching thickness fraction to identify thickness_in
*/
switch (thickness_frac) {
case "1/8":
thickness_in = (float) 0.125;
break;
case "1/4":
thickness_in = (float) 0.25;
break;
case "1/2":
thickness_in = (float) 0.5;
break;
}
/**
* if the thickness_in is still 0, then the entered thickness
* fraction is not valid
*/
if (thickness_in == 0) {
System.out.println("Error: The plate thickness is not an allowed thickness");
} else {
//valid fraction, calculating other values
thickness_ft = thickness_in / 12.0;
int width_in_ft = input.nextInt();
int width_in_in = input.nextInt();
double width_ft = width_in_ft + width_in_in / 12.0;
int length_in_ft = input.nextInt();
int length_in_in = input.nextInt();
double length_ft = length_in_ft + length_in_in / 12.0;
System.out.println("Weight: "
+ (thickness_ft * width_ft * length_ft * 489));
}
}
}
}
/*OUTPUT*/
Enter Dimensions: 1/2 2 5 4 8
Weight: 229.7847222222222
Enter Dimensions: 3/16 2 5 4 8
Error: The plate thickness is not an allowed thickness
Enter Dimensions: 1/4 4 11 5 7
Weight: 279.66102430555554
Enter Dimensions: end