Write a Square class, which represents a square on the2-dimensional x-y plane. A
ID: 3613329 • Letter: W
Question
Write a Square class, which represents a square on the2-dimensional x-y plane. Allsquares have a width (of each side) as well as a position on the2-dimensional x-y plane.
You may assume that the square’s sides must always beparallel to the x and y-axes.
Your class must have accessor methods for the following:
· The length of the side of the square
· The x coordinate of the center of the square
· The y coordinate of the center of the square
Your class must also have methods that allow you to determine(return) the following
(these should not be printed on the screen from inside thesemethods):
· The area of the square
· The perimeter of the square
· Whether a given point lies inside the square. This methodshould take 2
parameters, which are the x and y coordinates of the given point. Apoint that lies
on or outside of the boundary is not inside the square.
For your Square class, you must decide for yourself:
· Appropriate instance variables
· Appropriate method names, parameters and return types ofthe above methods, as
well as for the constructor
· What code to write in these methods and theconstructor
In a separate class, the SquareTester class, create Square objectsand perform basic
testing by using each of the methods that you wrote at least once,and printing out some
results to prove that they are working. There must be no printstatements inside the
Square class. Instead, all the printing should take place insidethe SquareTester class.
Explanation / Answer
publicclass Square { /* All squares have a width (of each side) aswell as a position on the 2-dimensional x-y plane. You may assumethat the square’s sides must always be parallel to the x andy-axes. */ // instancevariables privateint width; // x and y are thecoordinates of the center privateint x, y; //constructor publicSquare(int w, int x2, int y2) { width=w; x=x2; y=y2; } /* Your class must have accessor methods for thefollowing: · The length of the side of thesquare · The x coordinate of the center of thesquare · The y coordinate of the center of thesquare */ // accessors publicint getWidth() { return width; } publicint getX() { return x; } publicint getY() { return y; } /* Your class must also have methods that allowyou to determine (return) thefollowing (these should not be printed on the screen from insidethese methods): · The area of the square · The perimeter of the square · Whether a given point lies inside thesquare. This method should take 2 parameters, which are the x and ycoordinates of the given point. A point that lies on or outside ofthe boundary is not inside the square. */ publicint getArea() { return width*width; } publicint getPerimeter() { return width*4; } publicboolean containsPoint(int x2, int y2) { int halfwidth=width/2; //extends half the width from the center in eitherdirection return (x2>=x-halfwidth &&x2=y-halfwidth &&yRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.