2017年7月21日 星期五

CS106A - Programming Methodology - Lecture 5

"Variables" like a box store something
name name of the box...?
type what sores in the box
value what actually in the box





name: need to starts with a letter or an underscore
could be upper or lower case
after you can have any letters, number, or underscore
also could not be the same with the special names in java ex. class
good name would be representative, descriptive

types: primitive type
int (as integer) (I thought it's initial....)
double real-value (實數)
so there's an association called IEEE they set up the rule for names in computers,
and "double" is for double position real number, remember that computers only understand 0s and 1s
boolean logic (true or false) (布林運算)
char (character)

how much versus how many
how many: integer
how much: double

int x;
x is the name of the integer box

int x=3;
now we know the value in the box is 3

or like
double y=5.2;

type name=value;

public void run() {

    have those decorations only inside method "run"

}


int x;
x=3;

this is also the same effect

"assign the value"
variable=expression;

int total=10;
total=total+1;
for a mathematician they might get crazy because this is not gonna work (unless it's infinite?
but this is just for assignment, not really means equal

the "+" plus sign is not the same meaning in math as well
(see the Add2Interger program)

Glabel
GRect
GOval
GLine

they are subclass for GObject (and GObject is a class, not an object...)

public void run() {
    GLabel label = new GLabel("hello, world", 100, 75);
    ....
    add(label);
}

here the GLabel is a type (as a class name) and label is the name of the box
we assign the value of the GLabel box called label as the following things after "="

label.setFont();
objectname.methods();

this is different from Karel cause Karel is the only object in Karel's world so we don't need to specify the name of the object. but here in java, there are many objects so we need to specify the name of the objects we want to do something with.

And here the object we are working on is called "receiver"
the method that's being called is called the "message"

everything is measured in "pixels"




GObject class

object.setColor(color)
object.setLocation(x, y)
object.move(dx, dy)


java.awt package
ex. Color.BLACK


Constructor as a factory
new GLabel (text, x, y)

Methods specefic to the GLabbel class
label.setFont(font)
"family-style-size"

new GRect (x, y, width, height)
new GOval(x, y, width, height)
new GLine(x0, y0, x1, y1)

methods shared by GRect and GOval
object.setFilled(fill)
use the actual words of "true" or "false"
object.setFillColor(color)
the setcolor method is for the whole thing, no matter the line or the fill part

two methods have no receiver (objects)
getWidth()
getHeight()
get you the width and height for the graphics window

FunGraphics program


operator
+, -, *, /, %(remainder)(餘數)

7%2  ->1
7%20 ->7


沒有留言:

張貼留言