Hey I need some help understanding a Java project. It will have to do with the f
ID: 3589647 • Letter: H
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 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:
Explanation / Answer
//GraphicObject.java
package graphicsproject;
import java.awt.Color;
public abstract class GraphicObject {
double x,y;
Color color;
public GraphicObject(double newX,double newY,int red,int green,int blue){
x=newX;
y=newY;
color=new Color(red,green,blue);
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public Color getColor() {
return color;
}
public String toString(){
return "x="+x+", y="+y+", red:"+color.getRed()+", green:"+color.getGreen()+", blue:"+color.getBlue()+" , class: "+getClass();
}
public abstract void draw();
public abstract double calculateArea();
public abstract double calculatePerimeter();
}
//Circle.java
package graphicsproject;
import java.awt.Canvas;
public class Circle extends GraphicObject {
double radius;
public Circle(double newX, double newY, int red, int green, int blue,double newRadius) {
super(newX, newY, red, green, blue);
radius=newRadius;
}
public void draw() {
StdDraw.setPenColor(super.getColor());
StdDraw.filledCircle(super.getX(), super.getY(), radius);
}
public double calculateArea() {
// TODO Auto-generated method stub
return Math.PI*(radius*radius);
}
public double calculatePerimeter() {
// TODO Auto-generated method stub
return 2*Math.PI*radius;
}
}
//Rectangle.java
package graphicsproject;
public class Rectangle extends GraphicObject {
double length,width;
public Rectangle(double newX, double newY, int red, int green, int blue,double newLength,double newWidth) {
super(newX, newY, red, green, blue);
length=newLength;
width=newWidth;
// TODO Auto-generated constructor stub
}
@Override
public void draw() {
// TODO Auto-generated method stub
StdDraw.setPenColor(super.getColor());
StdDraw.filledRectangle(super.getX(), super.getY(), width, length);
}
@Override
public double calculateArea() {
return 2*length*2*width;
}
@Override
public double calculatePerimeter() {
// TODO Auto-generated method stub
return 4*(length+width);
}
}
//Square.java
package graphicsproject;
public class Square extends GraphicObject{
double sideLength;
public Square(double newX, double newY, int red, int green, int blue,double sideLength) {
super(newX, newY, red, green, blue);
this.sideLength=sideLength;
// TODO Auto-generated constructor stub
}
@Override
public void draw() {
StdDraw.setPenColor(super.getColor());
StdDraw.filledSquare(super.getX(), super.getY(), sideLength);
// TODO Auto-generated method stub
}
@Override
public double calculateArea() {
return 4*sideLength*sideLength;
}
@Override
public double calculatePerimeter() {
return 8*sideLength;
}
}
//Ellipse.java
package graphicsproject;
public class Ellipse extends GraphicObject{
double semiMajorAxis,semiMinorAxis;
public Ellipse(double newX, double newY, int red, int green, int blue, double newSemiMajor, double newSemiMinor) {
super(newX, newY, red, green, blue);
semiMajorAxis=newSemiMajor;
semiMinorAxis=newSemiMinor;
// TODO Auto-generated constructor stub
}
@Override
public void draw() {
StdDraw.setPenColor(super.getColor());
StdDraw.filledEllipse(super.getX(), super.getY(), semiMajorAxis, semiMinorAxis);
// TODO Auto-generated method stub
}
@Override
public double calculateArea() {
// TODO Auto-generated method stub
return Math.PI*semiMajorAxis*semiMinorAxis;
}
@Override
public double calculatePerimeter() {
// TODO Auto-generated method stub
return 2*Math.PI*Math.sqrt((semiMajorAxis*semiMajorAxis + semiMinorAxis*semiMinorAxis)/2);
}
}
//ObjectOperations.java
package graphicsproject;
import java.util.Scanner;
public class ObjectOperations {
public static void fillArray(GraphicObject[] obj,Scanner filename){
try{
//int count=getCount(filename);
//System.out.println("count"+count);
//obj=new GraphicObject[20];
int i=0;
//filename.reset();
while(filename.hasNextLine()){
String row=filename.nextLine();
String[] splitted_row=row.split(" ");
System.out.println(splitted_row);
if(splitted_row[0].equals("C")){ //Circle
obj[i]=new Circle(Double.parseDouble(splitted_row[1]), Double.parseDouble(splitted_row[2]), Integer.parseInt(splitted_row[3]), Integer.parseInt(splitted_row[4]), Integer.parseInt(splitted_row[5]), Double.parseDouble(splitted_row[6]));
}else if(splitted_row[0].equals("R")){//Rectangle
obj[i]=new Rectangle(Double.parseDouble(splitted_row[1]), Double.parseDouble(splitted_row[2]), Integer.parseInt(splitted_row[3]), Integer.parseInt(splitted_row[4]), Integer.parseInt(splitted_row[5]), Double.parseDouble(splitted_row[6]),Double.parseDouble(splitted_row[7]));
}else if(splitted_row[0].equals("S")){ //Square
obj[i]=new Square(Double.parseDouble(splitted_row[1]), Double.parseDouble(splitted_row[2]), Integer.parseInt(splitted_row[3]), Integer.parseInt(splitted_row[4]), Integer.parseInt(splitted_row[5]), Double.parseDouble(splitted_row[6]));
}else if(splitted_row[0].equals("E")){ //Ellipse
obj[i]=new Ellipse(Double.parseDouble(splitted_row[1]), Double.parseDouble(splitted_row[2]), Integer.parseInt(splitted_row[3]), Integer.parseInt(splitted_row[4]), Integer.parseInt(splitted_row[5]), Double.parseDouble(splitted_row[6]),Double.parseDouble(splitted_row[7]));
}
i++;
}
System.out.println("exit while loop");
}catch (Exception e) {
System.out.println("Invalid data");
}
}
public static void drawList( GraphicObject[] objectList){
for (GraphicObject graphicObject : objectList) {
graphicObject.draw();
}
}
public static void printList( GraphicObject[] objectList){
for (GraphicObject graphicObject : objectList) {
graphicObject.toString();
}
}
}
//Tester.java
package graphicsproject;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Tester {
static GraphicObject[] objects = new GraphicObject[20];
public static void main(String args[]){
Scanner scanner=new Scanner(System.in);
System.out.println("Enter a filename");
String fname=scanner.next(); //reading filename
File file = new File(fname);
try {
StdDraw.setCanvasSize( 1000,1000); //initializing StdDraw canvas
StdDraw.setXscale(0, 1000);
StdDraw.setYscale(0, 1000);
StdDraw.setPenRadius(0.02);
Scanner sc = new Scanner(file);
ObjectOperations.fillArray(objects, sc);
ObjectOperations.drawList(objects);
ObjectOperations.printList(objects);
}
catch (FileNotFoundException e) { //entered filename is wrong
e.printStackTrace();
}
scanner.close();
}
}