how to use math.pi in java - java

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.");

Related

Inputing mathematical formula into java

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

Overloading Method with user input

I am new to java and I have an assignment to write a circle calculator program that computes the area, circumference and diameter of a circle. The program will accept input number (radius) and that number will be used to get the area, circumference and diameter of a circle. This time you are going to use separate methods for each computation. For example, to get the value of area you need to write method area that has radius parameter and returns double value.
I'm not sure if my code is right, I am getting zero values.
import java.util.Scanner;
class Circle {
static Scanner sc = new Scanner(System.in);
private double radius;
public double getRadius() {
System.out.print("Enter the radius: ");
int radius = sc.nextInt();
return radius;
}
public void setRadius(double radius) {
}
public double diameter() {
double diameter = 2 * radius;
return diameter;
}
public double area() {
double area = Math.PI * (radius * radius);
return area;
}
public double circumference() {
double circumference = 2 * Math.PI * radius;
return circumference;
}
}
public class Methods {
public static void main(String[] args) {
Circle circleTest = new Circle();
System.out.printf("Radius: %.1f %n", circleTest.getRadius());
System.out.printf("Diameter: %.1f %n", circleTest.diameter());
System.out.printf("Area: %.1f %n", circleTest.area());
System.out.printf("Circumference: %.1f %n", circleTest.circumference());
}
}
Inside your getRadius method you are setting a local variable to the value entered by the user:
int radius = sc.nextInt();
When you calculate the diameter, area, circumference you're using an instance variable - for example:
double diameter = 2 * radius;
Since you never set the instance variable its value by default is 0.

Sphere volume method not working

I have tried many different calculations for trying to get my sphere volume method to work.
My Sphere class is extended from Circle, to get the area from circle, as well as implements my Shape3D interface which allows me to use my volume method.
However, I have tried all of these different formulas for my method and nothing gives me back an accurate volume of a sphere. It is always drastically off.
The one that gets the closest is (4*22*radius * radius * radius )/(3*7); but it is still off.
//return (4/3) * super.area() * radius;
//return (Double)((4*22*getRadius()*getRadius()*getRadius() )/(3*7));
//return (4*22*radius * radius * radius )/(3*7);
//return ( (4/3) * (Math.pow(getRadius(), 3) ) / (Math.PI) );
//return (getRadius() * getRadius() * getRadius() ) * (Math.PI) * (4/3);
//return ( super.area() * getRadius() ) * (4/3);
I am going to attach my code for my Shape abstract class, my Circle class, and my Sphere class, as well as my Shape3D interface.
Maybe I have overlooked something obvious. When I set the radius though and get it the radius returns back normal. So I am not sure why every one of these if completely off.
public class Main {
public static void main(String[] args) {
System.out.println(volume());
}
public static double volume() {
double vol;
double x = 4/3;
double y = Math.pow(30.0, 3);
double z = Math.PI;
vol = y * z * x;
return vol;
//return (4/3) * super.area() * radius;
//return (Double)((4*22*getRadius()*getRadius()*getRadius() )/(3*7));
//return (4*22*radius * radius * radius )/(3*7);
//return ( (4/3) * (Math.pow(getRadius(), 3) ) / (Math.PI) );
//return (getRadius() * getRadius() * getRadius() ) * (Math.PI) * (4/3);
//return ( super.area() * getRadius() ) * (4/3);
}// end sphere volume
}
Here is a simple java test program for calculating the volume of a sphere that you can use as reference for what you are doing wrong which is that 4/3 returns an int, 1 rather than a double representation of and therefore messes up your calculation:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// notice the descriptive variable names rather than x, y and z
double radius;
double diameter;
double volume;
System.out.print("Enter the radius of the sphere:");
radius = scanner.nextDouble();
diameter = (radius * 2.0);
volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3); // instead of 4/3 in yours
System.out.println("The volume is: " + String.format("%.1f", volume));
}
}
Example Usage:
Enter the radius of the sphere: 5
The volume is: 523.6
To confirm it is working correctly you can use Google's sphere volume calculator and confirm it gives the same value:
Instead of
double x = 4/3;
Try
double x = 4.0/3.0;
or
double x = 4.0/3;
or
double x = 4/3.0;

"Error: '.class' expected"

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();

Java - troubleshooting issues with DemoSquare

