Question

In: Computer Science

Fix the following codes in JAVA so they can work : public class GeometricObject { private...

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();
}
}

Solutions

Expert Solution

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!!


Related Solutions

Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
The following code is included for the java programming problem: public class Bunny {        private...
The following code is included for the java programming problem: public class Bunny {        private int bunnyNum;        public Bunny(int i) {               bunnyNum = i;        }        public void hop() {               System.out.println("Bunny " + bunnyNum + " hops");        } } Create an ArrayList <????> with Bunny as the generic type. Use an index for-loop to build (use .add(….) ) the Bunny ArrayList. From the output below, you need to have 5. Use an index for-loop...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements Stack<T> { private Node<T> top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); return top.info; } public T pop() throws StackException { Node<T> temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.getLink(); return temp.getInfo();...
This is Java class working on the eclipse. I include some of the codes just so...
This is Java class working on the eclipse. I include some of the codes just so you know what needed. create and work with interfaces you'll create the DepartmentConstants interface presented. In addition, you'll implement an interface named Displayable that's similar to the Printable interface Create the interfaces 1- Import the project named ch12-ex1_DisplayableTest and review the codes package murach.test; public interface Displayable {     String getDisplayText(); } 2 . Note that this code includes an interface named Displayable that...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public Fraction () {    numerator = 0;    denominator = 1;    numberOfFraction++; }    public Fraction (int n, int d) {    numerator = n;    denominator = d;    numberOfFraction++; } private int gcd (int num1, int num2) {    if (num1 == 0)    return num2;    return gcd (num2 % num1, num1); }    public Fraction add (Fraction third) {    int n = numerator * third.denominator + third.numerator * denominator;    int...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT