Question

In: Computer Science

I need to update this program to follow the these requirements please and thank you :...

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)));
}

}

Solutions

Expert Solution

/***************** 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();
   }

}


Related Solutions

I need a scholarship essay for my nursing program. please and thank you.
I need a scholarship essay for my nursing program. please and thank you.
I ONLY need requirements 1c-1f and 2 please. Thank you! Endless Mountain Company manufactures a single...
I ONLY need requirements 1c-1f and 2 please. Thank you! Endless Mountain Company manufactures a single product that is popular with outdoor recreation enthusiasts. The company sells its product to retailers throughout the northeastern quadrant of the United States. It is in the process of creating a master budget for 2019 and reports a balance sheet at December 31, 2018 as follows: Endless Mountain Company Balance Sheet December 31, 2018 Assets Current assets: Cash $ 46,200 Accounts receivable (net) 260,000...
I only need to update this JAVA program to be able to : 1 ) Exit...
I only need to update this JAVA program to be able to : 1 ) Exit cleanly after creating the chart 2 ) change the pie chart to a perfect circle rather than an oval 3 ) provide a separate class driver import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.util.Scanner; import javax.swing.JComponent; import javax.swing.JFrame; // class to store the value and color mapping of the // pie segment(slices) class Segment { double value; Color color; // constructor public...
please i need SBAR for normal abdominal examination thank you
please i need SBAR for normal abdominal examination thank you
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not against it being included in the answer* I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Please, I need a correct answer and clear explanation. Thank you, Part I – Explanation Q1...
Please, I need a correct answer and clear explanation. Thank you, Part I – Explanation Q1 Below are pairs of changes that affect elements of the accounting equation. Give an example of a transaction or economic event that reflects each: a. Asset increases, asset decreases b. Asset increases, liability increases c. Asset increases, shareholders’ equity increases d. Asset increases, revenue increases e. Liability decreases, asset decreases f. Asset decreases, expense increases g. Liability decreases, revenue increases h. Asset decreases, shareholders’...
I need an answer as soon as possible please...Thank you The following information relates to the...
I need an answer as soon as possible please...Thank you The following information relates to the HTM debt securities investments of Kiran Company during 2018: a.            February 1: The company purchased 9% bonds of Tempe Co. having a par value of $100,000 at 99 plus accrued interest. Interest is payable May 1 and November 1. Maturity date is 11/1/19. b.            May 1: Semiannual interest is received and amortization is updated. c.            July 1: 10% bonds of Flagstaff, Inc. were purchased....
I need an unadjusted and adjusted trial balance for this problem please and thank you! Palisade...
I need an unadjusted and adjusted trial balance for this problem please and thank you! Palisade Creek Co. is a merchandising business that uses the perpetual inventory system. The account balances for Palisade Creek Co. as of May 1, 2019 (unless otherwise indicated), are as follows: 110 Cash $ 83,600 112 Accounts Receivable 233,900 115 Merchandise Inventory 624,400 116 Estimated Returns Inventory 28,000 117 Prepaid Insurance 16,800 118 Store Supplies 11,400 123 Store Equipment 569,500 124 Accumulated Depreciation-Store Equipment 56,700...
HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM I WILL LEAVE AWESOME RATING. THANK YOU IN...
HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM I WILL LEAVE AWESOME RATING. THANK YOU IN ADVANCE. QUESTION Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT