Write a program to create a class Circles with the following information:
radius in integer.
Add two constructors, (a) default constructor, and (b) constructor to pass on radius of a Circle.
Add a method printData( ) to print the radius of the Circle in console.
Add methods printArea( ) and printPerimeter( ) to compute and print the area and perimeter of Circle in console.
Create two objects of this class, c1 and c2. Show the output of both the constructors and all methods of these two objects
class Circles {
private int radius;
private int breadth;
/**
* (a) default constructor
*/
public Circles() {
}
/**
* onstructor to pass on radius of a Circle.
*
* @param radius
*/
public Circles(int radius) {
this.radius = radius;
}
/**
* Add a method printData( ) to print the radius of the Circle in console.
*/
public void printData() {
System.out.println("Radius: " + radius);
}
public void printArea() {
double area = Math.PI * radius * radius;
System.out.println("Area: " + area);
}
/**
* print the perimeter of Circle in console.
*/
public void printPerimeter() {
double perimeter = 2 * Math.PI * radius;
System.out.println("Perimeter: " + perimeter);
}
}
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
// Create two objects of this class, c1 and c2. Show the output of both the
// constructors and all methods of these two objects
Circles с1 = new Circles();
Circles с2 = new Circles(4);
с1.printData();
с1.printArea();
с1.printPerimeter();
с2.printData();
с2.printArea();
с2.printPerimeter();
}
}
Comments
Leave a comment