Write an application that uses Circle class you created in the previous assignment.

? The program includes a static method createCircle() that

o reads a radius of a circle from the user

o creates a circle object, and

o returns the object.

? The program creates a circle object by using the above method.

? The program passes the circle object to a static method that prints the circumference and the area of the circle object by using the methods in the object.

? The program includes a static method createCircle() that

o reads a radius of a circle from the user

o creates a circle object, and

o returns the object.

? The program creates a circle object by using the above method.

? The program passes the circle object to a static method that prints the circumference and the area of the circle object by using the methods in the object.

View Answers

August 17, 2010 at 4:17 PM

Hi Friend,

Try the following code:

import java.util.*;

class Circle{

static double pi=3.14;

double radius;

Circle(){

}

Circle(double radius){

this.radius=radius;

}

public double getRadius(){

return radius;

}

public static Circle createCircle(){

System.out.println("Enter Radius");

Scanner input=new Scanner(System.in);

double r=input.nextDouble();

Circle c=new Circle(r);

return c;

}

public static void calculate(Circle c){

double r=c.getRadius();

double area=pi*r*r;

double circumference=2*pi*r;

System.out.println("Area: "+area);

System.out.println("Circumference: "+circumference);

}

public static void main(String[] args)

{

Circle c=createCircle();

calculate(c);

}

}import java.util.*;

class Circle{

static double pi=3.14;

double radius;

Circle(){

}

Circle(double radius){

this.radius=radius;

}

public double getRadius(){

return radius;

}

public static Circle createCircle(){

System.out.println("Enter Radius");

Scanner input=new Scanner(System.in);

double r=input.nextDouble();

Circle c=new Circle(r);

return c;

}

public static void calculate(Circle c){

double r=c.getRadius();

double area=pi*r*r;

double circumference=2*pi*r;

System.out.println("Area: "+area);

System.out.println("Circumference: "+circumference);

}

public static void main(String[] args)

{

Circle c=createCircle();

calculate(c);

}

}

Thanks

Try the following code:

import java.util.*;

class Circle{

static double pi=3.14;

double radius;

Circle(){

}

Circle(double radius){

this.radius=radius;

}

public double getRadius(){

return radius;

}

public static Circle createCircle(){

System.out.println("Enter Radius");

Scanner input=new Scanner(System.in);

double r=input.nextDouble();

Circle c=new Circle(r);

return c;

}

public static void calculate(Circle c){

double r=c.getRadius();

double area=pi*r*r;

double circumference=2*pi*r;

System.out.println("Area: "+area);

System.out.println("Circumference: "+circumference);

}

public static void main(String[] args)

{

Circle c=createCircle();

calculate(c);

}

}import java.util.*;

class Circle{

static double pi=3.14;

double radius;

Circle(){

}

Circle(double radius){

this.radius=radius;

}

public double getRadius(){

return radius;

}

public static Circle createCircle(){

System.out.println("Enter Radius");

Scanner input=new Scanner(System.in);

double r=input.nextDouble();

Circle c=new Circle(r);

return c;

}

public static void calculate(Circle c){

double r=c.getRadius();

double area=pi*r*r;

double circumference=2*pi*r;

System.out.println("Area: "+area);

System.out.println("Circumference: "+circumference);

}

public static void main(String[] args)

{

Circle c=createCircle();

calculate(c);

}

}

Thanks

Related Tutorials/Questions & Answers: