2017年8月22日 星期二

CS106A - Programming Methodology - Lecture 10

// I have been very super extremely busy in past few weeks... any way I finally finished lecture 10 and even a bouncing ball program!!


We have constructors in a class (for initiating), and we have methods, what we are going to do something with some other things (what am I talking about?).





And at last, we have the private instance variables, in the example, we have student name and student ID, and units earned. Every methods have their own copy of the variables, such as Delphi, 9781024 and 180. You don't give the variables a final value, so it is able to be changed during methods, but all the methods are in the same class. Conversely, the units to graduate is the same to all students, that is, all methods, so it will have a final version, it is a class variable.

in Karel, we have done things like this:

public class JumpingKarel extends SuperKarel {

So here you should understand that the Karel program you write is actually a subclass of class SuperKarel or Karel, and we use some property or what those classes can do to do more things.

then we will have a constuctor, initialize the class and sort of define what this class is doing.
(by the way we don't have this part in Karel you know)
and here you might be slightly different from the big class since a subclass has all the characters the original class have, and has even more.

Overriding a method

if you create a new method with the same name as the other one in the other class, it will always call the one in the current subclass. but the old method is still exist in it's own class, and also be able to be access by other subclasses (if they don't have their own versions).

And here you will understand that when we deal with public and private, it is quite important when you have another subclass created. the subclass should of course have some access to something but not all of the things. if you don't want it to be changed or accessed directly, you should make it private.

GCanvas

stacking order, z orderGraphicsProgram automatically creates GCanvas that fills the entire program window
the add method is actually being forwarded to GCanvas but not GraphicsProgram itself to do it.

GCanvas does:

remove
removeAll
getElementAt, null
getWidth
getHeight
setBackground(color)

only GraphicsProgram does:

pause(millisecond)
waitForClick()

For GObjects:

setLocaation
move
getX
getY
getWidth
getHeight
contains, if the objects contain the specific point, return true or false
setColor
getColor
setVisible, useful when you don't want to change the z order
isVisible, return if the object is visible or not.
sendToBack
sendToFront
sendFoward
sendBackword

Interface
set of methods
set of classes that have that set of methods
just like GObject and GLabel, GRect but without the hierarchy relationship

GFillable
setFilled
isFilled
setFilledColor
getFilledColor

GResizable
setSize
setBounds

GScalable
scale

Glabel

baseline
height
ascent
decent

GArc class
start angle (從哪裡開始畫弧)
sweep angle (要畫多少度的弧)
positive - counterclockwise
negative - clockwise

fill GArc
only the pie portion of the GArc