This assignment is on object oriented programming. You will be developing several classes that are fundamental in Geometry - Point, Circle, and Rectangle. You will then write a test class that will test some of the methods that you have written.
Here is the skeleton of the code that you will be writing. You will have to write the bodies of all the methods that I have listed. You may always add more methods.
import java.util.*; import java.io.*; class Point { // list of attributes - x and y coordinates private double x; private double y; // default constructor public Point () // non-default constructor public Point (double x, double y) // accessors get the x and y coordinates public double getX () public double getY () // mutators set the x and y coordinates public void setX (double x) public void setY (double y) // distance to another point public double distance (Point p) // string representation of a point, i.e. x and y coordinates public String toString () // test for equality of two points public boolean equals (Point p) } class Circle { // list of attributes private double radius; private Point center; // default constructor (radius = 1.0 and center at the origin) public Circle () // non-default constructors public Circle (double radius, double x, double y) public Circle (double radius, Point p) // accessors public double getRadius () public Point getCenter () // mutators public void setRadius (double radius) public void setCenter (double x, double y) public void setCenter (Point p) // calculate circumference public double circumference () // calculate area public double area () // determine if Point p is strictly inside this Circle public boolean isInside (Point p) // determine if Circle c is strictly inside this Circle public boolean isInside (Circle c) // determine if Rectangle r is strictly inside this Circle public boolean isInside (Rectangle r) // determine if Circle c intersects this Circle public boolean doesIntersect (Circle c) // determine if Rectangle r intersects this Circle public boolean doesIntersect (Rectangle r) // determine the Rectangle that inscribes this Circle public Rectangle inscribes () // string representation of the Circle in the form: // Radius: 1.2, Center: (3, 4) public String toString () // determine if two Circles have the same radius public boolean equals (Circle c) // determine if two Circles have the same center public boolean isConcentric (Circle c) } class Rectangle { // list attributes private Point UL; // upper left corner private Point LR; // lower right corner // default constructor creates a rectangle having // corners UL (0, 1) and LR (1, 0) public Rectangle () // non-default constructors accept user defined points // and creates a rectangle object if the points form a // rectangle (x1 < x1 and y1 > y2) or // the default rectangle if they do not. public Rectangle (Point ul, Point lr) public Rectangle (double x1, double y1, double x2, double y2) // accessors public Point getUL () public Point getLR () // get length - absolute value of the x coord difference public double getLength () // get width - absolute value of the y coord difference public double getWidth () // mutators reset the edge points only if the rectangle // shape is preserved (x1 < x2 and y1 > y2) public void setUL (Point ul) public void setLR (Point lr) public void setUL (double x1, double y1) public void setLR (double x2, double y2) public void setRectangle (double x1, double y1, double x2, double y2) // calculate area of a rectangle public double area () // calculate the perimeter of a rectangle public double perimeter () // determine if a point is strictly inside the rectangle public boolean isInside (Point p) // determine if the Circle c is strictly inside the rectangle public boolean isInside (Cricle c) // determine if the Rectangle r is strictly inside this Rectangle public boolean isInside (Rectangle r) // determine if Rectangle r intersects with this Rectangle public boolean doesIntersect (Rectangle r) // determine the Circle that circumscribes this Rectangle public Circle circumscribes () // return a string representation of a rectangle of the form // UL: (3, 4), LR: (7, 2) public String toString () // determine if two rectangles are congruent, i.e. the // length of one is equal to the length of the other // width of one is equal to the width of the other public boolean equals (Rectangle r) // determine if a rectangle is a square public boolean isSquare () } public class Geometry { public static void main (String[] args) throws IOException { // open file "geometry.txt" for reading // read the coordinates of the first Point pointP // read the coordinates of the second Point pointQ // print distance between pointP and pointQ // read the radius and coordinates of the first Circle circleA // read the radius and coordinates of the second Circle circleB // print the circumference of circleA // print the area of circleB // print if pointP is inside circleA or not // print if circleB is inside circleA or not // print if circleA and circleB intersect or not // print the Rectangle that circleB is inscribed in // read the coordinates of the UL and LR corners of Rectangle rectG // read the coordinates of the UL and LR corners of Rectangle rectH // print the length and width of rectG // print if the rectG and rectH have the same area but different perimeter or not // print if rectH is a square or not // print the Circle that circumscribes rectH // determine if pointP is inside rectG or not // determine if circleB is inside rectH or not // determine if rectH is inside rectG or not // determine if rectG and rectH intersect or not // close file "geometry.txt" } }
Note that in this program you will be checking for equality of two floating point numbers. Since there is a finite precision in the representation of floating point numbers, it is not always possible to determine exact equality. A working solution to determine equality is to take the difference of the floating point numbers and see if the difference is less than a pre-determined limit. This limit is arbitrary and is often dictated by the problem that you are trying to solve. Here is a method that tests for equality of two doubles.
public boolean isEqual (double x, double y) { double delta = 1.0e-18; // an arbitrary small number return (((Math.abs (x - y)) < delta); }
You will be reading your input from the file geometry.txt. The format of the file will be exactly like this:
3.0 2.0 // coordinates of pointP 7.0 3.0 // coordinates of pointQ 1.0 4.0 3.0 // dimensions of circleA 3.0 5.0 6.0 // dimensions of circleB 1.0 4.0 8.0 2.0 // dimensions of rectG 2.0 5.0 6.0 1.0 // dimensions of rectHThe structure of your output will be as follows:
Distance between pointP and pointQ: Circumference of circleA: Area of circleB: pointP (is / is not) inside circleA circleB (is / is not) inside circleA circleA and circleB (does / does not) intersect Coordinates of rectangle that inscribes circleB: Length and width of rectG: rectG and rectH (do / do not) have the same area but different perimeter rectH (is / is not) a square Coordinates of circle that circumscribes rectH: pointP (is / is not) inside rectG circleB (is / is not) inside rectH rectH (is / is not) inside rectG rectG and rectH (do / do not) intersect
We will be looking at good documentation, design, and adherence to the coding convention mentioned below. You may use the same variable names used in the problem statement or come up with your own. Your code must have the following header:
/* File: Geometry.java Description: Student Name: Student UT EID: Course Name: CS 312 Unique Numbers: Date Created: Date Last Modified: */
You will follow the standard Java Coding Conventions. You can either view the HTML page or download the PDF or Postscript and print it out. There is a modification that I would like to make to the standard coding conventions. Please align the opening and closing braces vertically so that you can easily make out the blocks of code. For example:
Do this: if ( x > 5 ) { a = b + c; } Not this: if ( x > 5 ) { a = b + c; }
Use the Canvas program to submit your .java file. We should receive your work by 11 PM on Sunday 07 August 2016. There will be substantial penalties if you do not adhere to the guidelines.