In: Computer Science
I need to update this program to follow the these requirements please and thank you :
Do not use packages
Every class but the main routine class must have a toString method . The toString method for Quadrilateral should display its fields: 4 Points.
All instance variables should be declared explicitly private .
Area incorrect : enter points no: 1 1 3 enter points no: 2 6 3 enter points no: 3 0 0 enter points no: 4 5 0 This is a Parallelogram Area is : 15.811
Type of quadrilateral incorrect : enter points no: 1 5 4 enter points no: 2 5 0 enter points no: 3 0 4 enter points no: 4 0 0 This is a Parallelogram Area is : 20.000
The assignment question is :
Write an inheritance hierarchy for the classes Quadrilateral (an abstract class), Trapezoid, Parallelogram, Rectangle, and Square and then code it up. Create and use a Point class that has instance variables: x-coordinate and y-coordinate (each quadrilateral will have 4 of these, declared in the Quadrilateral class as protected). Each class must be defined in a separate .java file.Write one or more constructors, get and set methods for each class, and another method that computes the area of the object of each class (except for Point and Quadrilateral). For simplicity require that one side of any CMPSC Assignments4quadrilateral lie along the x-axis, and any figure with parallel sides will have a parallel side lying along the x-axis; do NOT assume that the points have to entered in any particular order; reject any input that fails to meet these conditions. Test all of these methods.In a separate class write a main function that prompts the user for the 4 points. Hence there will be 7 classes: Point, Quadrilateral (abstract), Trapezoid, Parallelogram, Rectangle, Square, and a Driver (with the main routine).Extra credit: enhance the above to remove the simplifying requirement (NOT trivial!).
The program :
////// MAIN CLASS ////
package main;
import java.util.Scanner;
public class Main {
static double distSq(Point p, Point q)
{
return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
//Check whether for points forms rectangle or not.
static boolean isRectangle(Point p1, Point p2, Point p3, Point p4)
{
double d1 = distSq(p1, p2); // from p1 to p2
double d2 = distSq(p2, p3); // from p2 to p3
double d3 = distSq(p3, p4); // from p3 to p4
double d4 = distSq(p4, p1); // from p4 to p1
//Diagonal dist
double d5 = distSq(p1,p3);
//d5==d1+d2 ,then it divides the points into two right angle
triangle .Hence it is a rectangle
if(d5==d1+d2) {
return true;
}else {
return false;
}
}
// This function returns true if (p1, p2, p3, p4) form a
// square, otherwise false
static boolean isSquare(Point p1, Point p2, Point p3, Point
p4)
{
double d2 = distSq(p1, p2); // from p1 to p2
double d3 = distSq(p1, p3); // from p1 to p3
double d4 = distSq(p1, p4); // from p1 to p4
// If lengths if (p1, p2) and (p1, p3) are same, then
// following conditions must met to form a square.
// 1) Square of length of (p1, p4) is same as twice
// the square of (p1, p2)
// 2) Square of length of (p2, p3) is same
// as twice the square of (p2, p4)
if (d2 == d3 && 2 * d2 == d4
&& 2 * distSq(p2, p4) == distSq(p2, p3)) {
return true;
}
// The below two cases are similar to above case
if (d3 == d4 && 2 * d3 == d2
&& 2 * distSq(p3, p2) == distSq(p3, p4)) {
return true;
}
if (d2 == d4 && 2 * d2 == d3
&& 2 * distSq(p2, p3) == distSq(p2, p4)) {
return true;
}
return false;
}
static boolean isParallelogram(Point p1, Point p2, Point p3, Point
p4){
//To determine this first we calculate slope
double a = slope(p1,p2);
double b = slope(p3,p4);
double c = slope(p1,p4);
double d = slope(p2,p3);
double g = slope(p1,p3);
double h = slope(p2,p4);
//Test for parallelogram
if(a==b && (c==d || g==h)) {
return true;
}
return false;
}
static boolean isTrapezoid(Point p1, Point p2, Point p3, Point
p4){
//To determine this first we calculate slope
double a = slope(p1,p2);
double b = slope(p3,p4);
double c = slope(p1,p4);
double d = slope(p2,p3);
//Test for parallelogram
if(a==b || c==d) {
return true;
}
return false;
}
static private double slope(Point p1, Point p2) {
// TODO Auto-generated method stub
return (p2.y-p1.y)/(p2.x-p1.x);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Point[] arr = new Point[4] ;
Scanner sc = new Scanner(System.in);
for(int i=0;i<4;i++) {
System.out.printf("%s%n","enter points no: "+(i+1));
double first = sc.nextDouble();
double second = sc.nextDouble();
arr[i] = new Point(first,second);
}
if(isSquare(arr[0],arr[1],arr[2],arr[3])) {
System.out.printf("%s%n","This is a Square");
Square s = new Square(arr[0],arr[1],arr[2],arr[3]);
System.out.printf("Area is : %5.3f%n",s.area());
}
else if(isRectangle(arr[0],arr[1],arr[2],arr[3])) {
System.out.printf("%s%n","This is a Rectangle");
Rectangle r = new Rectangle(arr[0],arr[1],arr[2],arr[3]);
System.out.printf("Area is : %5.3f%n",r.area());
}
else if(isParallelogram(arr[0],arr[1],arr[2],arr[3])) {
System.out.printf("%s%n","This is a Parallelogram");
Parallelogram p = new
Parallelogram(arr[0],arr[1],arr[2],arr[3]);
System.out.printf("Area is : %5.3f%n",p.area());
}else if(isTrapezoid(arr[0],arr[1],arr[2],arr[3])) {
System.out.printf("%s%n","This is a Trapezoid");
Trapezoid t = new Trapezoid(arr[0],arr[1],arr[2],arr[3]);
System.out.printf("Area is : %5.3f%n",t.area());
}else {
System.out.printf("%s%n","Invalid Inputs");
}
}
}
//////////////////class Parallelogram//////////////
package main;
class Parallelogram extends Quadrilateral{
Point p1;
Point p2;
Point p3;
Point p4;
public Parallelogram(Point point, Point point2, Point point3,
Point point4) {
// TODO Auto-generated constructor stub
this.p1=point;
this.p2=point2;
this.p3=point3;
this.p4=point4;
}
double distSq(Point p, Point q)
{
return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
@Override
double area() {
// TODO Auto-generated method stub
double side = Math.sqrt(distSq(p1,p2));
double side2 = Math.sqrt(distSq(p1,p3));
return side*side2;
}
}
//////////////////class Point//////////////
package main;
public class Point {
// x represents x axis value and y represents y axis value
double x;
double y;
//Constructor to initialize data members
Point(double x, double y){ this.x=x; this.y=y; } }
//////////////////class Quadrilateral//////////////
package main;
abstract class Quadrilateral { abstract double area(); }
//////////////////class Rectangle//////////////
package main;
class Rectangle extends Quadrilateral{
Point p1;
Point p2;
Point p3;
Point p4;
public Rectangle(Point point, Point point2, Point point3, Point
point4) {
// TODO Auto-generated constructor stub
this.p1=point;
this.p2=point2;
this.p3=point3;
this.p4=point4;
}
double distSq(Point p, Point q)
{
return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
@Override
double area() {
// TODO Auto-generated method stub
double side = Math.sqrt(distSq(p1,p2));
double side2 = Math.sqrt(distSq(p1,p3));
return side*side2;
}
}
//////////////////class Square//////////////
package main;
public class Square extends Quadrilateral{
//Data members
Point p1;
Point p2;
Point p3;
Point p4;
//Constructor
Square(Point p1, Point p2, Point p3, Point p4){
this.p1=p1;
this.p2=p2;
this.p3=p3;
this.p4=p4;
}
double distSq(Point p, Point q)
{
return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
@Override
double area() {
// TODO Auto-generated method stub
double d1 = Math.sqrt(distSq(p1, p2)); // from p1 to p2
double d2 = Math.sqrt(distSq(p3, p4)); // from p1 to p3
return d1*d2;
}
}
////////////////////////////////Trapezoid///////////////
package main;
class Trapezoid extends Quadrilateral{
Point p1;
Point p2;
Point p3;
Point p4;
public Trapezoid(Point point, Point point2, Point point3, Point
point4) {
// TODO Auto-generated constructor stub
this.p1=point;
this.p2=point2;
this.p3=point3;
this.p4=point4;
}
double distSq(Point p, Point q)
{
return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y);
}
@Override
double area() {
// TODO Auto-generated method stub
return
0.5*Math.abs((p1.x*(p2.y-p3.y)+p2.x*(p3.y-p1.y)+p3.x*(p1.y-p2.y)))
+
0.5*Math.abs((p1.x*(p3.y-p4.y)+p3.x*(p4.y-p1.y)+p4.x*(p1.y-p3.y)));
}
}
/***************** Main.java ******************/
import java.util.Scanner;
public class Main {
private enum Quad {
SQUARE, RECTANGLE, TRAPEZOID,
PARALLELOGRAM, ERROR
}
static double distSq(Point p, Point q) {
return (p.getX() - q.getX()) *
(p.getX() - q.getX()) + (p.getY() - q.getY()) * (p.getY() -
q.getY());
}
private static double slope(Point p1, Point p2)
{
return (p2.getY() - p1.getY()) /
(p2.getX() - p1.getX());
}
private static Quad identifyQuadrilateral(Point p1, Point p2,
Point p3, Point p4) {
/* Checking Square or not */
double d2 = distSq(p1, p2); // from
p1 to p2
double d3 = distSq(p1, p3); // from
p1 to p3
double d4 = distSq(p1, p4); // from
p1 to p4
if (d2 == d3 && 2 * d2
== d4 && 2 * distSq(p2, p4) == distSq(p2, p3)) {
return
Quad.SQUARE;
}
// The below two cases are
similar to above case
if (d3 == d4 && 2 * d3 ==
d2 && 2 * distSq(p3, p2) == distSq(p3, p4)) {
return
Quad.SQUARE;
}
if (d2 == d4 && 2 * d2 ==
d3 && 2 * distSq(p2, p3) == distSq(p2, p4)) {
return
Quad.SQUARE;
}
/////////// Checking Rectangle or not
double r1 = distSq(p1, p2); //
from p1 to p2
double r2 = distSq(p2, p3); // from
p2 to p3
double r3 = distSq(p3, p4); // from
p3 to p4
double r4 = distSq(p4, p1); // from
p4 to p1
// Diagonal dist
double r5 = distSq(p1, p3);
// d5==d1+d2 ,then it divides
the points into two right angle triangle .Hence it
// is a rectangle
if (r5 == r1 + r2) {
return
Quad.RECTANGLE;
}
/////////// Checking
Parallelogram or not ///////
double a = slope(p1, p2);
double b = slope(p3, p4);
double c = slope(p1, p4);
double d = slope(p2, p3);
double g = slope(p1, p3);
double h = slope(p2, p4);
if (a == b && (c == d ||
g == h)) {
return
Quad.PARALLELOGRAM;
}
////////// Checking Trapezoid or
not ///////////
if (a == b || c == d) {
return
Quad.TRAPEZOID;
}
return Quad.ERROR;
}
public static void main(String[] args)
{
Point[] arr = new Point[4];
Scanner sc = new
Scanner(System.in);
for (int i = 0; i < 4; i++)
{
System.out.printf("%s%n", "enter points no: " + (i + 1));
double first =
sc.nextDouble();
double second =
sc.nextDouble();
arr[i] = new
Point(first, second);
}
Quad quad = identifyQuadrilateral(arr[0], arr[1], arr[2],
arr[3]);
switch (quad) {
case SQUARE:
Square s = new
Square(arr[0], arr[1], arr[2], arr[3]);
System.out.println(s.toString());
System.out.println("\n Area of the Square is " + s.area());
break;
case RECTANGLE:
Rectangle r =
new Rectangle(arr[0], arr[1], arr[2], arr[3]);
System.out.println(r.toString());
System.out.println("\n Area of the Rectangle is " +
r.area());
break;
case PARALLELOGRAM:
Parallelogram p
= new Parallelogram(arr[0], arr[1], arr[2], arr[3]);
System.out.println(p.toString());
System.out.println("\n Area of the Parallelogram is " +
p.area());
break;
case TRAPEZOID:
Trapezoid t =
new Trapezoid(arr[0], arr[1], arr[2], arr[3]);
System.out.println(t.toString());
System.out.println("\n Area of the Trapezoid is " +
t.area());
break;
default:
System.out.println("Invalid points entered");
}
}
}
/******************* Point.java *****************/
public class Point {
// x represents x axis value and y represents y axis
value
private double x;
private double y;
// Constructor to initialize data members
Point(double x, double y) {
this.x = x;
this.y = y;
}
// Returns the x- coordinate value of a point
public double getX() {
return x;
}
// Returns the y- coordinate value of a point
public double getY() {
return y;
}
@Override
public String toString() {
return "(" + x + ", " + y +
")";
}
}
/********************** Quadrilateral.java ********************/
abstract class Quadrilateral {
protected Point p1;
protected Point p2;
protected Point p3;
protected Point p4;
Quadrilateral(Point point1, Point point2, Point
point3, Point point4) {
this.p1 = point1;
this.p2 = point2;
this.p3 = point3;
this.p4 = point4;
}
abstract double area();
public void setP1(Point p1) {
this.p1 = p1;
}
public void setP2(Point p2) {
this.p2 = p2;
}
public void setP3(Point p3) {
this.p3 = p3;
}
public void setP4(Point p4) {
this.p4 = p4;
}
@Override
public String toString() {
return "[p1=" + p1 + ", p2=" + p2 +
", p3=" + p3 + ", p4=" + p4 + "]";
}
}
/*********************** Parallelogram.java *******************/
class Parallelogram extends Quadrilateral {
public Parallelogram(Point point1, Point point2, Point
point3, Point point4) {
super(point1, point2, point3,
point4);
}
@Override
double area() {
double r = ((p1.getX() * p2.getY())
- (p1.getY() * p2.getX()))
+ ((p2.getX() * p3.getY()) - (p2.getY() *
p3.getX()))
+ ((p3.getX() * p4.getY()) - (p3.getY() *
p4.getX()))
+ ((p4.getX() * p1.getY()) - (p4.getY() *
p1.getX()));
double a = Math.abs(r) / 2;
if (a == 0) {
Point t =
p4;
this.setP4(p3);
this.setP3(t);
a =
area();
}
return a;
}
@Override
public String toString() {
return "Parallelogram " +
super.toString();
}
}
/*************** Rectangle.java *********************/
class Rectangle extends Quadrilateral {
public Rectangle(Point p1, Point p2, Point p3,
Point p4) {
super(p1, p2, p3, p4);
}
double distSq(Point p, Point q) {
return (p.getX() - q.getX()) *
(p.getX() - q.getX()) + (p.getY() - q.getY()) * (p.getY() -
q.getY());
}
@Override
double area() {
double side = Math.sqrt(distSq(p1,
p2));
double side2 = Math.sqrt(distSq(p1,
p3));
return side * side2;
}
@Override
public String toString() {
return "Rectangle " +
super.toString();
}
}
/***************** Square.java *****************/
public class Square extends Quadrilateral {
Square(Point p1, Point p2, Point p3, Point p4)
{
super(p1, p2, p3, p4);
}
double distSq(Point p, Point q) {
return (p.getX() - q.getX()) *
(p.getX() - q.getX()) + (p.getY() - q.getY()) * (p.getY() -
q.getY());
}
@Override
double area() {
double d1 = Math.sqrt(distSq(p1,
p2)); // from p1 to p2
double d2 = Math.sqrt(distSq(p3,
p4)); // from p1 to p3
return d1 * d2;
}
@Override
public String toString() {
return "Square" +
super.toString();
}
}
/***************** Trapezoid.java ********************/
class Trapezoid extends Quadrilateral {
public Trapezoid(Point point, Point point2, Point
point3, Point point4) {
super(point, point2, point3,
point4);
}
double distSq(Point p, Point q) {
return (p.getX() - q.getX()) *
(p.getX() - q.getX()) + (p.getY() - q.getY()) * (p.getY() -
q.getY());
}
@Override
double area() {
return 0.5
* Math.abs((p1.getX() * (p2.getY() - p3.getY())
+ p2.getX() * (p3.getY() - p1.getY())
+
p3.getX() * (p1.getY() - p2.getY())))
+ 0.5 * Math.abs((p1.getX() * (p3.getY() -
p4.getY()) + p3.getX() * (p4.getY() - p1.getY())
+
p4.getX() * (p1.getY() - p3.getY())));
}
@Override
public String toString() {
return "Trapezoid" +
super.toString();
}
}