Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*************************************************************************************** public class Circle extends GeometricObject { private double radius; public Circle() { } public Circle(double radius) { this.radius = radius; } /** Return radius

image text in transcribedimage text in transcribedimage text in transcribed

***************************************************************************************

public class Circle extends GeometricObject { private double radius;

public Circle() { }

public Circle(double radius) { this.radius = radius; }

/** Return radius */ public double getRadius() { return radius; }

/** Set a new radius */ public void setRadius(double radius) { this.radius = radius; }

@Override /** Return area */ public double getArea() { return radius * radius * Math.PI; }

/** Return diameter */ public double getDiameter() { return 2 * radius; }

@Override /** Return perimeter */ public double getPerimeter() { return 2 * radius * Math.PI; }

/* Print the circle info */ public void printCircle() { System.out.println("The circle is created " + getDateCreated() + " and the radius is " + radius); } @Override public String toString() { // Implement it to return the three sides return "Circle: radius = " + radius; } }

*********************************************************************************

public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;

/** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); }

/** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }

/** Return color */ public String getColor() { return color; }

/** Set a new color */ public void setColor(String color) { this.color = color; }

/** Return filled. Since filled is boolean, * the get method is named isFilled */ public boolean isFilled() { return filled; }

/** Set a new filled */ public void setFilled(boolean filled) { this.filled = filled; }

/** Get dateCreated */ public java.util.Date getDateCreated() { return dateCreated; }

@Override public String toString() { return "created on " + dateCreated + " color: " + color + " and filled: " + filled; }

/** Abstract method getArea */ public abstract double getArea();

/** Abstract method getPerimeter */ public abstract double getPerimeter(); }

********************************************************************************

public class Rectangle extends GeometricObject { private double width; private double height;

public Rectangle() { }

public Rectangle(double width, double height) { this.width = width; this.height = height; }

/** Return width */ public double getWidth() { return width; }

/** Set a new width */ public void setWidth(double width) { this.width = width; }

/** Return height */ public double getHeight() { return height; }

/** Set a new height */ public void setHeight(double height) { this.height = height; }

@Override /** Return area */ public double getArea() { return width * height; }

@Override /** Return perimeter */ public double getPerimeter() { return 2 * (width + height); }

@Override public String toString() { // Implement it to return the three sides return "Rectangle: width = " + width + ", height = " + height; } }

************************************************************************

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.stage.FileChooser;

import javafx.stage.Stage;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.ComboBox;

import javafx.scene.control.Label;

import javafx.scene.control.RadioButton;

import javafx.scene.control.TextArea;

import javafx.scene.control.TextField;

import javafx.scene.control.ToggleGroup;

import javafx.scene.layout.GridPane;

import javafx.scene.layout.HBox;

import javafx.scene.layout.Pane;

import javafx.scene.layout.VBox;

import javafx.scene.text.Font;

import javafx.scene.text.FontWeight;

public class ShapeGenerator extends Application {

protected TextField txfValue1, txfValue2, txfValue3;

protected TextArea txaMessage;

protected ArrayList shapes = new ArrayList();

protected ComboBox cmbShape;

protected Label lbl1, lbl2, lbl3;

protected Button btnCreateShape, btnShowAll;

protected ToggleGroup tGrpShapeChoice = new ToggleGroup();

private Button btnReadShape;

private Button btnClearAll;

private Button btnWriteShape;

private final String PATH = "src\\prob1";

@Override

public void start(Stage primaryStage) {

Pane grdRootPane = buildGuiPane(primaryStage);

Scene scene = new Scene(grdRootPane, 400, 500);

primaryStage.setScene(scene);

primaryStage.setTitle("HW 6 - Problem 1");

primaryStage.show();

}

private Pane buildGuiPane(Stage stage) {

Pane topRow = buildTopRow();

Pane buttonRow = buildButtonRow(stage);

Pane messageRow = buildMessageRow();

// Main gui is a VBox, put the three rows in.

VBox pane = new VBox();

pane.setPadding(new Insets(10,10,10,10));

pane.setSpacing(10);

pane.getChildren().addAll(topRow, buttonRow, messageRow );

return pane;

}

private Pane buildTopRow() {

// Put shape choice radio buttons in a VBox

VBox vBoxShape = new VBox();

vBoxShape.setSpacing(10);

Label lblSelectShape = new Label("Select Shape");

lblSelectShape.setFont(Font.font(null, FontWeight.BOLD, 16));

vBoxShape.getChildren().add(lblSelectShape);

ShapeChoiceEventHandler cmbShapeEventHandler = new ShapeChoiceEventHandler();

RadioButton rbTriangle = new RadioButton("Triangle");

rbTriangle.setToggleGroup(tGrpShapeChoice);

rbTriangle.setSelected(true);

rbTriangle.setOnAction(cmbShapeEventHandler);

vBoxShape.getChildren().add(rbTriangle);

RadioButton rbRectangle = new RadioButton("Rectangle");

rbRectangle.setToggleGroup(tGrpShapeChoice);

rbRectangle.setOnAction(cmbShapeEventHandler);

vBoxShape.getChildren().add(rbRectangle);

RadioButton rbCircle = new RadioButton("Circle");

rbCircle.setToggleGroup(tGrpShapeChoice);

rbCircle.setOnAction(cmbShapeEventHandler);

vBoxShape.getChildren().add(rbCircle);

// Put shape length entry fields in a GridPane

GridPane gridShapeLengths = new GridPane();

gridShapeLengths.setVgap(5);

Label lblEnterValues = new Label("Enter Values");

lblEnterValues.setFont(Font.font(null, FontWeight.BOLD, 16));

gridShapeLengths.add(lblEnterValues, 0, 0);

lbl1 = new Label("Side 1:");

gridShapeLengths.add(lbl1, 0, 1);

txfValue1 = new TextField();

gridShapeLengths.add(txfValue1, 1, 1);

lbl2 = new Label("Side 2:");

gridShapeLengths.add(lbl2, 0, 2);

txfValue2 = new TextField();

gridShapeLengths.add(txfValue2, 1, 2);

lbl3 = new Label("Side 3:");

gridShapeLengths.add(lbl3, 0, 3);

txfValue3 = new TextField();

gridShapeLengths.add(txfValue3, 1, 3);

// Build top row of gui as HBox

// Put radio buttons VBox and entry field GridPane in an HBox

HBox topRow = new HBox();

topRow.setAlignment(Pos.TOP_LEFT);

topRow.setSpacing(20);

topRow.getChildren().addAll(vBoxShape, gridShapeLengths);

return topRow;

}

private Pane buildButtonRow(Stage stage) {

// First button row

HBox hBoxButtons = new HBox();

hBoxButtons.setSpacing(5.0);

hBoxButtons.setAlignment(Pos.CENTER);

btnCreateShape = new Button("Create Triangle");

hBoxButtons.getChildren().add(btnCreateShape);

CreateShapeEventHandler btnEventHandler = new CreateShapeEventHandler();

btnCreateShape.setOnAction(btnEventHandler );

btnShowAll = new Button("Show All");

hBoxButtons.getChildren().add(btnShowAll);

ShowAllEventHandler btnShowAllEventHandler = new ShowAllEventHandler();

btnShowAll.setOnAction(btnShowAllEventHandler );

btnClearAll = new Button("Clear All");

hBoxButtons.getChildren().add(btnClearAll);

btnClearAll.setOnAction( e -> { shapes.clear(); txaMessage.setText(""); } );

// Second button row

HBox hBoxButtons2 = new HBox();

hBoxButtons2.setSpacing(5.0);

hBoxButtons2.setAlignment(Pos.CENTER);

btnReadShape = new Button("Read Shapes");

btnReadShape.setOnAction( new ReadShapesEventHandler(stage));

hBoxButtons2.getChildren().add(btnReadShape);

btnWriteShape = new Button("Write Shapes");

btnWriteShape.setOnAction( new WriteShapesEventHandler(stage));

hBoxButtons2.getChildren().add(btnWriteShape);

VBox buttonRow = new VBox();

buttonRow.setSpacing(10);

buttonRow.getChildren().addAll(hBoxButtons, hBoxButtons2);

return buttonRow;

}

private Pane buildMessageRow() {

// Put message area in HBox. This is third row of gui.

HBox hBoxMessage = new HBox();

hBoxMessage.setAlignment(Pos.CENTER);

txaMessage = new TextArea();

txaMessage.setEditable(false);

txaMessage.setPrefColumnCount(30);

txaMessage.setPrefRowCount(15);

hBoxMessage.getChildren().add(txaMessage);

return hBoxMessage;

}

private class CreateShapeEventHandler implements EventHandler {

@Override

public void handle(ActionEvent event) {

RadioButton rad = (RadioButton)tGrpShapeChoice.getSelectedToggle();

String strShape = rad.getText();

GeometricObject shape = null;

switch( strShape ) {

case "Triangle":

System.out.println("Creating a Triangle");

// Get triangle side lengths

double side1 = Double.parseDouble( txfValue1.getText() );

double side2 = Double.parseDouble( txfValue2.getText() );

double side3 = Double.parseDouble( txfValue3.getText() );

// Create triangle object

shape = new Triangle(side1, side2, side3);

break;

case "Rectangle":

System.out.println("Creating a Rectangle");

// Get rectangle width and height

double width = Double.parseDouble( txfValue1.getText() );

double height = Double.parseDouble( txfValue2.getText() );

// Create triangle object

shape = new Rectangle(width, height);

break;

case "Circle":

System.out.println("Creating a Circle");

// Get rectangle width and height

double radius = Double.parseDouble( txfValue1.getText() );

// Create triangle object

shape = new Circle(radius);

break;

}

// Add to collection

shapes.add(shape);

// Build message to display

StringBuilder message = new StringBuilder();

message.append("Shape: ");

message.append( shape.toString() + " " );

String temp = String.format(" Area = %4.1f ", shape.getArea());

message.append(temp);

temp = String.format(" Perimeter = %4.1f ", shape.getPerimeter());

message.append(temp);

// Put message in display area

txaMessage.setText(message.toString());

// Erase fields

txfValue1.setText(null);

txfValue2.setText(null);

txfValue3.setText(null);

}

}

private class ShowAllEventHandler implements EventHandler {

@Override

public void handle(ActionEvent event) {

StringBuilder message = new StringBuilder();

message.append("All Shapes: ");

int i=1;

for( GeometricObject shape : shapes ) {

message.append((i++) + ". " + shape + " ");

}

txaMessage.setText(message.toString());

// Erase fields

txfValue1.setText(null);

txfValue2.setText(null);

txfValue3.setText(null);

}

}

private class ShapeChoiceEventHandler implements EventHandler {

@Override

public void handle(ActionEvent event) {

RadioButton rb = (RadioButton)event.getSource();

String shape = rb.getText();

switch( shape ) {

case "Triangle":

System.out.println("Triangle");

showTriangleEntry();

break;

case "Rectangle":

System.out.println("Rectangle");

showRectangleEntry();

break;

case "Circle":

System.out.println("Circle");

showCircleEntry();

break;

}

}

}

private class ReadShapesEventHandler implements EventHandler {

Stage stage;

public ReadShapesEventHandler(Stage stage) {

super();

this.stage = stage;

}

@Override

public void handle(ActionEvent event) {

File file = getInFile(stage);

if( file != null) {

try {

readShapesFile(file);

txaMessage.setText("-->readShapesFile() called successfully ");

txaMessage.appendText("-->You need to write the code in the readShapesFile method");

}

catch (FileNotFoundException e) {

txaMessage.setText("Error reading file");

e.printStackTrace();

}

}

}

}

private class WriteShapesEventHandler implements EventHandler {

Stage stage;

public WriteShapesEventHandler(Stage stage) {

super();

this.stage = stage;

}

@Override

public void handle(ActionEvent event) {

File file = getOutFile(stage);

if( file != null) {

try {

writeShapesFile(file);

txaMessage.setText("-->writeShapesFile() called successfully ");

txaMessage.appendText("-->You need to write the code in the writeShapesFile method ");

}

catch (FileNotFoundException e) {

txaMessage.setText("Error writing file");

e.printStackTrace();

}

}

}

}

private File getInFile(Stage stage) {

FileChooser fileChooser = new FileChooser();

FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");

fileChooser.getExtensionFilters().add(extFilter);

File initPath = new File(PATH);

fileChooser.setInitialDirectory(initPath);

File file = fileChooser.showOpenDialog(stage);

System.out.println(file);

return file;

}

private File getOutFile(Stage stage) {

FileChooser fileChooser = new FileChooser();

FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");

fileChooser.getExtensionFilters().add(extFilter);

File initPath = new File(PATH);

fileChooser.setInitialDirectory(initPath);

File file = fileChooser.showSaveDialog(stage);

System.out.println(file);

return file;

}

private void readShapesFile(File file) throws FileNotFoundException {

// This method should read shapes from "file" in the format

// shown in the problem statement, build the corresponding

// shape objects and add them to the shapes ArrayList

}

private void writeShapesFile(File file) throws FileNotFoundException {

// This method should write the shapes in the shapes ArrayList

// to the "file" that was input.

}

private void showTriangleEntry() {

lbl1.setVisible(true);

lbl1.setText("Side 1:");

txfValue1.setVisible(true);

lbl2.setVisible(true);

lbl2.setText("Side 2:");

txfValue2.setVisible(true);

lbl3.setVisible(true);

lbl2.setText("Side 3:");

txfValue3.setVisible(true);

btnCreateShape.setText("Create Triangle");

}

private void showRectangleEntry() {

lbl1.setVisible(true);

lbl1.setText("Width:");

txfValue1.setVisible(true);

lbl2.setVisible(true);

lbl2.setText("Height:");

txfValue2.setVisible(true);

lbl3.setVisible(false);

txfValue3.setVisible(false);

btnCreateShape.setText("Create Rectangle");

}

private void showCircleEntry() {

lbl1.setVisible(true);

lbl1.setText("Radius:");

txfValue1.setVisible(true);

lbl2.setVisible(false);

txfValue2.setVisible(false);

lbl3.setVisible(false);

txfValue3.setVisible(false);

btnCreateShape.setText("Create Circle");

}

public static void main(String[] args) {

launch(args);

}

}

************************************************************************

public class Triangle extends GeometricObject { private double side1 = 1.0, side2 = 1.0, side3 = 1.0;

/** Constructor */ public Triangle() { }

/** Constructor */ public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; } public double getSide1() { return side1; }

public double getSide2() { return side2; } public double getSide3() { return side3; } @Override /** Override method findArea in GeometricObject */ public double getArea() { double s = (side1 + side2 + side3) / 2; return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); }

@Override /** Override method findPerimeter in GeometricObject */ public double getPerimeter() { return side1 + side2 + side3; }

@Override public String toString() { // Implement it to return the three sides return "Triangle: side1 = " + side1 + ", side2 = " + side2 + ", side3 = " + side3; } }

*****************************************************************

shapes.txt

t 2 2 3 c 5 r 3 4 c 7 p dummyshape should be ignored r 4.5 7.57 t 14.3 22.4 5.6 c 8 c 3.5 what are you doing here? c 3.5

From the Package Explorer, open ShapeGenerator.java as shown above on the right and run the program. The Gui should appear as shown above. Create some shapes (Triangle, Rectangle, Circle) and use the other buttons that work: Show All and Clear All to get a feel how the program works. Try the Read Shapes button. A dialog will be displayed to allow you to select a file. Select the shapes.txt file and press "Open." The Gui will display a message implying that it has read the file, but it hasn't. You will write the method to read the file (directions follow).+ Try the Write Shapes button. A dialog will be displayed to allow you to create a file. Supply a file name and choose Save. The Gui will display a message implying that it has written the file, but it hasn't. You will write the method to write the file (directions follow).+ Close the Gui and open the Triangle class and verify that the constructor takes three arguments: the lengths of the three sides of the triangle. Do the same for the Rectangle class and the Circle class.+ Open shapes.text as shown on the right. Notice that the file has data for triangles, circles, and rectangles. And, there is also "junk" data in the 1 file. You will write code to read this file and I create the appropriate shapes (more later).+ | shapes.txt 23 t 2 2 3 2c 5 3r 3 4

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

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago