Write a class called Square that represents a square two-dimensional region Your
ID: 3916507 • Letter: W
Question
Write a class called Square that represents a square two-dimensional region Your Square objects should have the following methods /l Constructs a new Square object whose /l top-left corner is specified by the givenx and y coordinates Il and whose side length is specified by s. ublic Square(int x, int y, int s) /l Returns the Square's perimeter public int getPerimeter: pui /l Returns the Square's area public int getArea0: /l Returns a String representation of this Square, such as: // "Square[x-1. ?-1, s 2, perimeter: 8 units, area: 4 square units]" public String toString):Explanation / Answer
class Square
{
private int x,y,s;
public Square(int x,int y,int s)// constructor
{
this.x = x;
this.y = y;
this.s = s;
}
//get methods
public int getPerimeter()
{
return 4*s;
}
public int getArea()
{
return s*s;
}
public String toString()
{
return "Square[x = "+x+",y = "+y+" s = "+s+",perimeter = "+getPerimeter()+" units,Area = "+getArea()+" square units]";
}
}
class TestSquare
{
public static void main (String[] args)
{
Square sqr = new Square(1,1,2);
System.out.println(sqr);
}
}
Output:
Square[x = 1,y = 1 s = 2,perimeter = 8 units,Area = 4 square units]
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.