CAST pretend some type into another type
double y = 3.5;
int x = (int) y;
you are losing information by the process, so you HAVE to do the cast here.
However, in this case:
int x = 5;
double y = (double) x;
actually you can just do:
double y = x;
because in java if you don't lose any information by the process, you don't have to cast it.
"while loop" indefinite loop: you don't know how many times you are going to do something.
(not infinite, that would be forever)
Loop and a half?!
SENTINEL
Yeah I already learnt it from the homework... again...
In computer science, try any possible ways to avoid duplicated code!
That's why the solution online use "break;" cause this avoid the duplication.
private static final int SENTINEL = 0;
public void run() {
int total = 0;
while (true) {
int val = readInt("Enter val: ")
if (val == SENTINEL) break;
total += val;
}
prinln("Total = " + total);
}
This provide you a way to avoid duplication of codes and get out from the while loop in the middle of the statements.
NOTE that it is bad to make multiple breaks inside a loop because that would make the code hard to define which condition is true and where to jump out from the loop.
for versus while loop
for (init; test; step) {
statements
}
init
while (test) {
statements
step
}
These two situation is totally the same.
but in for loop, that's a definite iteration,
generally we know how many times
in while we have no idea how many times so we use a test try
checkerboard program
finished in advanced (yoshi
CONSTANTs are defined in class but not in particular methods.
"Methods"
like
readInt();
but in java, you will have a parameter, and a value returned.
which is not in Karel (in Karel only move(); turnLeft();...)
import java.lang.Math;
double x = 9.5;
double y = Math.sqrt(x);
here the x is the input (parameter) and y is the returned value.
power function: multiple parameters
double z = Math.pow(x, y);
means z is the value of x^y
method is something that is general enough to be used over and over again
receiver.name(arguments);
"define a method in java"
visibility type name(parameters) {
...body...
}
visibility: private or public
type: type of the return value
SO in Karel, your methods don't return any value, and you give a type called void(空的)
name: just a name ;p
parameters: ALSO, in Karel you don't have parameters, that's why it's empty inside.
"return" to get the return values!!
return ...expression...;
ex. feet to inches
private double feetToInches(double feet) {
return 12*feet;
}
the parameters also have type and name
multiple return statements
private int max(int val1, int val2) {
if (val1 > val2) {
return (val1);
} else {
return (val2);
}
}
this returns different results from different conditions.
you can have as much parameters you want, each of them are separated by comma and with types and names for them.
Predicate Methods
fancy name for a method that returns Boolean results
private boolean isOdd(int x) {
return ((x % 2) == 1);
}
A factorial program example (階乘)
a variable in a method has nothing to do with other variables in the public method called it.
the variable in the method only live inside the method.
Returning objects example
filledCircle program
you can set a new parameter different from the GOval
and fill in the color somthing blahblahblah
沒有留言:
張貼留言