Question: Hey I need some help understanding a Java project. It will have to do with the following classes. It would be a big help if
Hey I need some help understanding a Java project. It will have to do with the following classes. It would be a big help if you can leave comments too.
In this assignment you will create a family of related graphical objects. You will create an abstract Graphic Object class which contains attributes common to all graphic object shapes. You will then create an array of graphic objects whose elements contain references to the subtypes of shapes.
GraphicObject class:
This is an abstract class that represents common features and operations that can be performed of different shapes.
Data members:
double x & double y: This is the x - y coordinate where the shape will be drawn on the drawing canvas. This represents the CENTER of the shape
Color color: This is the color that will be used to draw the shape to the screen, it will be an instance of class Color created by calling the constructor from class color which receives float values for the red, green, & blue components of the color.
Operations:
public GraphicObject (double newX, double newY, int red, int green, int blue) : This constructor will receive a value to initialize each of the data members. It will create an instance of class Color using the supplied red, blue, & green value.
public double getX(): returns the value of the x coordinate.
public double getY(): returns the value of the y coordinate.
public Color getColor(): returns the value of the color attribute.
public String toString(): returns a string containing the values of all data members. Color values should be specified as RGB values. (there are methods to get the red, blue, & green components of a color. These methods that are part of class Color). It must also print the class of the object by calling the method getClass() inherited from class Object
public abstract void draw(): abstract method that draws the shape to the drawing canvas. This must be overridden in the child classes.
public abstract double calculateArea(): abstract method that calculates and returns the area of the shape. This must be overridden in the child classes.
public abstract double calculatePerimeter(): abstract method that calculates and returns the perimeter of the shape. This must be overridden in the child classes.
Circle class:
This class represents circles, which must be defined as a subclass of class GraphicObject.
Data members:
double radius
Operations:
public Circle(double newX, double newY, int red, int green, int blue ,double newRadius): this method creates a new instance of the Circle class, and must call the constructor for class GraphicObject
public void draw(): Calls the method to print a filled circle from StdDraw, using the data members for the shape. It must call the method setPenColor in StdDraw to set the correct color to draw the desired shape.
public double calculateArea(): calculates the area of the shape
public double calculatePerimeter(): calculates the perimeter of the shape
Rectangle class:
This class represents rectangles, which must be defined as a subclass of class GraphicObject.
Data members:
double length : This represents 1/2 of the desired length of the rectangle
double width : This represents 1/2 of the desired width of the rectangle
Operations:
public Rectangle(double newX, double newY, int red, int green, int blue, double newLength, double newWidth): this method creates a new instance of the Rectangle class, and must call the constructor for class GraphicObject
public void draw(): Calls the method to print a filled rectangle from StdDraw, using the data members for the shape. It must call the method setPenColor in StdDraw to set the correct color to draw the desired shape.
public double calculateArea(): calculates the area of the shape
public double calculatePerimeter(): calculates the perimeter of the shape
Square class:
This class represents Squares, which must be defined as a subclass of class GraphicObject.
Data members:
double sideLength: represents half the length of one side of the square
Operations:
public Square(double newX, double newY, int red, int green, int blue, double sideLength): this method creates a new instance of the Square class, and must call the constructor for class GraphicObject
public void draw(): Calls the method to print a filled Square from StdDraw, using the data members for the shape. It must call the method setPenColor in StdDraw to set the correct color to draw the desired shape.
public double calculateArea(): calculates the area of the shape
public double calculatePerimeter(): calculates the perimeter of the shape
Ellipse class:
Data members:
double semiMajorAxis: this value is 1/2 the length of the major axis of the ellipse.
double semiMinorAxis: this value is 1/2 the length of the minor axis of the ellipse.
Operations:
public Ellipse(double newX, double newY, int red, int green, int blue, double newSemiMajor, double newSemiMinor): this method creates a new instance of the Ellipse class, and must call the constructor for class GraphicObject
public void draw(): Calls the method to print a filled ellipse from StdDraw, using the data members for the shape. It must call the method setPenColor in StdDraw to set the correct color to draw the desired shape.
public double calculateArea(): calculates the area of the shape
public double calculatePerimeter(): calculates the perimeter of the shape
ObjectOperations class:
This class will contain two methods which manipulate an array of GraphicObjects.
Operations:
public static void fillArray( GraphicObject[] objectList, Scanner filename)
This method reads a data file containing information about shapes to be drawn to the screen.
EACH LINE IN THE DATA FILE REPRESENTS A SINGLE SHAPE.
As each line is read the data input is used to create an instance of the correct shape that is inserted onto the end of the objectList array.
The format of each line varies based on the TYPE of shape being read.
General format: Each line begins with a single character representing the type of shape being drawn, followed by the X-Y coordinate of the center of the shape followed by the Red/Blue/Green values for the color of the shape, the remainder of the line consists of a series of values specific to the shape
Circle:
C X Y Red Blue Green Radius
Rectangle:
R X Y Red Blue Green Length Width
Square
S X Y Red Blue Green sideLength
Ellipse
E X Y Red Blue Green SemiMinorAxis SemiMajorAxis
public static void drawList( GraphicObject[] objectList)
This method iterates through the objectList and calls the draw method to draw each shape to the screen.
public static void printList( GraphicObject[] objectList)
This method iterates through the ObjectList and calls the toString method for each object.
Tester class:
Main Method: This method sets up the array of graphic objects and draws the shapes to the screen.
It will instantiate an array of GraphicObjects capable of containing 20 values.
It will set up the drawing canvas which StdDraw will use as follows:
// set the scale for the x/y axis
StdDraw.setCanvasSize( 1000,1000);
StdDraw.setXscale(0, 1000);
StdDraw.setYscale(0, 1000);
StdDraw.setPenRadius(0.02);
It will prompt the user for a name of a data file, open it within a try/catch block to validate the file name
Once the file opens correctly it will call the fillArray method to read the data file line by line and creating of each child class and inserting them into the array.
Once filled, the method drawList will be invoked to draw all of the shapes to the screen
Finally call the method printList
Here is the data file:
E 190 900 68 138 255 50 200 S 10 590 100 255 218 100 R 625 700 224 64 251 75 200 R 100 700 174 234 0 123 85 C 175 175 0 255 0 76 R 250 50 93 64 55 90 45 C 300 250 255 0 0 100 R 400 280 239 108 0 75 20 R 490 750 239 154 154 100 40 E 500 500 186 104 200 125 300 E 600 500 84 110 122 60 100 C 650 120 0 0 255 100 E 750 50 255 235 59 10 30 C 725 625 49 27 146 90 C 790 405 173 20 87 100 E 390 400 165 214 167 45 120 C 900 920 255 209 128 75 S 800 810 0 200 83 50 S 850 150 63 81 181 66 S 560 100 85 139 47 45
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
