Graderpractice for programmers

273387 Swift - Mini Test 2

Awaiting submission
Write a class called Point. It should contain two Int variables: x and y. You should be able to create a new point object as follows:
let p = Point(x: 3, y: 5)
Awaiting submission
Add a function to your Point class that returns a String describing the value as (X,Y). Test code:
let p = Point(x: 7, y: 21)
print(p.description())        // expected output: (7,21)
Awaiting submission
Write a class called Rectangle that defines a rectangle as two Point objects. One point will represent the north-west corner (nw) and the other will represent the south-east corner (se).
let rect1 = Rectangle(ne: Point(x: 0, y: 1), sw: Point(x: 3, y: 3))
Awaiting submission
Modify your Rectangle class by adding 2 functions: width() and height(). Calculate the width and height from the x and y distance between the 2 points. Note: sw is the South-West point and ne is the North-East point.

Test code:
let p1 = Point(x: 8, y: 2)
let p2 = Point(x: 3, y: 5)
let rect = Rectangle(ne: p1, sw: p2)
print("Width is \(rect.width())")
print("Height is \(rect.height())")
Awaiting submission
Modify your Rectangle class by adding a function to calculate the area of the rectangle.
let p1 = Point(x: 8, y: 2)
let p2 = Point(x: 3, y: 5)
let rect = Rectangle(ne: p1, sw: p2)
print("Area is \(rect.area())")
Awaiting submission
Modify your Rectangle class again! Add a function that returns true if the rectangle is a square.
let p1 = Point(x: 8, y: 1)
let p2 = Point(x: 3, y: 6)
let rect = Rectangle(ne: p1, sw: p2)
if rect.isSquare() {
    print("It is a square")
}
Awaiting submission
Add a function to your Rectangle class that takes a Point as a parameter and returns true if the point is inside (or on the edge) of the rectangle.
let p1 = Point(x: 8, y: 2)
let p2 = Point(x: 3, y: 5)
let rect = Rectangle(ne: p1, sw: p2)
let p3 = Point(x: 4, y: 3)
if rect.contains(point: p3) {
    print("The point \(point.description()) is inside the rectangle.")
}