/* This program demostrates writing standalone classes in Java. * We are designing a class for a circle, that contains data about its radius and color. * We also write a method to find the area of the circle and one to print the required data. */ import java.util.Scanner; class Circle { //data attributes double radius; String color; static final double PI =3.14; //This variable counts the number of objects declared so far static int count =0; /* constructors * constructors are methods that are used to initialize the data attributes of an object. * They are always public, have the same name as the class and have no return type. * They are automatically invoked when the object is declared. */ //default constructor public Circle () { radius = 10; color ="Red"; count++; } //parameterized constuctor public Circle(double r, String c) { radius = r; color = c; count++; } /*accessors and mutators * Accessors, or get methods retrun the value of an attribute. * Mutators, or set methods change the value of an attribute. * They are one line public methods that contain the name of the attribute in their name. * There should be one of each per atribute. */ public double getRadius() { return radius; } public String getColor() { return color; } public void setRadius(double r) { radius = r; } public void setColor(String c) { color =c; } //This method prints all the pertinent data void print() { System.out.println("Radius: " +radius+"\nColor: "+color+"\nArea: "+findArea()); } //To calculate the area of the circle. double findArea() { return PI*radius*radius; } public static void main(String [] args) { Scanner input = new Scanner(System.in); //declare objects Circle c1 = new Circle(); //default constructor invoked Circle c2 = new Circle(12.5, "Green");//parameterized constructor invoked. //calling the print method // print is an instance method and should be called using the object c1.print(); c2.print(); System.out.println("Enter the radius :"); double r = input.nextDouble(); System.out.println("Enter the color:"); String col = input.next(); //change the values of c1's attributes to the user given values using the mutators c1.setRadius(r); c1.setColor(col); c1.print(); System.out.println("There are "+count+" objects"); } }