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"
class Program
{
public class Square
{
private double X_Coordinate;
private double Y_Coordinate;
private double side;
public Square(double X_Coordinate, double Y_Coordinate, double side)
{
this.X_Coordinate = X_Coordinate;
this.Y_Coordinate = Y_Coordinate;
this.side = side;
}
public void Move(double X, double Y)
{
X_Coordinate += X;
Y_Coordinate += Y;
}
public void Scale(double scaleFactor)
{
side *= scaleFactor;
}
public override string ToString()
{
return string.Format("Corner({0},{1}) side {2}", X_Coordinate, Y_Coordinate, side);
}
public double GetPerimeter()
{
return 4 * side;
}
public double GetArea()
{
return side * side;
}
}
static void Main(string[] args)
{
Square square = new Square(10, 5, 7);
Console.WriteLine(string.Format("Square: {0}",square));
square.Move(1, 2);
Console.WriteLine(string.Format("Square: {0}", square));
square.Scale(2);
Console.WriteLine(string.Format("Square: {0}", square));
Console.WriteLine(string.Format("Perimeter: {0}", square.GetPerimeter()));
Console.WriteLine(string.Format("Area: {0}", square.GetArea()));
Console.ReadKey();
}
}
Comments
Leave a comment