Please help me out with this program. Create a class named Point that will inclu
ID: 3671656 • Letter: P
Question
Please help me out with this program.
Create a class named Point that will include 2 int class variables x, y to represent a point. Create all possible constructors, set and get methods, and the toString method.
Create a class named Circle that will inherit Point. Circle will include 1 int class variables radius to represent the radius of the circle. Create all possible constructors, set and get methods, and the toString method. Include the area method and the circumference similar to the Circle class we did in class.
Write a Java program to test the above classes.
Explanation / Answer
/**
* @author Srinivas Palli point is in 2-Dimensional space
*
*/
public class Point {
/**
* x and y represents a (x,y) coordinates in 2-Dimensional space
*/
private int x, y;
/**
* default constructor initiallizes x and y to 0
*/
public Point() {
this.x = 0;
this.y = 0;
}
/**
* @param x
* @param y
*/
public Point(int x, int y) {
this.x = x;
this.y = y;
}
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @param x
* the x to set
*/
public void setX(int x) {
this.x = x;
}
/**
* @return the y
*/
public int getY() {
return y;
}
/**
* @param y
* the y to set
*/
public void setY(int y) {
this.y = y;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}
/**
* @author Srinivas Palli
*
*/
public class Circle extends Point {
int radius;
/**
* @param x
* @param y
* @param radius
*/
public Circle(int x, int y, int radius) {
super(x, y);
this.radius = radius;
// TODO Auto-generated constructor stub
}
/**
* @return
*/
public double calculateArea() {
double area = Math.PI * (radius * radius);
return area;
}
/**
* @return
*/
public double calculatePerimeter() {
double perimeter = 2 * 3.14 * radius;
return perimeter;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Circle [radius=" + radius + ", getX()=" + getX() + ", getY()="
+ getY() + "]";
}
}
/**
* @author Srinivas Palli
*
*/
public class TestCircle {
public static void main(String[] args) {
try {
Circle circle = new Circle(10, 12, 2);
System.out.println("Area:" + circle.calculateArea());
System.out.println("Perimeter:" + circle.calculatePerimeter());
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
Area:12.566370614359172
Perimeter:12.56