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, width and height that store the window's current width and height, respectively.
The initial width and height should be 80 and 60, respectively.
Include a method resize(newWidth:newHeight:) that takes new width and height parameters and changes the properties to reflect the new size.
let size1080x764 = Size(width: 1080, height: 764)
// Returns Size
var size1200x800 = size1080x764
// Returns Size
size1200x800.resize(newWidth: 1200, newHeight: 800)
size1200x800.height
// Returns 800Define 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)
// Returns Position
point.moveTo(newX: 100, newY: -100)
point.y
// Returns -100Define a window class with the following properties:
title : String, Initial value is "New Window"screenSize : Size, constant value with width = 800 and height = 600size : Size, initial value is the default value of the Size structposition : Position, initial value is the default value of the Position structcontents : String?, initial value is nilYou should also define an empty initializer for the class.
let window = Window()
window.title
// Returns "New Window"resize(to:) : (Size) -> () - This method takes a Size struct as input and attempts to resize the window to the specified size. However, the new size cannot exceed certain bounds.x = 400, y = 300 and a resize to height = 400, width = 300 is requested, then the window would be resized to height = 300, width = 300 as the screen is not large enough in the y direction to fully accommodate the request.move(to:) : (Position) -> () - This is similar to resize(to:), however, this method adjusts the position of the window to the requested value, rather than the size. As with resize the new position cannot exceed certain limits.x and y.x = 250, y = 100 and a move to x = 600, y = 200 is requested, then the window would be moved to x = 550, y = 200 as the screen is not large enough in the x direction to fully accommodate the request.update(title:) : (String) -> () - This method sets the title property to the value of the string that was passed in.update(text:) : (String?) -> () - This method sets the contents 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 the title "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" - If contents is nil, the last line should read "[This window intentionally left blank]"The window system should have an initializer so that the window can be created with custom inputs.
Create another initializer for the Window class.
The initializer should take the following parameters: title, contents, and two optional parameters size and position.
The size and position parameters should default to the default values of the Size and Position structs.
let window = Window(title: "My First Window", contents: "I 😍 my window")
window.display()
// Returns "My First Window\nPosition: (0, 0), Size: (80 x 60)\nI 😍 my window\n"
let window2 = Window(title: "My Second Window", contents: "I 😍 my window", size: Size(width: 200, height: 150), position: Position(x: 10, y: 100))
window2.display()
// Returns "My Second Window\nPosition: (10, 100), Size: (200 x 150)\nI 😍 my window\n"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, width and height that store the window's current width and height, respectively.
The initial width and height should be 80 and 60, respectively.
Include a method resize(newWidth:newHeight:) that takes new width and height parameters and changes the properties to reflect the new size.
let size1080x764 = Size(width: 1080, height: 764)
// Returns Size
var size1200x800 = size1080x764
// Returns Size
size1200x800.resize(newWidth: 1200, newHeight: 800)
size1200x800.height
// Returns 800Define 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)
// Returns Position
point.moveTo(newX: 100, newY: -100)
point.y
// Returns -100Define a window class with the following properties:
title : String, Initial value is "New Window"screenSize : Size, constant value with width = 800 and height = 600size : Size, initial value is the default value of the Size structposition : Position, initial value is the default value of the Position structcontents : String?, initial value is nilYou should also define an empty initializer for the class.
let window = Window()
window.title
// Returns "New Window"resize(to:) : (Size) -> () - This method takes a Size struct as input and attempts to resize the window to the specified size. However, the new size cannot exceed certain bounds.x = 400, y = 300 and a resize to height = 400, width = 300 is requested, then the window would be resized to height = 300, width = 300 as the screen is not large enough in the y direction to fully accommodate the request.move(to:) : (Position) -> () - This is similar to resize(to:), however, this method adjusts the position of the window to the requested value, rather than the size. As with resize the new position cannot exceed certain limits.x and y.x = 250, y = 100 and a move to x = 600, y = 200 is requested, then the window would be moved to x = 550, y = 200 as the screen is not large enough in the x direction to fully accommodate the request.update(title:) : (String) -> () - This method sets the title property to the value of the string that was passed in.update(text:) : (String?) -> () - This method sets the contents 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 the title "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" - If contents is nil, the last line should read "[This window intentionally left blank]"The window system should have an initializer so that the window can be created with custom inputs.
Create another initializer for the Window class.
The initializer should take the following parameters: title, contents, and two optional parameters size and position.
The size and position parameters should default to the default values of the Size and Position structs.
let window = Window(title: "My First Window", contents: "I 😍 my window")
window.display()
// Returns "My First Window\nPosition: (0, 0), Size: (80 x 60)\nI 😍 my window\n"
let window2 = Window(title: "My Second Window", contents: "I 😍 my window", size: Size(width: 200, height: 150), position: Position(x: 10, y: 100))
window2.display()
// Returns "My Second Window\nPosition: (10, 100), Size: (200 x 150)\nI 😍 my window\n"