I am having a problem I have not been able to solve searching the internet, basically I need the user to input first the diameter of a sphere then get back the radius and from there use the formula (4 over 3 multiplied by pi (3.14) multiplied by radius (calculated from diameter - user inputted) to the power of 3.
and the other one is very similar..... 4 multiplies by pi (3.14) multiplied by radius to the power of 2.
Now the thing is every time I try to compile I get errors that the method is lossy double not int or the symbol is not recognized.
guys any help would be helpful since I cant find a solution for this.
the code is below :
import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import java.lang.*;
import java.util.Scanner;
import java.util.*;
public class RadiusConverter {
public class Pi{
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int FirstStep, Diameter, Radius, SecondStep, ThirdStep;
System.out.println ("Please enter the diameter of the Sphere: ");
int diameter =scan.nextInt();
System.out.println ("Your Shpere has a radius of: " + FirstStep);
int radius = scan.nextInt();
System.out.println ("The Volume of your Sphere is: " + SecondStep);
System.out.println("The Surface Area of your Sphere is: " + ThirdStep);
FirstStep = Diameter / 2;
SecondStep = 4 / 3 * 3.14 * (int) FirstStep * 3;
ThirdStep = 4 * 3.14 * (int) Radius * 2;
Diameter = diameter;
Radius = radius;
//------------------------------------------------------------------------
//
//
//-------------------------------------------------------------------------
scan.close();
}
}
This is the older source code I was using … which also did not work.
import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import java.lang.*;
import java.util.Scanner;
import java.util.*;
public class RadiusConverter
{
public class Pi{
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
double FirstStep, Diameter, Radius, SecondStep, ThirdStep;
System.out.println ("Please enter the diameter of the Sphere: ");
double diameter =scan.nextDouble();
System.out.println ("Your Shpere has a radius of: " + FirstStep);
double radius =scan.nextDouble();
System.out.println ("The Volume of your Sphere is: " + SecondStep);
System.out.println("The Surface Area of your Sphere is: " + ThirdStep);
FirstStep = diameter / 2;
SecondStep = 4 / 3 * Math.PI* Math.pow (FirstStep, 3);
ThirdStep = 4 * Math.PI * radius * Math.pow (2);
//------------------------------------------------------------------------
//
//
//-------------------------------------------------------------------------
scan.close();
}
}
try out this one
import java.util.Scanner;
public class RadiusConverter {
public class Pi {
}
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int firstStep = 0, diameter = 0, radius = 0, secondStep = 0, thirdStep = 0;
System.out.print("Please enter the diameter of the Sphere: ");
diameter = scan.nextInt();
System.out.print("Please enter the radius of the Sphere: ");
radius = scan.nextInt();
firstStep = diameter / 2;
secondStep = (int) (4 / 3 * 3.14 * firstStep * 3);
thirdStep = (int) (4 * 3.14 * radius * 2);
System.out.println("Your Shpere has a radius of: " + firstStep);
System.out.println("The Volume of your Sphere is: " + secondStep);
System.out.println("The Surface Area of your Sphere is: " + thirdStep);
}
}
}
Output
Please enter the diameter of the Sphere: 250
Please enter the radius of the Sphere: 25
Your Shpere has a radius of: 125
The Volume of your Sphere is: 1177
The Surface Area of your Sphere is: 628
Related
I am new to Java. I am trying to calculate the area of a triangle using the formula:
s = (side 1 + side 2 + side 3)/2
area = square root (side (side - side 1)(side - side2)(side - side3).
If the user enter the three point as:
1.5 -3.4 4.6 5 9.5 -3.4 then the area of the triangle should be 33.6. However, my program runs, but it's giving me an incorrect answer. Below is my code.
// Import Java Scanner
import java.util.Scanner;
import java.lang.Math;
public class Ex_2_19 {
public static void main(String[] args) {
//Create a Scanner object
Scanner input = new Scanner(System.in);
float side = 0;
float area1 = 0;
float area2 = 0;
float area3 = 0;
float area4 = 0;
float calculatedarea = 0;
//Prompt the user to enter three points of a triangle
System.out.println("Enter point x1:");
System.out.println("Enter point y1:");
System.out.println("Enter point x2:");
System.out.println("Enter point y2:");
System.out.println("Enter point x3:");
System.out.println("Enter point y3:");
//Define the variables
float Pointx1 = input.nextFloat();
float Pointy1 = input.nextFloat();
float Pointx2 = input.nextFloat();
float Pointy2 = input.nextFloat();
float Pointx3 = input.nextFloat();
float Pointy3 = input.nextFloat();
//Formula to calculate the area of a triangle
side = (Pointx1 + Pointy1 + Pointx2 + Pointy2 + Pointx3 + Pointy3) / 2;
area1 = side - (Pointx1 + Pointy1);
area2 = side - (Pointx2 + Pointy2);
area3 = side - (Pointx3 + Pointy3);
area4 = side * area1 * (area2) * (area3);
calculatedarea = (float) (Math.sqrt(area4));
//calculatedarea = (float) (Math.sqrt(area1)*(area2) * (area3));
//Print result
System.out.println("The area of the triangle is " + calculatedarea);
}
}
You are trying Heron's Formula - note that a, b, c are the euclidean distance between the points, thus will need to be computed by sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)), etc., not just the sum of differences.
Got scolded earlier for not specify the question. Promise to do better this time.
For some reason, the loop doesn't stop when i enter 0, instead it continues to print out the main method again. How do i fix this? I couldn't find out what is wrong with it, since everything else runs smoothly.
Also, I want to use Switch instead of If, but i keep getting Duplicate Variable error. Why?
import java.util.Scanner;
public class FindArea
{
// side is only input provided to cube. Area of the cube is returned
public static double cube (double side)
{
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere (double radius)
{
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder (double radius, double height)
{
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut (double outerR, double innerR)
{
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
do
{
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
} while (n != 0);
System.exit(1);
}
}
It prints the output one more time because the comparison is not made until the end of the Do-While loop.
It checks for all cases of n other than 0, and prints the output no matter what.
I think what you're looking for is more so:
n = input.nextInt();
if(n!=0){
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
}
note the n!=0
Additionally look into the use of switch-case, once you learn them these comparisons will not only be faster, but very easy to comprehend
You can try this with your switch case -
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
else{
switch(n){
case 0:{
System.out.println("You enter 0 to exit");
break;
}
case 1:{
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
}
case 2:{
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
break;
}
case 3: {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
}
case 4:{
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
}
}
}
}
i have done this using switch case. Very simple as well
public class FindArea {
// side is only input provided to cube. Area of the cube is returned
public static double cube(double side) {
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere(double radius) {
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder(double radius, double height) {
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut(double outerR, double innerR) {
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut, Or enter 0 to exit ");
int n = 0;
double side = 0;
double Area = 0;
double radius = 0;
double height = 0;
n = input.nextInt();
switch (n) {
case 0:
System.out.println("Exit");
System.exit(1);
break;
case 1:
System.out.print("Enter side measurement of cube: ");
side = input.nextDouble();
Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
case 2:
System.out.print("Enter radius measurement of sphere: ");
radius = input.nextDouble();
Area = sphere(radius);
System.out.println("The area of the sphere is: " + Area);
break;
case 3:
System.out.print("Enter radius measurement of cylinder: ");
radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
height = input.nextDouble();
Area = cylinder(radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
case 4:
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
Area = doughnut(outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
default:
System.out.println("--------");
System.out.println("Invalid option selected");
System.out.println("--------");
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
break;
}
}
}
Good Luck.
My code compiles, but the output won't show. I think my methods are all correct, but I have no idea why my output will not display. (NB: beginner in Java)
import java.util.Scanner;
import java.lang.Math;
public class Lab8 {
public static void main(String[] args) {
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius = in.nextDouble();
System.out.print("Enter height: ");
height = in.nextDouble();
}
public static double Calculations(double radius, double height) {
double surfaceArea = (2 * Math.PI * radius * radius) + (2 * Math.PI * radius * height);
return surfaceArea;
}
public static double calculations(double radius, double height) {
double volume = Math.PI * radius * radius * height;
return volume;
}
public static void output(double surfaceArea, double volume) {
System.out.println("Surface Area of Cylinder: " + surfaceArea);
System.out.println("Voulme of Cylinder: " + volume);
}
}
You need to call your method inside main to print the value.
System.out.println("surfacearea " + Calculations(radius,height));
System.out.println("volume " +calculations(radius,height));
System.out.println("output " + output(radius,height));
Fixed Code:
import java.util.Scanner;
import java.lang.Math;
public class Lab8
{
public static void main(String[] args)
{
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius= in.nextDouble();
System.out.print("Enter height: ");
height= in.nextDouble();
System.out.println("surfacearea " + Calculations(radius,height));
System.out.println("volume " +calculations(radius,height));
System.out.println("output " + output(radius,height));
}
public static double Calculations(double radius, double height)
{
double surfaceArea= (2 * Math.PI * radius * radius) + (2 * Math.PI *radius* height);
return surfaceArea;
}
public static double calculations(double radius,double height)
{
double volume= Math.PI * radius * radius * height;
return volume;
}
public static void output(double surfaceArea, double volume)
{
System.out.println("Surface Area of Cylinder: " + surfaceArea);
System.out.println("Voulme of Cylinder: " + volume);
}
}
Try calling one of your Calculations method in your main method, post taking the input such as:
System.out.println(Lab8.Calculations(radius,height));
In main you miss the calculate and output method call
public static void main(String[] args)
{
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius= in.nextDouble();
System.out.print("Enter height: ");
height= in.nextDouble();
// call this
System.out.println("area " + output(Calculations(radius,height)));
}
You don't actually call your methods.
In main, after you get the radius and height, call Calculations and calculations respectively, with the appropriate arguments.
Here's an example:
surfaceArea = Calculations(radius, height);
volume = calculations(radius, height);
You didn't call any of your helper functions within main(...). Try adding a call to output or calculations within your main function.
In particular, something like
output(Calculations(radius, height), calculations(radius, height));
will probably suffice.
As you learn Java, it may be helpful to begin applying descriptive names to your functions, such as CylinderSurfaceArea to help keep confusion to a minimum both for you and for anyone who might read code you have written.
So I'm new to Java, and my most recent piece of code has not worked. I get error: not a statement twice and error: ';' expected once.
If I try radius=double;, it appears with the error: Error: '.class' expected on line 8, where the caret shows under the semi-colon.
I am not sure what is wrong, but here is my code. It isn't long... thank-you in advance.
import java.util.Scanner;
import static java.lang.Math;
public class Formula {
public static void main(String[] args);{
double = radius;
Scanner in = new Scanner(System.in);
System.out.print("Radius of circle (cm) :> ");
double radius = in.nextDouble();
System.out.print("Area of circle :> " + Math.PI * Math.pow(radius,2));
System.out.print("Circumference of circle :> " + 2 * Math.PI * radius);
System.out.print("Surface area the sphere with that radius :> " + 4 * Math.PI * Math.pow(radius,2));
System.out.print("Volume of sphere with that radius :> " + 4/3 * (radius * Math.pow(radius,2)));
}
}
Change
double = radius;
to
double radius = 0;
and remove the ; after the public static void main(String[] args); method definition.
Also, in the statement double radius = in.nextDouble(); you will have to remove the double keyword, since you have already defined a variable with the same name.
You have three problems with your code:
Remove the semi-colon on the declaration of the main method.
public static void main(String[] args)
Your second problem is that you do not have a reference for your double variable. All variables must have references. Consider your code which should be:
double radius = 0;
double is the data type and radius is the reference. In other words, double is the type of variable and radius is the name of the variable.
Your third problem is this line.
double radium = in.nextDouble();
You should change it to:
radius = in.nextDouble();
All variables must be correctly referenced. Also by declaring the variable again, you're shadowing the old one.
It would be better to instead of initializing the variable then initializing it again, delete your line:
double = radius
or if you changed it to what I said above, remove
double radius = 0;
Remove the ';' from your main
public static void main(String[] args);
to
public static void main(String[] args)
Your variable declarations are confused: double = radius; doesn't make sense because double is a type, not a variable (and variable declarations look like type identifier = value; not type = identifier; or identifier = type;), and you declare but never use the radium variable. Should be something like:
Scanner in = new Scanner(System.in);
System.out.print("Radius of circle (cm) :> ");
double radius = in.nextDouble();
System.out.print("Area of circle :> " + Math.PI * Math.pow(radius,2));
System.out.print("Circumference of circle :> " + 2 * Math.PI * radius);
System.out.print("Surface area the sphere with that radius :> " + 4 * Math.PI * Math.pow(radius,2));
System.out.print("Volume of sphere with that radius :> " + 4/3 * (radius * Math.pow(radius,2)));
And you shouldn't have that ; at the end of your main method declaration line.
culprit (i.e. wrong way of declaring a variable):
double = radius;
should be something of this form:
double radius = 0;
However, I see you are using radius again, but this time correctly, to get the value entered by the user from the console, so you can safely delete the culprit statement and it should work.
The code that you have :
import java.util.Scanner;
import static java.lang.Math;
public class Formula
{
public static void main(String[] args);
{
double = radius;
Scanner in = new Scanner(System.in);
System.out.print("Radius of circle (cm) :> ");
double radius = in.nextDouble();
System.out.print("Area of circle :> " + Math.PI * Math.pow(radius,2));
System.out.print("Circumference of circle :> " + 2 * Math.PI * radius);
System.out.print("Surface area the sphere with that radius :> " + 4 * Math.PI * Math.pow(radius,2));
System.out.print("Volume of sphere with that radius :> " + 4/3 * (radius * Math.pow(radius,2)));
}
}
What it should look like :
import java.util.Scanner;
import static java.lang.Math;
public class Formula
{
public static void main(String[] args) {
double radius = 0;
Scanner in = new Scanner(System.in);
System.out.print("Radius of circle (cm) :> ");
radius = in.nextDouble();
System.out.print("Area of circle :> " + Math.PI * Math.pow(radius,2));
System.out.print("Circumference of circle :> " + 2 * Math.PI * radius);
System.out.print("Surface area the sphere with that radius :> " + 4 * Math.PI * Math.pow(radius,2));
System.out.print("Volume of sphere with that radius :> " + 4/3 * (radius * Math.pow(radius,2)));
}
}
You had a semicolon after your method, declaring a double needs to equal something, and you don't need to say double radius = in.nextDouble(); you just need radius = in.nextDouble();
I am having problems converting this formula V = 4/3 π r^3. I used Math.PI and Math.pow, but I get this error:
';' expected
Also, the diameter variable doesn't work. Is there an error there?
import java.util.Scanner;
import javax.swing.JOptionPane;
public class NumericTypes
{
public static void main (String [] args)
{
double radius;
double volume;
double diameter;
diameter = JOptionPane.showInputDialog("enter the diameter of a sphere.");
radius = diameter / 2;
volume = (4 / 3) Math.PI * Math.pow(radius, 3);
JOptionPane.showMessageDialog("The radius for the sphere is "+ radius
+ "and the volume of the sphere is ");
}
}
You're missing the multiplication operator. Also, you want to do 4/3 in floating point, not integer math.
volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
^^ ^
Here is usage of Math.PI to find circumference of circle and Area
First we take Radius as a string in Message Box and convert it into integer
public class circle {
public static void main(String[] args) {
// TODO code application logic here
String rad;
float radius,area,circum;
rad = JOptionPane.showInputDialog("Enter the Radius of circle:");
radius = Integer.parseInt(rad);
area = (float) (Math.PI*radius*radius);
circum = (float) (2*Math.PI*radius);
JOptionPane.showMessageDialog(null, "Area: " + area,"AREA",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "circumference: " + circum, "Circumfernce",JOptionPane.INFORMATION_MESSAGE);
}
}
Replace
volume = (4 / 3) Math.PI * Math.pow(radius, 3);
With:
volume = (4 * Math.PI * Math.pow(radius, 3)) / 3;
Your diameter variable won't work because you're trying to store a String into a variable that will only accept a double. In order for it to work you will need to parse it
Ex:
diameter = Double.parseDouble(JOptionPane.showInputDialog("enter the diameter of a sphere.");