Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Point { private int x; private int y; private int id; public static int idCounter; // constructor public Point(int _x,int _y){ this.x=_x; this.y=_y;

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

public class Point { private int x; private int y; private int id; public static int idCounter; // constructor public Point(int _x,int _y){ this.x=_x; this.y=_y; this.id=idCounter; idCounter++; } public Point(){ this.x=0; this.y=0; this.id=idCounter; idCounter++; } // getter and setter public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } // return object data as string public String toString(){ return("ID "+this.id+" ("+this.x+","+this.y+")"); } public boolean equals(Point p){ if(this.id==p.hashCode()){ return true; } else{ return false; } } public int hashCode(){ return this.id; } }

In this assignment, you will program using the Adapter pattern. The requirements and steps are given below. 1) First, copy the Point class from Assignment 1 to the new project. (Be sure to name the project correctly.) 2) Next, create an interface named Shape having just one method named display(), with no parameters and returning void. A Shape object represents a shape (like a line or circle or rectangle) that can be displayed graphically or by listing its relevant points on the console. The display mode (graphical or listing) is at the discretion of the implementor. 3) The interface is implemented by the following class Polygon, which represents a polygon. The Point class is from the first assignment. (Important convention: X coordinates increase as we move right and Y coordinates increase as we move down.) * This class implements a Polygon shape. The vertices are stored in an array of Point objects. Vertices are added using the add() method. * After all vertices are added, the user is expected to call the end() method, which duplicates the first vertex * and stores it as the last vertex. This makes it a bit easier to display the Polygon in a graphical interface. (The current implementation only displays the vertices on the console.) @author Brahma Dathan @Copyright (c) 2018 *7 public class Polygon implements Shape { private Point[points; private int numberOfPoints; Creates the array to hold at most 10 Point objects. The addPoint() method * expands the size as needed. 7 public Polygon() { points = new Point(10): } * Adds one more vertex to the polygon * @param point */ public void addPoint(Point point) { if (this.points.length == numberOfPoints + 1) { Point( temp = new Point(numberOfPoints. 2): System.arraycopy(points, O, temp, 0, numberOfPoints); points = temp: } points[numberOfPoints++] = point; points[numberOfPoints] = point; } * Adds the first vertex as the last vertex, so the drawing (if we are drawing) would be complete. public void end() { points[numberOfPoints] = points[0]; } @Override public void display() { System.out.println("Polygon" + toString(); } @Override public String toString() { String string = "Vertices "; for (int index = 0; index

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

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions

Question

=+ At what rate does capital per person grow?

Answered: 1 week ago