In: Computer Science
2. Program containing two modules:
a. Create a class Code one programmer-written constructor
Create several instance data attributes
Create 3 methods which will each be invoked from the driver program
- one method will not receive any parameters, but will return a value to driver program
- one method will receive one parameter, and will also return a value to driver program
- a display method will receive 2 parameters from driver program
Define a constant properly; use the constant in a calculation
Generate getters and setters for each data attribute
b. Create a driver program
Instantiate objects using the programmer-written constructor
Call each of 3 methods, for every instantiated object, saving any values being returned
Call display method passing necessary values to the method that resides in the class
If you have any problem with the code feel free to comment.
Circle PART A
import java.awt.Point;
class Circle {
// constant variable
private static final double PI = 3.14;
// instance variables
private int x;
private int y;
private double radius;
// constructor
public Circle(int x, int y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
// calculate area of the circle
public double getArea() {
return PI * radius * radius;
}
// chekcing if a point is inside the circle
public boolean isPointInside(Point p) {
double xPart = (p.getX() - x) * (p.getX() - x);
double yPart = (p.getY() - y) * (p.getY() - y);
double radiusSquare = radius * radius;
if (xPart + yPart <= radiusSquare)
return true;
return false;
}
// displaying circle propertise
public void display(String color, String fillColor) {
System.out.println("Location: (" + x + ", " + y + ")");
System.out.println("Rdaius: " + radius + "units");
System.out.println("Color: " + color);
System.out.println("Fill Color: " + fillColor);
}
// ALL GETTERS AND SETTERS
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
Test PART B
import java.awt.Point;
//demo class for testing
public class Test {
public static void main(String[] args) {
Point p = new Point(2, 2);
Circle circle = new Circle(2, 3, 10);
System.out.println("Area of the circle: " + circle.getArea() + "units");
System.out.println("Is point(2, 2) inside the circle? " + circle.isPointInside(p));
System.out.println("\nCircle Stats:-");
circle.display("Black", "Orange");
}
}
Output