Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need this answer in java. Consider a graphics system with 3 classes-Shape,Circ

ID: 3820720 • Letter: I

Question

I need this answer in java.

Consider a graphics system with 3 classes-Shape,Circle and Rectangle-that you are to design and write. Have both Cicle and Rectangle inherit from Shape. In Shape, you should include the description, the x and y coordinates of the center point, a toString method, a move method, and an abstract area method. The constructor should only take the description in, then generate the x and y values randomly in the range 0 and 255. The move method takes in delta-x and a delta-y value and adds these to the x and y respectively. Since move works the same way in both child classes, make sure they cannot override it. The toString should display the description, and the x and y coordinate values.

In the Circle class add a radius value and implement the area method. The constructor should only recieve the radius and not permit a negative value(use the absolute value). Override the toString method to display everything the Shape toString displays plus the radius and area. Use the parent class toString to do most of the work.

In the Rectangle class add the length and width values and implement the area method. The constructor should only recieve the length and width and not permit negative values(use the absolute value). Override the toString method to display everything the shape toString displays plus the length, width, and area. Like the Circle toString, you should use the parent class to do most of the work.

a)Show the design of these classes using a UML diagram.

b)Implement your design in Java. Note that a test driver program is not required.

Explanation / Answer

package myProject;
import java.util.*;
//Abstract class Shape definition
abstract class Shape
{
   //Instance variables
   int x, y;
   //Random class object created to generate random numbers
   Random r = new Random();
   //Abstract method move
   abstract public void move(int x, int y);
   //Abstract method area
   abstract public double area();
   //Constructor for Shape class
   Shape()
   {
       //Generates random Number for x and y between 0 and 255
       this.x = r.nextInt(255 - 0) + 0;
       this.y = r.nextInt(255 - 0) + 0;
   }//End of constructor
   //Overrides toString() method
   public String toString()
   {
       return (" Coordinate X = " + x + " Coordinate Y = " + y);
   }//End of method
}//End of class Shape

//Class Circle derived from Shape
class Circle extends Shape
{
   double radius;
   //Constructor for Circle class
   Circle(double r)
   {
       radius = Math.abs(r);
   }//End of constructor
  
   //Overrides move method
   public void move(int deltaX, int deltaY)
   {
       x += deltaX;
       y += deltaY;
   }//End of method
  
   //Overrides area method
   public double area()
   {
       //Calculates area
       return (3.141 * radius * radius);
   }//End of method
   //Overrides toString() method
   public String toString()
   {
       //super.toString() Calls base class method
       return (super.toString() + " Radius = " + radius + " Area of Circle = " + Double.toString(area()));
   }//End of method
  
}//End of class Circle

//Class Rectangle derived from Shape
class Rectangle extends Shape
{
   double length, width;
   //Constructor for Rectangle class
   Rectangle(double l, double w)
   {
       length = Math.abs(l);
       width = Math.abs(w);
   }//End of constructor
  
   //Overrides move method
   public void move(int deltaX, int deltaY)
   {
       x += deltaX;
       y += deltaY;
   }//End of method
  
   //Overrides area method
   public double area()
   {
       //Calculates area
       return (length * width);
   }//End of method
  
   //Overrides toString() method
   public String toString()
   {
       //super.toString() Calls base class method
       return (super.toString() + " Length = " + length + " Width = " + width + " Area of Rectangle = " + Double.toString(area()));
   }//End of method
}//End of class Rectangle

//Driver class
public class ShpeTest
{
   //Main method
   public static void main(String ss[])
   {
       //Creates an object for Circle class
       Shape cir = new Circle(1.8);
       //Creates an object for Rectangle class
       Shape rec = new Rectangle(12.0, 10.0);
       System.out.println(cir);
       System.out.println(rec);
   }//End of main
}//End of class

Output:


Coordinate X = 38 Coordinate Y = 120
Radius = 1.8
Area of Circle = 10.17684

Coordinate X = 14 Coordinate Y = 121
Length = 12.0
Width = 10.0
Area of Rectangle = 120.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote