Class Square Java Exercise - Java Programming Course

 Exercise

Class Square

 Objetive

Complete the project named "Shapes", adding a class named "Square" to it. For each square, we will store its starting X and Y coordinates (the upper left corner, already stored as a "Location") and the length of its side.

You will have to create:
- A suitable constructor, to assign starting values to X, Y and the side. (2 points)
- A Move method, to change X and Y coordinates. (1 point)
- A Scale method, to change its side (for example, a scale factor of 2 would turn a side of 3 into 6). (1 point)
- A method ToString, to return a string with its data (for example: "Corner (10,5), side 7". (1 point)
- Redefine "GetPerimeter" and "GetArea", so that they return the correct values (2 points).

- Another point corresponds to the attributes and the overall structure.

- The remaining 2 points correspond to the test from "Main"

You must deliver a ZIP file containing the entire project.

 Example Code

package shapes;
public class Main
{
	private Location l = new Location();

	public Square(double x, double y, double side)
	{

		l.SetX(x);
		l.SetY(y);
		l.SetSide(side);
	}

	public final void Move(double x, double y)
	{
		l.SetX(x);
		l.SetY(y);
	}

	public final void Scale(int factor)
	{
		l.SetSide(l.GetSide() * factor);
	}

	public final String toString()
	{
		return "Corner (" + l.GetX() / 2 + "), side " + l.GetSide();
	}
}



Juan A. Ripoll - Programming Tutorials and Courses © 2024 All rights reserved.  Legal Conditions.