Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cse1102.geometry;
- public class GeometryApp {
- private static Recorder recorder = new Recorder("McNeil_Colin.txt");
- public static void main(String[] args)
- {
- recorder.writeln("Welcome to the geometric application",true);
- recorder.writeln("Interface Version.",true);
- printInstances();
- recorder.writeln("*****Create the shapes*****",true);
- Rectangle Rect1 = new Rectangle(10,10,10,5);
- Rect1.setLineColor("Green");
- Rect1.setFillColor("White");
- Rectangle Rect2 = new Rectangle(15,15,10,5);
- Rect2.setLineColor("White");
- Rect2.setFillColor("Green");
- printInstances();
- Triangle Tri1 = new Triangle(30,30,10,5,0);
- Tri1.setLineColor("Black");
- Tri1.setFillColor("Green");
- Triangle Tri2 = new Triangle(50,50,10,20,10);
- Tri2.setLineColor("Green");
- Tri2.setFillColor("Black");
- Triangle Tri3 = new Triangle(40,40,10,20,10);
- Tri3.setLineColor("Green");
- Tri3.setFillColor("Black");
- Circle cir1 = new Circle(100,100,4);
- cir1.setLineColor("Blue");
- cir1.setFillColor("Grey");
- printInstances();
- Square sqr1 = new Square(60,60,20);
- sqr1.setLineColor("Black");
- sqr1.setFillColor("Orange");
- printInstances();
- if (Tri1.equals(Tri2)){
- recorder.writeln("Triangle 1 = Triangle 2", true);
- }
- else{
- recorder.writeln("Triangle 1 != Triangle 2", true);
- }
- if (Tri2.equals(Tri3)){
- recorder.writeln("Triangle 2 = Triangle 3", true);
- }
- else{
- recorder.writeln("Triangle 2 != Triangle 3", true);
- }
- if (Tri1.equals(Tri3)){
- recorder.writeln("Triangle 1 = Triangle 3", true);
- }
- else{
- recorder.writeln("Triangle 1 != Triangle 3", true);
- }
- recorder.writeln("*****Display descriptions of the shapes*****",true);
- Definable d1 = Rect1;
- recorder.writeln("~~~Rectangle 1~~~",true);
- recorder.writeln(d1,true);
- d1 = Rect2;
- recorder.writeln("~~~Rectangle 2~~~",true);
- recorder.writeln(d1,true);
- d1 = Tri1;
- recorder.writeln("~~~Triangle 1~~~",true);
- recorder.writeln(d1,true);
- d1 = Tri2;
- recorder.writeln("~~~Triangle 2~~~",true);
- recorder.writeln(d1,true);
- d1= Tri3;
- recorder.writeln("~~~Triangle 3~~~",true);
- recorder.writeln(d1,true);
- d1 = sqr1;
- recorder.writeln("~~~Square 1~~~",true);
- recorder.writeln(d1,true);
- recorder.writeln("~~~Circle 1~~~",true);
- recorder.writeln(cir1,true);
- recorder.writeln("*****Move the shapes*****",true);
- Rect1.deltaMove(10, 5);
- Rect2.deltaMove(10, 5);
- Tri1.deltaMove(10, 5);
- Tri2.deltaMove(10, 5);
- cir1.deltaMove(10, 5);
- sqr1.deltaMove(10, 5);
- recorder.writeln("*****Display the descriptions of the shapes (casts)*****",true);
- d1= (Definable) (Rect1);
- recorder.writeln("~~~Rectangle 1~~~",true);
- recorder.writeln(d1,true);
- d1= (Definable) (Rect2);
- recorder.writeln("~~~Rectangle 2~~~",true);
- recorder.writeln(d1,true);
- d1= (Definable) (Tri1);
- recorder.writeln("~~~Triangle 1~~~",true);
- recorder.writeln(d1,true);
- d1= (Definable) (Tri2);
- recorder.writeln("~~~Triangle 2~~~",true);
- recorder.writeln(d1,true);
- d1=(Definable) (Tri3);
- recorder.writeln("~~~Triangle 3~~~",true);
- recorder.writeln(d1,true);
- d1= (Definable) (sqr1);
- recorder.writeln("~~~Square~~~",true);
- recorder.writeln(d1,true);
- recorder.writeln("~~~Circle~~~",true);
- recorder.writeln(cir1,true);
- recorder.writeln("*****Display the area and perimeter of the shapes*****",true);
- ShapesUser su = (ShapesUser)(Rect1);
- recorder.writeln("Rectangle 1 perimeter: "+su.getPerimeter() +" Area: "+su.getArea(),true);
- su = (ShapesUser)(Rect2);
- recorder.writeln("Rectangle 2 perimeter: "+su.getPerimeter() +" Area: "+su.getArea(),true);
- su = (ShapesUser)(Tri1);
- recorder.writeln("Triangle 1 perimeter: "+su.getPerimeter() +" Area: "+su.getArea(),true);
- su = (ShapesUser)(Tri2);
- recorder.writeln("Triangle 2 perimeter: "+su.getPerimeter() +" Area: "+su.getArea(),true);
- su = (ShapesUser)(Tri3);
- recorder.writeln("Triangle 3 perimeter: "+su.getPerimeter() +" Area: "+su.getArea(),true);
- su = (ShapesUser)(cir1);
- recorder.writeln("Circle perimeter: "+su.getPerimeter() +" Area: "+su.getArea(),true);
- su = (ShapesUser) (sqr1);
- recorder.writeln("Square perimeter: "+su.getPerimeter() +" Area: "+su.getArea(),true);
- recorder.close();
- }
- public static void printInstances(){
- recorder.writeln("# of rectangles: "+Rectangle.getObjectCount(),true);
- recorder.writeln("# of triangles: "+Triangle.getObjectCount(),true);
- recorder.writeln("# of circles: "+Circle.getObjectCount(),true);
- recorder.writeln("# of squares: "+Square.getObjectCount(),true);
- recorder.writeln("********************",true);
- }
- }
- --Definable--
- package cse1102.geometry;
- public interface Definable {
- String getObjectDefinition();
- }
- --ShapesUser--
- package cse1102.geometry;
- public interface ShapesUser {
- public abstract void deltaMove(int deltaX,int deltaY);
- public abstract Double getArea();
- public abstract Double getPerimeter();
- }
- --Shape--
- package cse1102.geometry;
- public class Shape {
- private String type;
- private String lineColor;
- private String fillColor;
- public void Shape(){
- this.type="";
- this.lineColor="";
- this.fillColor="";
- }
- public String toString(){
- String message = this.type +"\n" +
- "lineColor = " + this.lineColor + "\n" +
- "fillColor = " + this.fillColor + "\n";
- return message;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getLineColor() {
- return lineColor;
- }
- public void setLineColor(String lineColor) {
- this.lineColor = lineColor;
- }
- public String getFillColor() {
- return fillColor;
- }
- public void setFillColor(String fillColor) {
- this.fillColor = fillColor;
- }
- }
- --Rectangle--
- package cse1102.geometry;
- public class Rectangle extends Shape implements Definable, ShapesUser{
- private Vertex vertex1;
- private Vertex vertex2;
- private Vertex vertex3;
- private Vertex vertex4;
- private int length;
- private int width;
- private static int instances = 0;
- public Rectangle(){
- super();
- instances++;
- super.setType("Rectangle");
- }
- public Rectangle(int originX, int originY, int length, int width){
- this();
- this.vertex1=new Vertex(originX, originY);
- this.vertex2=new Vertex(originX,originY+width);
- this.vertex3=new Vertex(originX+length,originY+width);
- this.vertex4=new Vertex(originX+length,originY);
- this.length=length;
- this.width=width;
- }
- @Override
- public void deltaMove(int deltaX, int deltaY){
- vertex1.deltaMove(deltaX, deltaY);
- vertex2.deltaMove(deltaX, deltaY);
- vertex3.deltaMove(deltaX, deltaY);
- vertex4.deltaMove(deltaX, deltaY);
- }
- @Override
- public Double getArea(){
- return (double) (length*width);
- }
- @Override
- public Double getPerimeter(){
- return (double) (2*length+2*width);
- }
- public static int getObjectCount(){
- return instances;
- }
- public int getLength() {
- return length;
- }
- public int getWidth() {
- return width;
- }
- public Vertex getVertex1() {
- return vertex1;
- }
- public Vertex getVertex2() {
- return vertex2;
- }
- public Vertex getVertex3() {
- return vertex3;
- }
- public Vertex getVertex4() {
- return vertex4;
- }
- public String getObjectDefinition() {
- String message = super.toString() + "\n" +
- "origin = " + this.vertex1.getX() + " , " + this.vertex1.getY() + "\n" +
- "vertex 2 x,y = " + this.vertex2.getX() + " , " + this.vertex2.getY() + "\n" +
- "vertex 3 x,y = " + this.vertex3.getX() + " , " + this.vertex3.getY() + "\n" +
- "vertex 4 x,y = " + this.vertex4.getX() + " , " + this.vertex4.getY() + "\n" +
- "width = " + this.width + "\n" +
- "length = " + this.length + "\n";
- return message;
- }
- }
- --Triangle--
- package cse1102.geometry;
- public class Triangle extends Shape implements ShapesUser, Definable{
- private static int instances = 0;
- private Vertex vertex1;
- private Vertex vertex2;
- private Vertex vertex3;
- private int base;
- private int offset;
- private int height;
- public Triangle(){
- super();
- instances++;
- super.setType("Triangle");
- }
- public Triangle(int originX, int originY, int height, int base, int offset){
- this();
- this.vertex1=new Vertex(originX, originY);
- this.vertex2=new Vertex(originX+offset,originY+height);
- this.vertex3=new Vertex(originX+base,originY);
- this.base=base;
- this.offset=offset;
- this.height=height;
- }
- public Vertex getVertex1() {
- return vertex1;
- }
- public Vertex getVertex2() {
- return vertex2;
- }
- public Vertex getVertex3() {
- return vertex3;
- }
- public int getBase() {
- return base;
- }
- public int getOffset() {
- return offset;
- }
- public int getHeight() {
- return height;
- }
- @Override
- public void deltaMove(int deltaX, int deltaY){
- vertex1.deltaMove(deltaX, deltaY);
- vertex2.deltaMove(deltaX, deltaY);
- vertex3.deltaMove(deltaX, deltaY);
- }
- @Override
- public Double getArea(){
- return (double) (base*height/2);
- }
- @Override
- public Double getPerimeter(){
- double side1 = Math.sqrt(Math.pow(offset,2)+Math.pow(height,2));
- double side2 = Math.sqrt(Math.pow(base-offset,2)+Math.pow(height,2));
- return (base+side1+side2);
- }
- public boolean equals(Triangle t2){
- boolean tests = false;
- if (this.base==t2.base) {
- if (this.offset==t2.offset){
- if(this.height==t2.height){
- tests=true;
- }
- }
- }
- else{
- tests=false;
- }
- return tests;
- }
- public static int getObjectCount() {
- return instances;
- }
- public String getObjectDefinition() {
- String message = super.toString() + "\n" +
- "origin = " + this.vertex1.getX() + " , " + this.vertex1.getY() + "\n" +
- "vertex 2 x,y = " + this.vertex2.getX() + " , " + this.vertex2.getY() + "\n" +
- "vertex 3 x,y = " + this.vertex3.getX() + " , " + this.vertex3.getY() + "\n" +
- "height = " + this.height + "\n" +
- "base = " + this.base + "\n" +
- "offset = " + this.offset + "\n";
- return message;
- }
- }
- --Circle--
- package cse1102.geometry;
- public class Circle extends Shape implements ShapesUser, Definable{
- private static int instances=0;
- private Vertex vertex1;
- private double radius;
- public Circle(){
- super();
- instances++;
- super.setType("Circle");
- }
- public Circle(int originX, int originY, double radius){
- this();
- this.vertex1 = new Vertex(originX,originY);
- this.radius=radius;
- }
- public void deltaMove(int deltaX, int deltaY){
- vertex1.deltaMove(deltaX, deltaY);
- }
- @Override
- public Double getArea(){
- Double area = (Math.sqrt(this.radius)*Math.PI);
- return area;
- }
- @Override
- public Double getPerimeter(){
- double perimeter = (this.radius*2*Math.PI);
- return perimeter;
- }
- public static int getObjectCount(){
- return instances;
- }
- public double getRadius() {
- return radius;
- }
- public void setRadius(double radius) {
- this.radius = radius;
- }
- public Vertex getVertex1() {
- return vertex1;
- }
- public String getObjectDefinition() {
- String message = super.toString() +
- "origin = " + this.vertex1.getX() + " , " + this.vertex1.getY() + "\n" +
- "radius = " + this.radius + "\n" ;
- return message;
- }
- }
- --Square--
- package cse1102.geometry;
- public class Square extends Shape implements ShapesUser, Definable{
- private static int instances=0;
- private Vertex vertex1;
- private Vertex vertex2;
- private Vertex vertex3;
- private Vertex vertex4;
- private int length;
- public Square(){
- super();
- instances++;
- super.setType("Square");
- }
- public Square(int originX, int originY, int length){
- this();
- this.vertex1=new Vertex(originX, originY);
- this.vertex2=new Vertex(originX,originY+length);
- this.vertex3=new Vertex(originX+length,originY+length);
- this.vertex4=new Vertex(originX+length,originY);
- this.length=length;
- }
- @Override
- public void deltaMove(int deltaX, int deltaY){
- vertex1.deltaMove(deltaX, deltaY);
- vertex2.deltaMove(deltaX, deltaY);
- vertex3.deltaMove(deltaX, deltaY);
- vertex4.deltaMove(deltaX, deltaY);
- }
- @Override
- public Double getArea(){
- return (Math.pow(length,2));
- }
- @Override
- public Double getPerimeter(){
- return (double) (4*length);
- }
- public static int getObjectCount(){
- return instances;
- }
- public int getLength() {
- return length;
- }
- public int getWidth() {
- return length;
- }
- public Vertex getVertex1() {
- return vertex1;
- }
- public Vertex getVertex2() {
- return vertex2;
- }
- public Vertex getVertex3() {
- return vertex3;
- }
- public Vertex getVertex4() {
- return vertex4;
- }
- public String getObjectDefinition() {
- String message = super.toString() + "\n" +
- "origin = " + this.vertex1.getX() + " , " + this.vertex1.getY() + "\n" +
- "vertex 2 x,y = " + this.vertex2.getX() + " , " + this.vertex2.getY() + "\n" +
- "vertex 3 x,y = " + this.vertex3.getX() + " , " + this.vertex3.getY() + "\n" +
- "vertex 4 x,y = " + this.vertex4.getX() + " , " + this.vertex4.getY() + "\n" +
- "width = " + this.length + "\n" +
- "length = " + this.length + "\n";
- return message;
- }
- }
- --Vertex--
- package cse1102.geometry;
- public class Vertex {
- private int x;
- private int y;
- public Vertex(){
- this.x=0;
- this.y=0;
- }
- public Vertex(int x, int y){
- this.x=x;
- this.y=y;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public void deltaMove(int deltaX, int deltaY){
- this.x+=deltaX;
- this.y+=deltaY;
- }
- public String toString(){
- String message = "Type = Vertex" + "\n" +
- "X coordinate = " + this.x + "\n" +
- "Y coordinate = " + this.y + "\n";
- return message;
- }
- }
- --Recorder--
- // Program Name: Recorder
- // Purpose: Write a string to a file and optionally to the console
- // Revision History
- // Revision Date Author Comment
- // 1.0 2/26/13 W. Beacham Initial Release
- //
- /*
- * How to use this class.
- * 1. Call the constructor with the name of the file as the argument. Omit the path,
- * just supply the file name and extension. The file will be created in the same
- * directory that contains the project src directory in your workspace.
- * 2. The write() method writes the supplied string to the file without adding a
- * final "\n". Similar to System.out.print() method. If the second argument = true,
- * the same content is written to the console.
- * 3. The writeln() method writes the supplied string to the file and also adds a
- * final "\n". Similar to System.out.println() method. If the second argument = true,
- * the same content is written to the console.
- * 4. The close() method closes the file and ensure that any buffered content is written. Using
- * this method is required for stable operation.
- */
- package cse1102.geometry;
- /******************************************************
- * The <code>Recorder</code> class represents a recorder that
- * is used to print strings or object descriptions to a
- * data file and, if so directed, to the console window.
- *
- * How to use this class.
- * 1. Call the constructor with the name of the file as the argument. Omit the path,
- * just supply the file name and extension. The file will be created in the same
- * directory that contains the project src directory in your workspace.
- * 2. The write() method writes the supplied string to the file without adding a
- * final "\n". Similar to System.out.print() method. If the second argument = true,
- * the same content is written to the console.
- * 3. The writeln() method writes the supplied string to the file and also adds a
- * final "\n". Similar to System.out.println() method. If the second argument = true,
- * the same content is written to the console.
- * 4. The close() method closes the file and ensure that any buffered content is written. Using
- * this method is required for stable operation.
- *
- * @author W.Beacham
- * @version 2.0
- */
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- public class Recorder {
- /*******************************************************
- * Instance variables
- ********************************************************/
- private String filename;
- private PrintWriter pw;
- private boolean successfulCreation;
- /******************************************************
- * Creates a recorder object
- * @param filename the name of the file to store data. See
- * project directory
- *******************************************************/
- public Recorder(String filename){
- this.filename = filename;
- try{
- FileWriter fw = new FileWriter(this.filename);
- BufferedWriter bw = new BufferedWriter(fw, 8192);
- pw = new PrintWriter(bw);
- }
- catch (IOException e){
- System.out.println(e);
- }
- }
- //getter for the success flag
- /*********************************************************
- * Returns a <code>Boolean</code> that indicates if the file
- * was successfully created
- * @return True if file successfully created
- **********************************************************/
- public boolean isSuccessfulCreation() {
- return successfulCreation;
- }
- //regular method to write content w/o a final new line
- /***********************************************************
- * Writes a string to the data file and console window.
- * @param content String of data to store
- * @param console If set to true, write to the console also
- ***********************************************************/
- public void write(String content, boolean console){
- pw.write(content);
- if (console) {
- System.out.print(content);
- }
- }
- //regular method to write content with a final new line
- /*************************************************************
- * Writes a string with a trailing \n to the data file and
- * console window.
- * @param content String of data to store
- * @param console If set to true, write to the console also
- *************************************************************/
- public void writeln(String content, boolean console){
- pw.write(content + "\n");
- if (console) {
- System.out.println(content);
- }
- }
- //regular method to the content of a Shape
- /***************************************************************
- * Invokes the toString method of the shape that is passed in to
- * print the shape description with a trailing \n to the data file
- * and console window.
- * @param definable Object of type definable or a sub class
- * @param console if set to true, write to the console also
- ***************************************************************/
- public void writeln(Definable definable, boolean console){
- pw.write(definable.getObjectDefinition() + "\n");
- if (console) {
- System.out.println(definable.getObjectDefinition());
- }
- }
- public void writeObject(Definable d){
- System.out.println(d.getObjectDefinition());
- }
- //regular method to print the content of a Shape
- /****************************************************************
- * Invokes the toString method of the shape that is passed in to
- * print the shape description with a trailing \n to the console
- * window only.
- * @param shape Object of tyope shape or a sub class
- ****************************************************************/
- public void writeln(Shape shape){
- System.out.println(shape.toString());
- }
- //regular method to close the file
- /****************************************************************
- * Closes out the data file. THis will force any remaining buffered
- * data to be written.
- ****************************************************************/
- public void close(){
- pw.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment