1. Consider the following class declaration: public class Circle { private doubl
ID: 3530568 • Letter: 1
Question
1. Consider the following class declaration: public class Circle { private double radius; public Circle(double r) { radius = r; } public double getArea() { return Math.PI * radius * radius; } public double getRadius() { return radius; } } a. Write a toString method for this class. The method should return a string containing the radius and area of the circle. { String str; str = "Radius: " + radius + " Area: " + getArea(); } b. Write an equals method for this class. The method should accept a Circle object as an argument. It should return true if the argument object contains the same data as the calling object, or false otherwise. public (c) { boolean status; if (c.getRadius() == ) status = true; else status = false; return status; } c. Write a greaterThan method for this class. The method should accept a Circle object as an argument. It should return true if the argument object has an area that is greater than the area of the calling object, or false otherwise. public boolean greaterThan(Circle c) { boolean status; if () status = true; else status = false; return status; } 2. Consider the following class declaration: public class Thing { private int x; private int y; private static int z = 0; public Thing() { x = z; y = z; } static void putThing(int a) { z = a; } } Assume a program containing the class declaration defines three Thing objects with the following statements: Thing Thing(); Thing two = new Thing(); Thing three = new Thing(); a. How many separate instances of the x member exist? b. How many separate instances of the y member exist? c. How many separate instances of the z member exist? d. What value will be stored in the x and y members of each object? e. Write a statement that will call the putThing method. (5); 3. Assuming the following enum declaration exists: enum Dog { POODLE, BOXER, TERRIER } what will the following statements display? a. System.out.println(Dog.POODLE + " " + Dog.BOXER + " " + Dog.TERRIER); b. System.out.println(Dog.POODLE.ordinal() + "Explanation / Answer
import java.awt.*;
public abstract class Shape {
// Instance variables
private int x;
private int y;
private Color color;
// Constructor
protected Shape(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
// Abstract methods
public abstract void draw(Graphics g);
public abstract int getHeight();
public abstract int getWidth();
public abstract void scale();
public abstract double area();
// Other instance methods
public Color getColor() {
return color;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void move(int dx, int dy) {
x += dx;
y += dy;
}
public void setColor(Color color) {
this.color = color;
}
public String toString() {
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
} }
Here are the class files:
// Represents a circle that can be displayed in a graphics
// context
import java.awt.*;
public class Circle extends Shape {
// Instance variables
private int diameter;
private double scaleFactor = 2;
private double area;
// Constructor
public Circle(int x, int y, Color color, int diameter) {
super(x, y, color);
this.diameter = diameter;
}
// Instance methods
public void draw(Graphics g) {
g.setColor(getColor());
g.fillOval(getX(), getY(), diameter, diameter);
}
public int getHeight() {
return diameter;
}
public int getWidth() {
return diameter;
}
public void scale(double scaleFactor) {
(double) diameter *= scaleFactor;
}
public double area() {
area = (3.14 * (diameter * diameter)) / 4;
return area;
}
public String toString() {
return "Diameter: " + diameter + " Width: " + getWidth() + "Height: " + getHeight();
}
}
import java.awt.*;
public class Rectangle extends Shape {
// Instance variables
private int width;
private int height;
private double scaleFactor = 2;
private double diameter = width * height;
private double area;
// Constructor
public Rectangle(int x, int y, Color color,
int width, int height) {
super(x, y, color);
this.width = width;
this.height = height;
}
// Instance methods
public void draw(Graphics g) {
g.setColor(getColor());
g.fillRect(getX(), getY(), width, height);
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public void scale(double scaleFactor){
width *= scaleFactor;
height *= scaleFactor;
}
public double area() {
area = (3.14 * (diameter * diameter)) / 4;
return area;
}
public String toString() {
return "Diameter: " + diameter + " Width: " + getWidth() + "Height: " +
getHeight();
}
}
And here is the test file:
public class ShapeTest
{
public static void main(String[] args)
{
Color myC = new Color(10, 30, 20);
Circle myCircle = new Circle(0, 0, myC, 5);
Rectangle myRectangle = new Rectangle(0, 0, myC, 2, 3);
System.out.println(myCircle.toString());
System.out.println(myRectangle.toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.