Question
I have gone through my textbook multiple times and it doesn't give me an example code of how I would set this up. It's the
I have gone through my textbook multiple times and it doesn't give me an example code of how I would set this up. It's the only part of our assignment that I can't get right.
Add a line that prompts the user to enter the diameter of a sphere.
Read in and store the number into a variable called diameter (you will need to declare any variables that you use).
The diameter is twice as long as the radius, so calculate and store the radius in an appropriately named variable.
The formula for the volume of a sphere is:
V=(4/3)pie r3
Convert the formula to Java code and add a line which calculates and stores the value of volume in an appropriately named variable. Use Math.PI for p and Math.pow to cube the radius.
Print your results to the screen with an appropriate message. So far this is what I've accomplished... which doesn't seem to work. int diameter; int radius; int volume; // TASK #4 declare variables used here
// ADD LINES FOR TASK #4 HERE System.out.print("Enter an integer for the diameter of a sphere "); // Prompt the user for a diameter of a sphere diameter = radius.nextLine(); // Read the diameter radius = Math.pow(diameter/2, 3.0);// Calculate the radius volume= ((4/3) * Math.PI * radius); // Calculate the volume System.out.println(volume);// Print out the volume
I receive the following errors:
----jGRASP exec: javac -g NumericTypesAlt.java NumericTypesAlt.java:69: error: int cannot be dereferenced diameter = radius.nextLine(); // Read the diameter ^ NumericTypesAlt.java:70: error: incompatible types: possible lossy conversion from double to int radius = Math.pow(diameter/2, 3.0);// Calculate the radius ^ NumericTypesAlt.java:71: error: incompatible types: possible lossy conversion from double to int volume= ((4/3) * Math.PI * radius); // Calculate the volume ^ 3 errors
----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
How would this be corrected? And could you explain how it works?
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