Difference between revisions of "W1501 Introduction to Objects"

From Coder Merlin
Line 1: Line 1:
[[File:Blurry reflection.jpg|thumb|link=|Blurry reflection]]
== Prerequisites ==
== Prerequisites ==
* [[W1412 Constructors]]
* [[W1412 Constructors]]


== Research ==
== Background ==
* Read [[Media:ComputerScienceOne.pdf|Computer Science I Textbook by Bourke]] Chapter 10. Encapsulation & Objects
* Read [[Media:ComputerScienceOne.pdf|Computer Science I Textbook by Bourke]] Chapter 10. Encapsulation & Objects
* Review [[Media:1501_Objects.pdf|Presentation on Objects]]
* Review [[Media:1501_Objects.pdf|Presentation on Objects]]


== Explore ==
== Introduction ==
{{NewProject|Begin a '''new''' project.}}
We'll begin our exploration of objects using '''Scenes'''.  '''Scenes''' is an object-oriented, event-driven platform that enables us to write server-side Swift code which (rapidly) sends a series of instructions to a browser to generate visual effects, and optionally, an interactive experience.  '''Scenes''' is built upon another framework called '''Igis'''.


Create a new Igis shell project within your "Experiences" directory.
{{Caution|
<syntaxhighlight lang="bash">
It's important to understand that while most of the objects we'll be using have an associated ''visual'' representation, most of the objects that we build and interact with in programming will not have such a representation.
cd ~/Experiences
}}
git clone https://github.com/TheCoderMerlin/IgisShellD W1501
 
</syntaxhighlight>
== Prepare ==
Create a new Scenes shell project within your "Experiences" directory:
{{ConsoleLine|jane-williams@codermerlin:~$ |cd ~/Experiences}}
{{ConsoleLine|jane-williams@codermerlin:~$ |git clone <nowiki>https://github.com/TheCoderMerlin/ScenesShellBasic</nowiki> W1501}}
 
 
Getting started with a ''shell'' enables us to avoid writing repetitive code commonly required by all similar projects.  This type of code is often called [https://en.wikipedia.org/wiki/Boilerplate_code '''boilerplate code'''].
 
Enter the Sources/ScenesShell directory of the new project:
{{ConsoleLine|jane-williams@codermerlin:~$ |cd W1501/Sources/ScenesShell/}}


Enter into the Sources/IgisShellD directory of the new project.
<syntaxhighlight lang="bash">
cd W1501/Sources/IgisShellD/
</syntaxhighlight>


{{RunProgram|Run the program.
{{RunProgram|Run the program.
<syntaxhighlight lang="bash">
{{ConsoleLine|jane-williams@codermerlin:~$ |run}}
./run.sh
}}
</syntaxhighlight>}}
 


Open a browser (or use a new tab on an already-open browser).
Go to the URL:  http://www.codermerlin.com/users/user-name/dyn/index.html


Open a browser (or use a new tab on an already-open browser).  Then, go to the URL: 
<pre>
http://www.codermerlin.com/users/user-name/dyn/index.html
</pre>
NOTE:  You MUST change '''user-name''' to your actual user name.  For example, http://www.codermerlin.com/users/john-williams/dyn/index.html
NOTE:  You MUST change '''user-name''' to your actual user name.  For example, http://www.codermerlin.com/users/john-williams/dyn/index.html


Line 37: Line 43:
== Hello World ==
== Hello World ==
{{StopProgram|Stop the running program.
{{StopProgram|Stop the running program.
<syntaxhighlight lang="bash">
Return to the ''console'' and press ^C (Hold down the CONTROL key and press C)
Press ^C (Hold down the CONTROL key and press C)
}}
</syntaxhighlight>}}





Revision as of 20:19, 30 March 2020

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Blurry reflection

Prerequisites[edit]

Background[edit]

Introduction[edit]

We'll begin our exploration of objects using Scenes. Scenes is an object-oriented, event-driven platform that enables us to write server-side Swift code which (rapidly) sends a series of instructions to a browser to generate visual effects, and optionally, an interactive experience. Scenes is built upon another framework called Igis.

CautionWarnIcon.png

It's important to understand that while most of the objects we'll be using have an associated visual representation, most of the objects that we build and interact with in programming will not have such a representation.

Prepare[edit]

Create a new Scenes shell project within your "Experiences" directory:

jane-williams@codermerlin:~$  cd ~/Experiences

jane-williams@codermerlin:~$  git clone https://github.com/TheCoderMerlin/ScenesShellBasic W1501


Getting started with a shell enables us to avoid writing repetitive code commonly required by all similar projects. This type of code is often called boilerplate code.

Enter the Sources/ScenesShell directory of the new project:

jane-williams@codermerlin:~$  cd W1501/Sources/ScenesShell/


Start button green arrow
Run the program.

jane-williams@codermerlin:~$  run


Open a browser (or use a new tab on an already-open browser). Then, go to the URL:

http://www.codermerlin.com/users/user-name/dyn/index.html

NOTE: You MUST change user-name to your actual user name. For example, http://www.codermerlin.com/users/john-williams/dyn/index.html

You'll know you're successful if you see the title bar change to "Coder Merlin: IGIS". (The browser window will be blank because we haven't added any graphics yet.)

Hint.pngHelpful Hint
Helpful hint: It's useful to bookmark this page in your browser.

Hello World[edit]

Stop button red ex
Stop the running program.

Return to the console and press ^C (Hold down the CONTROL key and press C)


Open main.swift in emacs.

emacs main.swift

Find the function within the Painter object named "setup". Add a line to create a Text object and then draw (render) that object on the "canvas", an imaginary surface for painting within the browser.

    func setup(canvas:Canvas) {
        let text = Text(location:Point(x:50, y:50), text:"Hello, World!")
        canvas.render(text)
    }

Remember to save the file, then suspend emacs.

Start button green arrow
Run the program and refresh the browser page.
ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 1
  1. What do you observe?
  2. How large is the text?
Stop button red ex
Stop the running program.


Let's change the font. Resume emacs and edit the line where the text object is instantiated.

    func setup(canvas:Canvas) {
        let text = Text(location:Point(x:50, y:50), text:"Hello, World!")
        text.font = "30pt Arial"
        canvas.render(text)
    }

Remember to save the file, then suspend emacs.

Start button green arrow
Run the program and refresh the browser page.
ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 2
  1. What do you observe?
  2. How large is the text now?
Stop button red ex
Stop the running program.


Let's add a rectangle around the text. Resume emacs and add the following lines so that the entire function appears as:

    func setup(canvas:Canvas) {
        let text = Text(location:Point(x:50, y:50), text:"Hello, World!")
        text.font = "30pt Arial"
        canvas.render(text)

        let rect = Rect(topLeft:Point(x:20, y:10), size:Size(width:300, height:50))
        let rectangle = Rectangle(rect:rect, fillMode:.stroke)
        canvas.render(rectangle)
    }

Remember to save the file, then suspend emacs.

Start button green arrow
Run the program and refresh the browser page.
ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 3
  1. What do you observe?
  2. What is the purpose of the location parameter in the Text constructor?
  3. What is the corresponding parameter named in the Rectangle?
  4. What is the difference between a Rect and a Rectangle? Hint: Are you able to render a Rect?
Stop button red ex
Stop the running program.

Exercises[edit]

ExercisesExercisesIcon.png

Making use of your accumulated knowledge to date (loops, functions, coding style, etc.) and using only Rectangles and Text on a single canvas:

  1. Draw a single rectangle (without any text)
  2. Draw a grid (at least 3 x 3)
  3. Draw a brick wall (at least 5 x 5)
  4. Draw a pyramid constructed of bricks (at least seven rows high)
  5. Draw a skyscraper with a large sign on the roof containing your name
  6. Draw at least three skyscrapers of different heights on the same Canvas, each containing your name