Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the class ReadShapeFile, there is a file called shapes.txt. Ive opened it on line 2 This has a list of shapes. Youre going to

In the class ReadShapeFile, there is a file called shapes.txt. Ive opened it on line 2 This has a list of shapes. Youre going to read each line for each shape and call createShape, which will create one of the available shapes, Circle, Rectangle, or Square and returns a GeometricObject, which you will add to the ArrayList called shapeList.

The file has some unavailable shapes. If the shape is unavailable, createShape should throw a ShapeException. You should catch a ShapeException and continue reading the file.

By the end, the side of shapeList should be 20.

Summary:

  • Create a ShapeException class
  • Implement createShape to return the appropriate shape depending on the string shapeName
  • createShape should throw a ShapeException if it is not a Circle, Square, or Rectangle
  • A loop should read in the shapes.txt file lineby-line
  • If the file cannot be read, you should break out of the loop
  • If you get a ShapeException, you should continue reading the file

GeometricObjects.java

import java.util.Date;

public class GeometricObject {

private static int next_id = 0;

private int id;

private String color;

private boolean filled;

private Date dateCreated;

public GeometricObject() {

this.color = "green";

this.filled = true;

this.dateCreated = new Date();

this.id = next_id;

next_id++;

}

public GeometricObject(String color, boolean filled) {

this();

this.color = color;

this.filled = filled;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

protected boolean isFilled() {

return this.filled;

}

public void setFilled(boolean filled) {

this.filled = filled;

}

public Date getDateCreated() {

return this.dateCreated;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String toString() {

return super.toString() + ": GeometricObject[color=" + this.color + ", filled=" + this.filled + "]";

}

public static void main (String[] args) {

GeometricObject s1 = new GeometricObject();

System.out.println(s1.toString());

s1.setColor("brown");

s1.setFilled(false);

System.out.println(s1.toString());

System.out.println("s1 color is " + s1.getColor());

System.out.println("s1 filled is " + s1.isFilled());

GeometricObject s2 = new GeometricObject("purple", true);

System.out.println(s2.toString());

}

}

Circle.java

public class Circle extends GeometricObject {

private double radius = 1.0;

public Circle() {

this.radius = 1.0;

}

public Circle(double radius) {

this.radius = radius;

}

public Circle(double radius, String color, boolean filled) {

super(color, filled);

this.radius = radius;

}

public double getRadius() {

return this.radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

public double getArea() {

return Math.PI*this.radius*this.radius;

}

public double getPerimeter() {

return Math.PI*2*this.radius;

}

public double getDiameter() {

return 2*this.radius;

}

public String toString() {

// the output of "A Circle with radius=xxx, which is a subclass of yyy",

// where yyy is the output of the toString() method from the superclass

// is also acceptable

return "Circle[" + super.toString() + ", radius=" + this.radius + "]";

}

}

Rectangle.java

public class Rectangle extends GeometricObject {

private double width = 1.0;

private double length = 1.0;

public Rectangle() {

this.width = 1.0;

this.length = 1.0;

}

public Rectangle(double width, double length) {

this.width = width;

this.length = length;

}

public Rectangle(double width, double length,

String color, boolean filled) {

super(color,filled);

this.width = width;

this.length = length;

}

public double getWidth() {

return this.width;

}

public void setWidth(double width) {

this.width = width;

}

public double getLength() {

return length;

}

public void setLength(double length) {

this.length = length;

}

public double getArea() {

return this.width * this.length;

}

public double getPerimeter() {

return 2*this.width + 2*this.length;

}

public String toString() {

// the output of "A Rectangle with width=xxx and length=zzz, which is a subclass of yyy"

// where yyy is the output of the toString() method from the superclass

// is also acceptable

return "Rectangle[" + super.toString() +

",width=" + this.width + ",length=" + this.length + "]";

}

public static void main(String[] args) {

Rectangle rect1 = new Rectangle();

System.out.println(rect1.toString());

Rectangle rect2 = new Rectangle(2,3);

System.out.println("rect2 length = "+ rect2.getLength());

System.out.println("rect2 width = "+ rect2.getWidth());

System.out.println("rect2 area = "+ rect2.getArea());

System.out.println("rect2 perimeter = "+ rect2.getPerimeter());

System.out.println(rect2);

Rectangle rect3 = new Rectangle(3,4, "blue", false);

System.out.println(rect3);

}

}

Square.java

public class Square extends Rectangle {

public Square() {

super(1.0,1.0);

}

public Square(double side) {

super(side, side);

}

public Square(double side, String color, boolean filled) {

super(side, side, color, filled);

}

public void setSide(double side) {

super.setLength(side);

super.setWidth(side);

}

public double getSide() {

return this.getLength();

}

public String toString() {

//"A Square with side=xxx, which is a subclass of yyy",

// where yyy is the output of the toString() method from the superclass.

// is also acceptable

return "Square[" + super.toString() + "]";

}

/* override these to prevent any code from violating the square geometry */

public void setWidth(double width) {

this.setSide(width);

}

public void setLength(double length) {

this.setSide(length);

}

public static void main(String[] args) {

Square sq1 = new Square();

System.out.println("sq1: " + sq1);

Square sq2 = new Square(5);

System.out.println("sq2 side = " + sq2.getSide());

System.out.println("sq2 area = " + sq2.getArea());

System.out.println("sq2 perim = " + sq2.getPerimeter());

System.out.println(sq2);

sq2.setSide(8);

System.out.println(sq2);

sq2.setWidth(7);

System.out.println(sq2);

sq2.setWidth(6);

System.out.println(sq2);

Square sq3 = new Square(3, "gray", false);

System.out.println("sq3: " + sq3);

}

}

ReadShapeFiles.java

import java.io.*;

import java.util.ArrayList;

import shapes.*;

/* your tasks:

* create a class called ShapeException

* createShape should throw a ShapeException

* in main(), you should catch the ShapeException

*

*/

public class ReadShapeFile {

public static GeometricObject createShape(String shapeName) {

/* if shapeName is "Circle" return Circle();

* if shapeName is "Square" return Square();

* if shapeName is "Rectangle" return Rectangle();

* if it is not any one of these, it should throw

* a ShapeException

*/

return null;

}

public static void main(String[] args) {

ArrayList shapeList = new ArrayList();

File f = new File("shapes.txt");

String inString = null;

/* create a loop to read the file line-by-line */

try {

GeometricObject shape = createShape(inString);

} catch (/* your exception */ ) {

System.err.println("Cannot create shape: " + inString);

}

System.out.println(shapeList);

}

}

Shape.txt

Circle Circle Rectangle Circle Circle Circle Circle Rhombus Circle Square Square Square Circle Ellipse Rectangle Rectangle Square Square Circle Square Circle Rectangle

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions

Question

Please refer to the image below

Answered: 1 week ago