You are on page 1of 3

PUBLIC CLASS CIRCLE

public class Circle {

// private instance variable, not accessible from outside this class

private double radius;

private String color;

// Constructors (overloaded)

/** Constructs a Circle instance with default value for radius and color */

public Circle() { // 1st (default) constructor

radius = 1.0;

color = "red";

/** Constructs a Circle instance with the given radius and default color */

public Circle(double radius, String color) { // 2nd constructor

this.radius = radius;

this.color = color;

/** Returns the radius */

public double getRadius() {

return radius;

/** Returns the area of this Circle instance */

public double getArea() {

return radius * radius * Math.PI;

/**

 * @return the color

 */

public String getColor() {

return color;
}

/**

 * @param color the color to set

 */

public void setColor(String color) {

this.color = color;

/**

 * @param radius the radius to set

 */

public void setRadius(double radius) {

this.radius = radius;

Class CircleTest

import java.util.Scanner;

/**

 *

 * A Test Driver for the Circle class

 *

 */

public class TestCircle extends Circle {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Declare an instance of Circle class called c1.

Circle c1 = new Circle();

// Invoke public methods on instance c1, via dot operator.

System.out.println("The first circle is " + c1.getColor() + "and has radius of " + c1.getRadius()

+ " and area of " + String.format("%.3f", c1.getArea()));


// Getting radius input

System.out.print("Enter radius: ");

double myradius = sc.nextDouble();

// Getting color input

System.out.print("Enter color: ");

String mycolor = sc.next();

// Declare an instance of class circle called c2.

Circle c2 = new Circle(myradius, mycolor);

// Invoke public methods on instance c2, via dot operator.

System.out.println("The second circle is " + c2.getColor() + " and has radius of " + c2.getRadius()

+ " and area of " + String.format("%.3f", c2.getArea()));

// Getting radius input

System.out.print("Enter the new radius: ");

c2.setRadius(sc.nextDouble());

// Getting color input

System.out.print("Enter the new color: ");

c2.setColor(sc.next());

System.out.println("The second circle is now " + c2.getColor() + " and it's radius became " +
c2.getRadius()

+ " and area is now " + String.format("%.3f", c2.getArea()));

You might also like