Starting from:

$20

Defines a Circle and a Rectangle and prints the area Solution

Write a java application that defines a Circle and a Rectangle and prints the area of each. Make use of encapsulation to defined class attributes and methods.

A circle has three attributes: center Point, radius, and area. Use a proper constructor to initialize Circle object. To initialize a circle, you will need a center point and a radius. Then, you can use the radius to calculate the area of a circle. The area of a circle is calculated using the following equation (PI = 3.14):



Area = PI * radius * radius



A Rectangle consists of four Points which are the upper left, upper right, lower left, and lower right corner points, length, height, and area. To initialize a rectangle, you will need the coordinates of all four corner points from which you can calculate the length, the height, and the area of a rectangle. The area of a rectangle is calculated using the following equation (Note that length is the distance between the upper left and upper right corner points and height is the distance between the upper left and lower left corner points):

Area = length * height



The Point class defines the X and Y coordinates of a point. The distance between two points is calculated using the following equation:



Distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)



For each class define displayInfo( ) method that would output information about an object. You can use the following main method to test your application. A sample output of the application is also shown below.



public class ShapeApp {

public static void main(String[] args) {

Point upperLeft = new Point(1, 5);

Point lowerLeft = new Point(1, 1);

Point upperRight = new Point(5, 5);

Point lowerRight = new Point(5, 1);

Rectangle r1 = new Rectangle(upperLeft, upperRight, lowerLeft,lowerRight);

r1.display();



Point center = new Point(10,10);

int radius = 4;

Circle c1 = new Circle(center, radius);

c1.display();

}

}





=================== SAMPLE OUTPUT =========================



Rectangle Info:

Upper Left Point (x,y) : 1,5

Upper Right Point (x,y): 5,5

Lower Left Point (x,y) : 1,1

Lower Right Point (x,y): 5,1

area : 16.0



Circle Info:

Center Point (x,y) : 10,10

Radius : 4

area : 50.24

More products