"Error: '.class' expected" - java

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

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;

My program will not display any output

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.

how to use math.pi in 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.");

Categories