In this exercise, you will be simulating a windowing based computer system. You will create some windows that can be moved and resized and display their contents. The following image is representative of the values you will be working with below.
<--------------------- screenSize.width --------------------->
^ ╔════════════════════════════════════════════════════════════╗
| ║ ║
| ║ position.x,_ ║
| ║ position.y \ ║
| ║ \<----- size.width -----> ║
| ║ ^ *──────────────────────┐ ║
| ║ | │ title │ ║
| ║ | ├──────────────────────┤ ║
screenSize.height ║ | │ │ ║
| ║ size.height │ │ ║
| ║ | │ contents │ ║
| ║ | │ │ ║
| ║ | │ │ ║
| ║ v └──────────────────────┘ ║
| ║ ║
| ║ ║
v ╚════════════════════════════════════════════════════════════╝
Define a struct named Size
with two Int
properties, height
and width
that store the window's current height and width, respectively. The initial height and width should be 80 and 60, respectively. Include a method resize(newHeight:newWidth:)
that takes new height and width parameters and changes the properties to reflect the new size.
let size1080x764 = Size(height: 764, width: 1080)
// => Size
var size1200x800 = size1080x764
// => Size
size1200x800.resize(newHeight: 800, newWidth: 1200)
size1200x800.height
// => 800
Define a struct named Position
with two Int
properties, x
and y
that store the current horizontal and vertical position, respectively, of the window's upper left corner. The initial values of x and y should each be 0. The position (0, 0) is the upper left corner of the screen with x
values getting larger as you move right and y
values getting larger as you move down.
Include a method moveTo(newX:newY:)
that takes new x and y parameters and changes the properties to reflect the new position.
var point = Position(x: 10, y: 20)
// => Position
point.moveTo(newX: 100, newY: -100)
point.y
// => -100
Define a window class with the following properties:
title
:String
, Initial value is "New Window"screenSize
:Size
, constant value withwidth
= 800 andheight
= 600size
:Size
, initial value is the default value of theSize
structposition
:Position
, initial value is the default value of thePosition
structcontents
:String?
, initial value isnil
resize(to:)
:(Size) -> ()
- This method takes aSize
struct as input and attempts to resize the window to the specified size. However, the new size cannot exceed certain bounds. - The minimum allowed height or width is 1. Requested heights or widths less than 1 will be clipped to 1. - The maximum height and width depends on the current position of the window, the edges of the window cannot move past the edges of the screen. Values larger than these bounds will be clipped to the largest size they can take. E.g. if the window's position is atx
= 400,y
= 300 and a resize toheight
= 400,width
= 300 is requested, then the window would be resized toheight
= 300,width
= 300 as the screen is not large enough in they
direction to fully accommodate the request.
move(to:)
:(Position) -> ()
- This is similar toresize(to:)
, however, this method adjusts the position of the window to the requested value, rather than the size. As withresize
the new position cannot exceed certain limits. - The smallest position is 0 for bothx
andy
. - The maximum position in either direction depends on the current size of the window; the edges cannot move past the edges of the screen. Values larger than these bounds will be clipped to the largest size they can take. E.g. if the window's size is atx
= 250,y
= 100 and a move tox
= 600,y
= 200 is requested, then the window would be moved tox
= 550,y
= 200 as the screen is not large enough in thex
direction to fully accommodate the request.
update(title:)
:(String) -> ()
- This method sets thetitle
property to the value of the string that was passed in.update(text:)
:(String?) -> ()
- This method sets thecontents
property to the value of the optional string that was passed in.display()
:() -> String
- This method returns a string describing the current state of the window. For example, if the window has thetitle
"My First Window" with position: x = 10, y = 100; size: width = 200, height = 150; and contents: "I 😍 my window", it should return the string:"My First Window\nPosition (10, 100), Size: (200 x 150)\nI 😍 my window\n"
- Ifcontents
is nil, the last line should read "[This window intentionally left blank]"
Create an instances of the Window class and modify it via their methods as follows:
- The window should be given the title "Main Window", with a width of 400, a height of 300 and positioned at x = 100, y = 100. Its contents should be "This is the main window". Assign this instance to the name
mainWindow
.