Ruby
3 credits
Square class created | 5 |
The side of a square can be set at instantiation | 10 |
The side of a square can be changed and displayed on its own | 10 |
Squares have a side attribute | 5 |
A square returns all required information when used with a puts statement | 30 |
A square can output its correct perimeter | 15 |
A square can return its correct area | 15 |
When the Square class is used with the sample code it produces the same output | 10 |
Total | 100 pts |
To work with basic custom ruby objects
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.
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.
When your square class is executing with the following code it should produce the following output:
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
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