this.x-x this.y-y this.radius -radius return radius; public String toString) ret
ID: 3756221 • Letter: T
Question
this.x-x this.y-y this.radius -radius return radius; public String toString) return "Center:+x.y +Radius:"+radius public boolean equals(object o) If (o instanceof Circle) //Colored interface public double area) ( Circle other (Circle) o return x other.x && y public Interface Colored return Nath.PI radius radius public String getcolor) othery 8& radius-sother.radlus else public double perineter) //circle class return false return 2"Rath.Pl radlus public class Circle private int x private Int y private int radius public CircleO public int getx) return x Write ColoredCircle class so that implements the Co ored interface and extends Circle so that Circles have colors Override tostring method to print out the cen ter, radius and color of the Circle, override equals method so that it compares color as well. And write the necessary constructors, accessors and mutators Write a client class and create objects of the Col oredcircle class. Print out the colored circles that you have created and compare if they are equal public int getyO return y public circle(int x, int y, int ra- dius) f public int getRadiusExplanation / Answer
ColoredCircle.java
//Extends from Circle and implements from Colored
public class ColoredCircle extends Circle implements Colored
{
//Color variable
String color;
//Default constructor which calls parent's default constructor
public ColoredCircle()
{
super();
color = "White";
}
//Parameterized constructor which calls parent's parameterized constructor
public ColoredCircle(int x, int y, int radius, String color)
{
super(x, y, radius);
this.color = color;
}
//Overrides toString() from Circle class
public String toString()
{
return super.toString() + " Color: " + color;
}
//Overrides equals() from Circle class
public boolean equals(Object o)
{
//First check for x, y and radius
if(super.equals(o))
{
//Then for color
ColoredCircle other = (ColoredCircle)o;
return color.equals(other.color);
}
//Return false if x, y and radius are not equal
else
return false;
}
//Implements getColor() of Colored interface
public String getColor()
{
return color;
}
}
Demo.java
//Driver class
public class Demo
{
public static void main(String[] args)
{
ColoredCircle circle1 = new ColoredCircle(0, 0, 2, "Red");
ColoredCircle circle2 = new ColoredCircle(0, 0, 2, "Red");
ColoredCircle circle3 = new ColoredCircle();
ColoredCircle circle4 = new ColoredCircle(0, 0, 2, "Yellow");
System.out.println(circle1);
System.out.println(circle2);
System.out.println(circle3);
System.out.println(circle4);
System.out.println("circle1 == circle2: " + circle1.equals(circle2));
System.out.println("circle1 == circle3: " + circle1.equals(circle3));
System.out.println("circle1 == circle4: " + circle1.equals(circle4));
System.out.println("circle3 == circle4: " + circle3.equals(circle4));
}
}
The interface Colored and class Circle remains intact. Output for the above driver class is provided below.
Center: (0, 0) Radius:2 Color: Red
Center: (0, 0) Radius:2 Color: Red
Center: (0, 0) Radius:0 Color: White
Center: (0, 0) Radius:2 Color: Yellow
circle1 == circle2: true
circle1 == circle3: false
circle1 == circle4: false
circle3 == circle4: false
Kindly give a thumbs up, if found useful. Comment for queries. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.