Please help I need to do this program: 1. Create a Project called “Weight” (File
ID: 3903761 • Letter: P
Question
Please help I need to do this program:
1. Create a Project called “Weight” (File > New Project, Java/Java Application, Next, Finish)
2. Add the code for “Enter dimensions: “ prompt.
a. Note that System.out.println() produces a line feed after the output where System.out.print()
does not.
System.out.print(“Enter dimensions: ”);
b. Build and run to verify that you have a prompt without a line feed after it.
3. Add the scanner to your code so you can read the dimensions from the console.
a. The Scanner is a utility that allows you to read input from the console. We’ll talk more about
utilities later, but for now add the following to your code so you can use the scanner.
import java.util.Scanner;
public class Weight {
...
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(“Enter dimensions: ”);
b. Build and run to verify that you have added the scanner successfully.
4. Read in thickness in inches, convert it to feet
a. Declare thickness in inches (suggested variable name is thickness_in)
b. Declare thickness in feet (suggested variable name is thickness_ft)
c. Question: Are you going to use float or double? Why?
d. Read thickness in inches using the scanner. Here’s an example of scanner input (you will
need to change the code for your above declarations):
float my_float_var = input.nextFloat();
double my_double_var = input.nextDouble();
int my_int_var = input.nextInt();
e. Convert thickness in inches to thickness in feet.
f. Add a temporary line of code so you can confirm you are doing it correctly:
System.out.println(thickness_ft);
g. Build and run to verify that you have read and calculated the thickness in feet.
5. Add code to read and convert width feet and inches.
a. Feet and inches will be read from input always be integers (use input.nextInt() to read), but
when you convert the feet and inches to fractions of a ft, you need to declare float or double.
b. Read and convert width using the following:
int width_num_ft = input.nextInt();
int width_num_in = input.nextInt();
float width_ft = width_num_ft + width_num_in / 12;
c. Add a temporary line of code so you can confirm you are doing it correctly:
System.out.println(width_ft);
d. Question: Does it look right? If not, fix it. First try changing the 12 to 12.0. What happens?
Next try “casting” width_num_in to a float.
6. Add code to read and convert length feet and inches.
a. Hint: Copy and paste the width code and change the variable names.
7. Add code to declare, calculate, and output the weight of the plate of the given dimensions.
8. Clean up the code be commenting out or removing the temporary output lines.
9. Test and verify with the following inputs
This is what I have:
import java.util.Scanner;
public class Weight {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Dimensions");
double thickness_in = input.nextDouble();
double thickness_ft = input.nextDouble();
int width_in_ft = input.nextInt();
int width_in_in = input.nextInt();
double width_ft = width_in_ft + width_in_in / 12.0;
System.out.println("Width in Ft");
}
}
Do I need to declare the math functions? Math.pow to calculate the answer? This if for Java Programming
Explanation / Answer
You can skip the use of math functions.
First we need to find the volume of the plate which can calculate using this:
Volume = (22/7)*(width_ft*width_ft)/4; /* Here you could have used Math.pow function but it can be replaced with this statement. */
After this for calculating weight, you need to take the density as input.
Let's say the density is d, then
weight = Volume*d;
System.out.println("Weight of the plate: "+weight);
Make sure to declare all variables as double or float.