The first bit of code I am having problems with is DemoSquare - it will crash when I run it. The error is:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable area
location: class Square
at Square.computeSurfaceArea(DemoSquare.java:57)
at DemoSquare.main(DemoSquare.java:23)
Java Result: 1
My code is -
// package demosquare;
import java.util.*;
public class DemoSquare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the height of the square/rectangle");
double sqHeight = input.nextDouble();
System.out.println("Please enter the width of the square/rectangle");
double sqWidth = input.nextDouble();
System.out.println("Please enter the height of the Cube/Rectangular Prism");
double cuHeight = input.nextDouble();
System.out.println("Please enter the width of the Cube/Rectangular Prism");
double cuWidth = input.nextDouble();
System.out.println("Please enter the depth of the Cube/Rectangular Prism");
double cuDepth = input.nextDouble();
// Create a square and print out the information about it
Square square = new Square(sqHeight, sqWidth);
System.out.println("A Square with dimensions " + square.getHeight()
+ " by " + square.getWidth() + " has a surface area of "
+ square.computeSurfaceArea());
// Create a cube and print out the information about it.
Cube cube = new Cube(cuHeight, cuWidth, cuDepth);
System.out.println("A Cube with dimensions " + cube.getHeight()
+ " by " + cube.getWidth() + " by " + cube.getDepth() + " has a surface area of "
+ cube.computeSurfaceArea());
} // end main method
} //end class DemoSquare
class Square {
// enter the code to create the square class here
double sqHeight = 0;
double sqWidth = 0;
public Square(double height, double width) {
sqHeight = height;
sqWidth = width;
}
public double getWidth() {
return sqWidth;
}
public double getHeight() {
return sqHeight;
}
public double computeSurfaceArea() {
double surfaceArea = sqHeight * sqWidth;
surfaceArea = (getHeight() * getWidth());
return area;
}
}
class Cube extends Square {
double sqDepth = 0.00;
// enter the cube class code here
public Cube(double height, double width, double depth) {
super(height, width);
sqDepth = depth;
}
#Override
public double getWidth() {
return sqWidth;
}
#Override
public double getHeight() {
return sqHeight;
}
public double getDepth() {
return sqDepth;
}
#Override
public double computeSurfaceArea() {
//Surface Area = 2hw + 2wd + 2dh
double tsa = (2 * sqHeight * sqWidth) + (2 * sqWidth * sqDepth) + (2 * sqDepth * sqHeight);
return tsa;
}
}
What am I doing wrong in this code?
You can tell by the stacktrace where the error lies. Let's review it.
The reason why the program didn't work:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
This means your code is currently broken and will not execute by any means until at least compiles.
Uncompilable source code - cannot find symbol
symbol: variable area
This means there's an unknown variable with name area that's been used in your code but is never declared.
Then it comes the location of the error:
location: class Square
at Square.computeSurfaceArea(DemoSquare.java:57)
Now we have where to look. Let's go to the Square class, inside the method computeSurfaceArea, more specifically, at line 57 in the file DemoSquare.java.
//54. public double computeSurfaceArea() {
//55. double surfaceArea = sqHeight * sqWidth;
//56. surfaceArea = (getHeight() * getWidth());
//57. return area;
//58. }
Now we found the culprit: return area;. Note that area here is an undeclared variable. As noted in other answers, you probably meant to use surfaceArea instead.
//54. public double computeSurfaceArea() {
//55. double surfaceArea = sqHeight * sqWidth;
//56. surfaceArea = (getHeight() * getWidth());
//57. return surfaceArea;
//58. }
Do this change and the code will be fixed until now.
Do the same process to fix other problems in your code.
Do a similar process when you encounter other exceptions at runtime like NullPointerException.
Here's what you're doing wrong:
public double computeSurfaceArea() {
double surfaceArea = sqHeight * sqWidth;
surfaceArea = (getHeight() * getWidth());
return area; // no such variable - it's called surfaceArea...
}
The variable is called surface area, not area.
I.e., you should be doing:
public double computeSurfaceArea() {
double surfaceArea = sqHeight * sqWidth;
surfaceArea = (getHeight() * getWidth());
return surfaceArea;
}
double surfaceArea = sqHeight * sqWidth;
surfaceArea = (getHeight() * getWidth());
return area;
I think you meant
return surfaceArea ;

Categories