Defining Classes to Represent Points, Triangles, and Rectangles in the Plane A t
ID: 3680180 • Letter: D
Question
Defining Classes to Represent Points, Triangles, and Rectangles in the Plane
A triangle in two dimensions is defined by three two-dimensional vertices (points). This homework assignment is based upon first defining a Point class and then using it to create a Triangle class that represents triangles. A rectangle is defined by two points for the lower left corner and the upper right corner. You will also define the Rectangle class.
Designing the Classes:
Study the Javadoc documentation for the Triangle class and the Point class. This documentation provides the specification for these classes.
The constructor for the Point class will receive two doubles, representing the X and Y coordinates of the Point being constructed. Add a method to show (print) an instance of the Point class, i.e., the coordinates of a single point. This class will not need any other methods.
The constructor for the Triangle class will receive three Point objects representing the three vertices of the triangle. Note that the three arguments to the constructor can be in any order. This class should also be able to return the area, perimeter, and lengths of the three sides of the triangle. Your definition for the class must include the declaration of the variables in the class along with the constructor, the area method, the perimeter method, and three methods for the lengths of the sides. The area is defined as such:
Area = SQRT(s (s - a)(s - b)(s - c)),
where a, b, and c are the lengths of the three sides of the triangle
and s is the semiperimeter:
s = (a + b + c)/2
The constructor for the Rectangle class will receive two Point objects representing the lower left and upper right vertices of the triangle. This class should be able to return the area and perimeter of the rectangle. Your definition for the class must include the declaration of the variables in the class along with the constructor, the area method and the perimeter method. Since there is a choice in which points you use to construct a rectangle, make sure that you satisfy the following requirements:
1. The points used to construct the rectangle are identical to two of the points used in your triangle
2. The rectangle you construct has a non-zero area
Testing the Classes:
Write the code for the Point, Triangle, and Rectangle classes.
Write a class named Geometry that has a main method in which does the following:
reads six real (i.e., double) values from the console
instantiates three Point objects.
Uses all three points to instantiate a Triangle object
Uses two of the three points to instantiate a Rectangle object.
Print the coordinates of the three points used to define the triangle, the lengths of its sides, its area, its perimeter of the triangle.
Print the two Points used to define the rectangle, its area, and its perimeter.
--------------------------------------------------------------------------------------------------------
Example Dialog:
Enter the coordinates (x,y) of the first point: 1 1
Enter the coordinates (x,y) of the second point: 5 1
Enter the coordinates (x,y) of the third point: 1 5
Triangle
---------
Coordinates:
(1.0, 1.0)
(5.0, 1.0)
(1.0, 5.0)
Area: 8.0
Perimeter 13.657
Rectangle
-----------
Coordinates (two corners):
(5.0, 1.0)
(1.0, 5.0)
Area: 16.0
Perimeter 16.0
Generate the Javadoc for your code, using the method shown in class. Submit your source code and its Javadoc documentation (Point.html, Triangle.html, Rectangle.html and Geometry.html) to the dropbox
Explanation / Answer
Point.java
public class Point
{
double x;
double y;
Point(double x, double y)
{
this.x = x;
this.y = y;
}
public void printPoint()
{
System.out.println("Point : (" + x + "," + y + ")");
}
}
Triangle.java
import java.util.*;
public class Triangle
{
Point A,B,C;
public Triangle(Point A, Point B, Point C)
{
this.A = A;
this.B = B;
this.C = C;
}
public double getLength(Point X, Point Y)
{
double length = Math.sqrt((Math.pow((Y.x - X.x),2)) + (Math.pow((Y.y - X.y),2)));
return length;
}
public double getPerimeter(double l1, double l2, double l3)
{
double perimeter = l1 + l2 + l3;
return perimeter;
}
public double getArea(double l1, double l2, double l3)
{
double s = (l1 + l2 + l3)/2;
double area = Math.sqrt(s*(s - l1)*(s - l2)*(s - l3));
return area;
}
}
Rectangle.java
import java.util.*;
public class Geometry
{
public static void main(String args[])
{
double x1, x2, x3, y1, y2, y3;
System.out.println("Enter the coordinates of all points(Enter the points with single space (x1 y1 x2 y2 x3 y3");
Scanner scan = new Scanner(System.in);
String read = scan.nextLine();
String ss[] = read.split(" ");
x1 = Double.parseDouble(ss[0]);
y1 = Double.parseDouble(ss[1]);
x2 = Double.parseDouble(ss[2]);
y2 = Double.parseDouble(ss[3]);
x3 = Double.parseDouble(ss[4]);
y3 = Double.parseDouble(ss[5]);
Point X1 = new Point(x1,y1);
Point X2 = new Point(x2,y2);
Point X3 = new Point(x3,y3);
// Creation of Triangle -- start
Triangle t1 = new Triangle(X1,X2,X3);
System.out.println("THE COORDINATES OF TRIANGLE ARE : ");
X1.printPoint();
X2.printPoint();
X3.printPoint();
double len1 = t1.getLength(X1,X2);
double len2 = t1.getLength(X2,X3);
double len3 = t1.getLength(X1,X3);
System.out.println("LENGTH of SIDE 1 : " + len1 + " LENGTH of SIDE 2 : " + len2 + " LENGTH of SIDE 3 : " + len3);
double perimeter = t1.getPerimeter(len1,len2,len3);
System.out.println(" PERIMETER OF TRIANGLE IS : " + perimeter);
double area = t1.getArea(len1,len2,len3);
System.out.println(" AREA OF RECTANGLE IS : " + area);
//Creation of triangle -- end
//Creation of Rectangle -- start
Rectangle r1 = new Rectangle(X1,X2);
Point C = new Point(X1.x, X2.y); //AS it is a rectangle, the points lying on the same axis have same coordinates and the other coordinate value is the the distance of the parallel coordinate from the given pint
Point D = new Point(X2.x, X1.y);
System.out.println("THE COORDINATES OF RECTANGLE ARE : ");
System.out.println("THE 2 CORNERS ARE : ");
X1.printPoint();
X2.printPoint();
System.out.println("THE REMAINING COORDINATES OF RECTANGLE ARE : ");
C.printPoint();
D.printPoint();
double l1 = r1.getLength(X1, C);
double l2 = r1.getLength(X2, D);
double perimeter_rect = r1.getPerimeter(l1,l2);
System.out.println(" PERIMETER IS : " + perimeter_rect);
double area_rect = r1.getArea(l1,l2);
System.out.println(" AREA IS : " + area_rect);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.