1.This is using Java. Write a method called makeCircle that returns a new Circle
ID: 3721511 • Letter: 1
Question
1.This is using Java. Write a method called makeCircle that returns a new Circle object based on the method's parameters: two integer values representing the (x, y) coordinates of the center of the circle, an integer representing the circles radius, and a Color object that defines the circle's fill color.
2.overload the makeCircle method such that if the Color parameter is not provided, the circle's color will default to red.
3.overload the makeCircle method such that if the radius is not provided, a random radius in the range in the range 10 to 20.
4.overload the makeCircle method such that if both the color and radius are not provided, the color wll default to green and the radius will default to 40.
Explanation / Answer
Circle.java
public class Circle {
//Declaring instance variables
private int x;
private int y;
private int radius;
private String color;
//Parameterized constructor
public Circle(int x, int y, int radius, String color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
// 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 int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Circle [x=" + x + ", y=" + y + ", radius=" + radius
+ ", color=" + color + "]";
}
}
_________________
Demo.java
import java.util.Random;
public class Demo {
public static void main(String[] args) {
//calling methods
Circle c1=makeCircle(2,3,5,"Yellow");
Circle c2=makeCircle(5,6,5);
Circle c3=makeCircle(-2,4,"Blue");
Circle c4=makeCircle(4,1);
//Displaying the Circles Info
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
}
private static Circle makeCircle(int x, int y) {
return new Circle(x, y,40,"green");
}
private static Circle makeCircle(int x, int y, String color) {
//Creating a random Class object
Random r = new Random();
int radius=r.nextInt(11) + 10;
return new Circle(x, y, radius, color);
}
private static Circle makeCircle(int x, int y, int radius) {
return new Circle(x,y,radius,"red");
}
private static Circle makeCircle(int x, int y, int radius, String color) {
return new Circle(x,y,radius,color);
}
}
_________________
Output:
Circle [x=2, y=3, radius=5, color=Yellow]
Circle [x=5, y=6, radius=5, color=red]
Circle [x=-2, y=4, radius=17, color=Blue]
Circle [x=4, y=1, radius=40, color=green]
___________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.