An OOP Example ACO 101: Intro to CS
Engineer the illusion of simplicity
Abstraction: Turner “about atmospherics, not finicky topographical description” How awful is the silence of the waste Where Nature lift* her mountains to the sky; Majestic solitude, behold the tower, Where hopeless Owen, long imprison'd, pined And wrung his hands for liberty in vain
about atmospherics
Review: Object Oriented Abstraction : aka Entity Abstraction
Review: Identifying Classes During design, you need to identify the classes. Classes are used to model things: Tangible things in the design: nouns, such as – purse, mailbox, bank account, keypad, message Users – Student, Administrator, Bank Manager Agents ("-er" or "-or" words) – Scanner, file reader, sentence reader, CSVreader – agents perform some operation (sentence reader) Events and Transactions – Button click, ATM Withdrawal, Register for Course are all events – transactions are uses for interactions between classes, usually contain info that doesn't "belong" in a thing class
See the big picture
Recognize the pieces
Identify the modules and abstract
Step 1 Take the time to read and understand the problem. Look up stuff in the API you don’t understand Make a sequence diagram to try to understand what you need to do – a sequence diagram is a way to make a picture of the solution
Launch labrat Don’t forget to run the bat file [at school only] and pick the JDK.
The problem: draw a house
Implement a class house and supply a method draw
Supply a method draw(Graphics 2D g2)
draw(Graphics 2D What does this tell us? If you are not sure what this means look it up in the API pi/index.html pi/index.html
Graphics 2D in the API look it has a method called draw
Soo…. We are supposed to supply a method called draw And one of the objects we are required to use has a draw method – The draw method of the Graphics 2D class is used to: “Strokes the outline of a Shape using the settings of the current Graphics2D context.” Huh. – Who talks like that?
Take a moment to understand Strokes the outline of a Shape using the settings of the current Graphics2D context. – Strokes the outline of a Shape = draw an outline of a shape Shape = what shape have we seen so far? – Rectangle
Take a second moment to understand Strokes the outline of a Shape using the settings of the current Graphics2D context. – What alien planet talks like that? Is that where Shakespeare is from? – What makes sense? Graphics 2D – Lets go look at it again in the problem
Supply a method draw(Graphics 2D g2) Hmmm… the Graphics 2D object is being passed into the method draw that I am supposed to supply. – using the settings of the current Graphics2D context – Looks like the current context is g2 And g2 will have a method called draw
The draw method of the Graphics 2D class has a Shape parameter
huh Soooo…. If I pass in a shape to the draw method of the Graphics 2D object – will it draw it?
There are shapes in that there drawin’
Ok so let me see if I grock this I gotta write a program that will draw shapes in the configuration of a house using the Graphics 2D object. – This is the big picture – about atmospherics, not finicky topographical description So back to the code snippet [now we need to be finicky]
Make a new frame and set it up, oh, I need a piece of paper [all the stuff in the main method is code that they gave me]
Make a new frame and set it up, I need a piece of paper
Make a house component and add it to the frame
Make the frame visible
Ok so I get the main method On to the house component next. Look at the code snippet –
HouseComponent extends JComponent
What does it mean to extend? this is part of what you will learn in 102; but for now you can google terms you don’t understand
Tell me please, what is JComponent? Where can I find that answer? It is a class in the Java Library Where do you find the java library documentation?
JComponent has fields
It has a constructor and methods
Hummpf… I am stumped I'm going back to the code.
Point at which people get lost because they don’t slow down and take notice: HouseComponent has a method called paint component – since it extends JComponent I wonder if JComponent has a method of the same name
And it does; so now we know what the HouseComponent method will do. It will draw the UI.
How does paint component get called? When a window becomes visible (uncovered or deminimized) or is resized, the "system" automatically calls the paintComponent() method for all areas of the screen that have to be redrawn. – lowlevel/graphics/15who-calls- paintcomponent.html lowlevel/graphics/15who-calls- paintcomponent.html Back to the code – how did Wiley Plus override the method?
Make a graphics 2D object, make a house object and draw
What the whole sequence looks like
What is happening inside draw?
a series of steps to draw the house You need an algorithm
Review: the Ways to break it down (abstract the problem domain) Procedurally = algorithms – Things happen when subprograms are called and statements are executed Object-oriented = classes and objects – Things happen when objects are operated on Logic = goals – Things happen based on what need to happen next to attain a goal Rule based = if then rules – Things happen when events cause new rules to fire Constraint = invariant relationships – Things happening is dependent on what you can’t do.
Your first Algorithm An algorithm is a step sequence. It has 3 characteristics – Unambiguous = precise instructions for what to do at each step and where to go next – Executable = can be carried out in practice – Terminating = it will end You can describe an algorithm with pseudocode
Everyday Algorithms
Pseudocode is an informal way of saying what a program is going to do there are no real rules – there is no right or wrong way
Pseudocode for the algorithm for draw Create a new Rectangle for the window Create a new Rectangle for the door Create a new Rectangle for the outline Create a new Line for the roof on the left side Create a new Line for the roof on the right side Draw the window Draw the door Draw the outline Draw the roof left side Draw the roof right side
Now that you have a plan It is time to code – Use the sequence diagram and the pseudocode to help you code
Download the files
Here are the files
Open the house class the ellipses […] indicate where you need to code
The comments are telling you what you need. You have 2 parameters [one for x and one for y] and you need to assign them to the left corner coordinate and the bottom corner
So what do I need for the coordinates? The parameters are already there but the coordinates are not…. The coordinates are what an the house object knows about itself. What part of an object holds the state again?
Fields hold the state
And then what do we do with The fields in the constructor???
Assign them
The constructor and the fields are done It is time for the draw method
What goes in here? Consult your pseudocode
Pseudocode for the algorithm for draw Create a new Rectangle for the window Create a new Rectangle for the door Create a new Rectangle for the outline Create a new Line for the roof on the left side Create a new Line for the roof on the right side Draw the window Draw the door Draw the outline Draw the roof left side Draw the roof right side
Create rectangles
Draw rectangles
The lines are up to you Look up Line2D.Double in the API and figure out how to create and draw them on your own. This is an in class assignment so you can discuss it all you want to outside of class.
Homework Finish Wiley plus More rectangles Start on your first program: rubric posted