Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please Explain in JAVA Need help in order to learn and understand if you could please explain how you did the coding What problem will

Please Explain in JAVA

Need help in order to learn and understand if you could please explain how you did the coding

What problem will you be addressing in this lab? In this lab, you are going to manipulate shapes in a 2D space (with (x,y) coordinates): polygons, but also rectangles and triangles. After the lab, we encourage you to keep extending the code to accommodate more shapes.

For this lab, we accept that a polygon is fully described by:

  • Its size: that is, its number of vertices;
  • The 1D array of its vertices, ordered adjacently (contiguous vertices in the array are adjacent vertices in the polygon); and
  • A name, which describes what type of polygon we are handling

So for instance, a square is a polygon with 4 vertices that is named a square, and whose list of vertices contains the 4 vertices that make up the square at hand.

You are given a file called Polygon.java that allows you to create and handle polygons. In particular, it comes with:

  • A method called perimeter, which computes the perimeter of the polygon;
  • A method called area, which aims to compute the surface defined by the polygon; and
  • A method toString, which aims to nicely print the vertices of the polygon in the order in which they appear in the array of vertices.

Please make yourselves familiar with it.

You will notice that this java file is missing its getters and setters. Your task, with this file, is to complete it with one getter and one setter per attribute of your type Polygon.

Now, each vertex is basically a point in space. Each point is fully described by:

  • Its x coordinate; and
  • Its y coordinate.

So for instance, point (3,2) has 3 as the value for x and 2 as the value for y. You are given a file called Point.java that allows you to create and handle points. In particular, it comes with:

  • A method distance to compute the distance between the current point and another point; and
  • A method toString to nicely print out the coordinates of the current point.

Please make yourself familiar with it.

You have nothing to add to this file J

There are many types of polygons: a square is a polygon, a triangle is a polygon, a hexagon is a polygon, etc. All of these is a relationships are a good indication of an inheritance relationship. If we were to define a type Rectangle, it should definitely inherit from the type Polygon (because a Rectangle is a Polygon).

We provide you with one more file: Rectangle.java. This new type inherits from Polygon. We say that Rectangle and Triangle are subclasses of Polygon, and reversely, Polygon is the superclass of Rectangle.

  • Attributes: A rectangle has no more attributes than a polygon. Therefore, you can see in the file Rectangle.java that no additional attribute is defined.
  • Methods:
    • The perimeter of a rectangle is calculated the same way a polygons is. Therefore there is no perimeter method within Rectangle.java.
    • However, the area of a rectangle is easily computed and a new version is available to you in Rectangle.java.

What are you asked to do?

You are asked to do the following:

  • Within java file that is given to you:
    • Write one setter and one getter per attribute of a polygon
    • Write a method distanceMin:
      • This method applies to a current polygon P1 and takes another one P2 as a parameter;
      • It identifies the pair of points (p1,p2) where p1 is a vertex of P1, and p2 is a vertex of P2, that are closest in distance (using the method distance from class Point), and returns their distance;
      • Hint: you will use the Point.java method distance to implement this method.
    • Answer the following question: How many steps, in terms of the respective sizes of polygons P1 and P2, does method distanceMin execute? Justify your answer?
  • Create a new class Triangle (a triangle is a polygon) that inherits from Polygon.
    • You have to create a new file java.
    • This class has no more attributes than a Polygon.
    • It should have its own version of the area method: you do not have to figure out the formula for a triangles area. Your method simply has to be different from that of its superclass and include this printout Area of a triangle."
  • Create a new class Square (a square is some sort of a rectangle) that inherits from Rectangle.
    • You have to create a new file java.
    • This class has no more attributes than a Rectangle, and for that matter, than a Polygon.
    • It should have its own version of the area method: the area method of the square class should use the area method of Rectangle (it should clearly call it) but also print out This is the area method of class Square."

HERE IS CODE GIVEN

/* This file is given to you so you can try the classes and methods you have to put together */

public class ExecuteShapes {

public static void main(String[] args) { Point one = new Point(0,0); Point two = new Point(0,2); Point three = new Point(1,2); Point four = new Point(1,0); Point[] square = {one, two, three, four};

// Polygon System.out.println("Let's play with Polygons: "); Polygon shape = new Polygon(square, "square"); System.out.println(shape.perimeter()); System.out.println(shape.area());

// Rectangle System.out.println("Let's play with Rectangles: "); Rectangle R = new Rectangle(); R.setVertices(square); System.out.println(R.toString()); System.out.println(R.area());

Point one2 = new Point(1,0); Point two2 = new Point(2,0); Point three2 = new Point(2,-1); Point four2 = new Point(1,-1); Point[] square2 = {one2, two2, three2, four2}; Rectangle R2 = new Rectangle(); R2.setVertices(square2); System.out.println(R2.toString()); System.out.println(R2.area());

System.out.println(R.distanceMin(R2));

// Triangle Point[] tri = {one, two, three}; Polygon shapeT = new Polygon(tri, "triangle"); System.out.println(shapeT.perimeter()); System.out.println(shapeT.area());

System.out.println("Let's play with Triangles: "); Triangle T = new Triangle(); T.setVertices(tri); System.out.println(T.toString());

System.out.println(T.area());

System.out.println(T.distanceMin(R2));

// Array of polygons of different specialized types Polygon[] AP = new Polygon[3]; AP[0] = new Rectangle(); AP[0].setVertices(square2); AP[1] = new Triangle(); AP[1].setVertices(tri); AP[2] = new Polygon(square,"square"); for (int i=0; i

// Let's play Polygon P2 = new Rectangle(); P2.setVertices(square); System.out.println(P2.toString()); System.out.println(P2.area());

}

}

public class Polygon {

/* Attributes */ private int size; private Point[] vertices = new Point[size]; // list of adjacent vertices private String name; /* Constructors ***************************************************/ public Polygon() {} public Polygon(int size, String str) { this.size = size; name = str; }

public Polygon(Point[] P, String str) { size = P.length; name = str; vertices = P; }

/* Getters and Setters *********************************************/ /** * @return the size */ public int getSize() { }

/** * @param size the size to set */ public void setSize(int size) {

}

/** * @return the vertices */ public Point[] getVertices() {

}

/** * @param vertices the vertices to set */ public void setVertices(Point[] vertices) {

}

/** * @return the name */ public String getName() {

}

/** * @param name the name to set */ public void setName(String name) {

} /* Other Methods *********************************************************/ public double perimeter() { if (size <= 1) return 0; double result = 0; Point current = vertices[0]; for (int i = 1; i < vertices.length; i++) { result += Math.sqrt(Math.pow(current.getX()-vertices[i].getX(), 2) + Math.pow(current.getY()-vertices[i].getY(), 2)); current = vertices[i]; } result += Math.sqrt(Math.pow(current.getX()-vertices[0].getX(), 2) + Math.pow(current.getY()-vertices[0].getY(), 2));

return result; } public double area() { System.out.println("I won't compute it at this level. Please run it at the subclass level"); return -1; } public String toString() { String result = "{"; for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1. What is meant by Latitudes? 2. What is cartography ?

Answered: 1 week ago

Question

What is order of reaction? Explain with example?

Answered: 1 week ago

Question

4. Does cultural aptitude impact ones emotional intelligence?

Answered: 1 week ago