Programmer102 - 10 months ago 31

Java Question

write a program that takes the radius of a sphere(a double) as input and outputs the sphere's diameter,radius,circumferance, surface area and volume

`public class Sphere{`

public static void main(String[] args){

double radius;

double diameter;

double circumference;

double surface area;

double volume;

diameter of a sphere = (2)(r)

circumference ofa sphere = (2)(3.14)(r)

surface area of a sphere = 4(3.14)(r*r)

volume of a sphere = 4/3(3.14)(r*r*r)

Scanner input = new Scanner( System.in );

System.out.println("The diameter of this sphere is " + diameter);

System.out.println("The circumference of this sphere is " + circumference);

System.out.println("The surface area of this sphere is " + surface area);

System.out.println("The volume of this sphere is " + volume);

}

}

output -

----jGRASP exec: javac -g Sphere.java

Sphere.java:15: error: ';' expected

double surface area;

^

Sphere.java:15: error: not a statement

double surface area;

^

Sphere.java:18: error: ';' expected

diameter of a sphere = (2)(r)

^

Sphere.java:18: error: ';' expected

diameter of a sphere = (2)(r)

^

Sphere.java:18: error: not a statement

diameter of a sphere = (2)(r)

^

Sphere.java:18: error: ';' expected

diameter of a sphere = (2)(r)

^

Sphere.java:19: error: ';' expected

circumference ofa sphere = (2)(3.14)(r)

^

Sphere.java:19: error: ';' expected

circumference ofa sphere = (2)(3.14)(r)

^

Sphere.java:19: error: not a statement

circumference ofa sphere = (2)(3.14)(r)

^

Sphere.java:19: error: ';' expected

circumference ofa sphere = (2)(3.14)(r)

^

Sphere.java:20: error: ';' expected

surface area of a sphere = 4(3.14)(r

^

Sphere.java:20: error: ';' expected

surface area of a sphere = 4(3.14)(r

^

Sphere.java:20: error: ';' expected

surface area of a sphere = 4(3.14)(r

^

Sphere.java:20: error: not a statement

surface area of a sphere = 4(3.14)(r

^

Sphere.java:20: error: ';' expected

surface area of a sphere = 4(3.14)(r

^

Sphere.java:21: error: ';' expected

volume of a sphere = 4/3(3.14)(r

^

Sphere.java:21: error: ';' expected

volume of a sphere = 4/3(3.14)(r

^

Sphere.java:21: error: not a statement

volume of a sphere = 4/3(3.14)(r

^

Sphere.java:21: error: ';' expected

volume of a sphere = 4/3(3.14)(r

^

Sphere.java:30: error: ')' expected

System.out.println("The surface area of this sphere is " + surface area);

^

Sphere.java:30: error: illegal start of expression

System.out.println("The surface area of this sphere is " + surface area);

^

21 errors

Answer Source

There are so many issues in your code :

You cannot have space in variable name

**surface area**.You are not reading any input from a user or not defining the value of radius.

- You have to use arithmetic operator (*) to multiply numbers.

Here are few changes I have made :

```
double radius;
double diameter;
double circumference;
double surfacearea;
double volume;
double r;
Scanner input = new Scanner( System.in );
System.out.println("Intput the radius :");
r= input.nextDouble(); //reads the radius input from user.
diameter = 2*r; //use the varibale you defined here.
circumference = 2*3.14*(r);
surfacearea= 4*3.14*r*r;
volume = (4/3) *3.14*r*r*r;
System.out.println("The diameter of this sphere is " + diameter);
System.out.println("The circumference of this sphere is " + circumference);
System.out.println("The surface area of this sphere is " + surfacearea);
System.out.println("The volume of this sphere is " + volume);
}
```