In: Computer Science
Fix the following codes in JAVA so they can work :
public class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject1() {
dateCreated = new java.util.Date();
}
public GeometricObject1(String Color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
}
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle1() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public Rectangle(double width, double height, String color,
boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
public class TestCircleRectangle {
public static void main(String[] args) {
Circle4 circle = new Circle4(1);
System.out.println("A circle " + circle.toString());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " +
circle.getDiameter());
Rectangle1 rectangle = new Rectangle(2, 4);
System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle1() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public Rectangle(double width, double height, String color,
boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
public class TestCircleRectangle {
public static void main(String[] args) {
Circle4 circle = new Circle4(1);
System.out.println("A circle " + circle.toString());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " +
circle.getDiameter());
Rectangle1 rectangle = new Rectangle(2, 4);
System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}
public class Circle4 extends GeometricObject {
private double radius;
public Circle4() {
}
public Circle4(double radius) {
super();
this.radius = radius;
}
public Circle4(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
//setColor(color);
//setFilled(filled);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
public double getDiameter() {
return 2 * radius;
}
public double getPerimeter() {
return 2 * radius * Math.PI;
}
public void printCircle() {
System.out.println(toString() + "The circle is created " +
getDateCreated() +
" and the radius is " + radius);
}
public String toString() {
return "Circle WWWW " + getColor() + super.toString();
}
}
The code is absolutely perfect, there is only a single mistake, you should know that in a single java file there could be only one public class and in your case, all your classes were public, also while defining the constructor of the class, the name of the constructor should be exactly same as the class name but in some classes, you have added "1" after the constructor name. Also, there are some classes that are defined two times in your code I have considered it only once. To correct the code I have only removed the "public" access specifier from all the classes and their constructors except for the one which has the "main" method, also corrected the name of the constructors where required. The modified code has been added below followed by the output.
Please note that to run the program don't forget to save this file(code) as "TestCircleRectangle.java" i.e,same as the name of the class which has public access specifier.
CODE:
class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
GeometricObject() {
dateCreated = new java.util.Date();
}
GeometricObject(String Color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
}
class Rectangle extends GeometricObject {
private double width;
private double height;
Rectangle() {
}
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
Rectangle(double width, double height, String color,
boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
class Circle4 extends GeometricObject {
private double radius;
Circle4() {
}
Circle4(double radius) {
super();
this.radius = radius;
}
Circle4(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
//setColor(color);
//setFilled(filled);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
public double getDiameter() {
return 2 * radius;
}
public double getPerimeter() {
return 2 * radius * Math.PI;
}
public void printCircle() {
System.out.println(toString() + "The circle is created " +
getDateCreated() +
" and the radius is " + radius);
}
public String toString() {
return "Circle WWWW " + getColor() + super.toString();
}
}
public class TestCircleRectangle {
public static void main(String[] args) {
Circle4 circle = new Circle4(1);
System.out.println("A circle " + circle.toString());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " + circle.getDiameter());
Rectangle rectangle = new Rectangle(2, 4);
System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}
OUTPUT:
A circle Circle WWWW whitecreated on Tue Oct 20 19:56:22 IST 2020
color: white and filled: false
The radius is 1.0
The area is 3.141592653589793
The diameter is 2.0
A rectangle created on Tue Oct 20 19:56:22 IST 2020
color: white and filled: false
The area is 8.0
The perimeter is 12.0
NOTE: If you have any query regarding the solution, please mention in the comment section. HAPPY LEARNING!!