Download presentation
Presentation is loading. Please wait.
Published byOlivia Malone Modified over 9 years ago
1
An OOP Example ACO 101: Intro to CS
2
Engineer the illusion of simplicity
3
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
4
about atmospherics
5
Review: Object Oriented Abstraction : aka Entity Abstraction
6
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, email 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
7
See the big picture
8
Recognize the pieces
9
Identify the modules and abstract
10
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
11
Launch labrat Don’t forget to run the bat file [at school only] and pick the JDK.
12
The problem: draw a house
13
Implement a class house and supply a method draw
14
Supply a method draw(Graphics 2D g2)
15
draw(Graphics 2D What does this tell us? If you are not sure what this means look it up in the API http://download.oracle.com/javase/7/docs/a pi/index.html http://download.oracle.com/javase/7/docs/a pi/index.html
16
Graphics 2D in the API look it has a method called draw
17
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?
18
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
19
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
20
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
21
The draw method of the Graphics 2D class has a Shape parameter
22
huh Soooo…. If I pass in a shape to the draw method of the Graphics 2D object – will it draw it?
23
There are shapes in that there drawin’
24
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]
25
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]
26
Make a new frame and set it up, I need a piece of paper
27
Make a house component and add it to the frame
29
Make the frame visible
31
Ok so I get the main method On to the house component next. Look at the code snippet –
32
HouseComponent extends JComponent
33
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
34
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?
35
JComponent has fields
36
It has a constructor and methods
37
Hummpf… I am stumped I'm going back to the code.
38
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
39
And it does; so now we know what the HouseComponent method will do. It will draw the UI.
40
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. – http://leepoint.net/notes-java/GUI- lowlevel/graphics/15who-calls- paintcomponent.html http://leepoint.net/notes-java/GUI- lowlevel/graphics/15who-calls- paintcomponent.html Back to the code – how did Wiley Plus override the method?
41
Make a graphics 2D object, make a house object and draw
43
What the whole sequence looks like
44
What is happening inside draw?
45
a series of steps to draw the house You need an algorithm
46
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.
47
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
48
Everyday Algorithms
49
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
50
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
51
Now that you have a plan It is time to code – Use the sequence diagram and the pseudocode to help you code
52
Download the files
53
Here are the files
54
Open the house class the ellipses […] indicate where you need to code
55
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
56
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?
57
Fields hold the state
58
And then what do we do with The fields in the constructor???
59
Assign them
60
The constructor and the fields are done It is time for the draw method
61
What goes in here? Consult your pseudocode
62
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
63
Create rectangles
64
Draw rectangles
65
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.
66
Homework Finish Wiley plus More rectangles Start on your first program: rubric posted
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.