Squares

100 points

Squares

Requirements

Square class created5
The side of a square can be set at instantiation10
The side of a square can be changed and displayed on its own10
Squares have a side attribute5
A square returns all required information when used with a puts statement30
A square can output its correct perimeter15
A square can return its correct area15
When the Square class is used with the sample code it produces the same output10
Total100 pts

Resources

Objective

To work with basic custom ruby objects

Instructions

Summary

For this lab you will need to create a ruby class called Square. This class should allow for objects to be instantiated from it that represent square shapes, for the squares to be modified, and for various types of information about the squares to be output. No input or data validation is required in this lab and we are not concerned with units of measure.

Details

Each square object created from the Square class should have a single attribute called side. The value of the side attribute represents the length of a single side of the square. The length of a square's side should be settable at the time the square object is instantiated or after the object exists and should be displayable on its own.

Each square object should also be able to calculate and return its area (side * side) and it's perimeter (side * 4). To allow this the class should have instance methods called area and perimeter which return the corresponding values.

Each square object should also be displayable. When a square is displayed it should return a string that states that this is a square, what it's side value is, what it's area value is, and what it's perimeter is.

Execution

When your square class is executing with the following code it should produce the following output:

Code

s1 = Square.new 2
puts s1

s1.side = 3
puts "The side was changed to #{s1.side}"
puts "Now it's area is #{s1.area}"
puts "and it's perimeter is #{s1.perimeter}"

s2 = Square.new 3
puts s2

Output

Square: side is 2, area is 4, perimeter is 8
The side was changed to 3
Now it's area is 9
and it's perimeter is 12
Square: side is 3, area is 9, perimeter is 12