Download 09F-NoteSet09b

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSE 1341 Honors
Professor Mark Fontenot
Southern Methodist University
Note Set 09
More Methods
public static void main (String [] args) {
int val = getSecretNum(25, 8);
System.out.println(val);
val = getSecretNum(12, 3);
System.out.println(val);
val = getSecretNum(5, 10);
System.out.println(val);
}
public int getSecretNum(int a, int b) {
if (a == 3){
Only one of the return
return b + 2;
statements in a method will
} else if (a > 10 && a < 20) {
execute.
return b + 4;
} else if (a >= 20) {
A return stmt can only return
return b + 6;
one value.
} else {
return b + 8;
}
}
More Methods
public static void main (String [] args) {
int val = getSecretNum(25, 8);
System.out.println(val);
val = getSecretNum(12, 3);
System.out.println(val);
val = getSecretNum(5, 10);
System.out.println(val);
}
public int getSecretNum(int a, int b) {
if (a == 3){
Remember: Both conditions
return b + 2;
must be true for the
} else if (a > 10 && a < 20) {
entire expression to be true.
return b + 4;
} else if (a >= 20) {
return b + 6;
} else {
return b + 8;
}
}
More Methods
public static void main (String [] args) {
int val = getSecretNum(25, 8);
System.out.println(val);
val = getSecretNum(12, 3);
System.out.println(val);
val = getSecretNum(5, 10);
System.out.println(val);
}
public int getSecretNum(int a, int b) {
if (a == 3){
return b + 2;
Remember: Both conditions
} else if (a > 10 && a < 20) {
must be true for the
return b + 4;
entire expression to be true.
} else if (a >= 20) {
return b + 6;
} else {
return b + 8;
}
}
More Methods
public static void main (String [] args) {
int val = getSecretNum(25, 8);
System.out.println(val);
val = getSecretNum(12, 3);
System.out.println(val);
val = getSecretNum(5, 10);
System.out.println(val);
}
public int getSecretNum(int a, int b) {
if (a == 3){
return b + 2;
} else if (a > 10 && a < 20) {
return b + 4;
} else if (a >= 20) {
return b + 6;
} else {
return b + 8;
}
}
Output:
More Than One Method
public class TestClass {
public static int method1(int total) {
return total + 10;
}
public static int method2(int t) {
return t * 2;
}
public static void main (String [] args) {
System.out.println(method2(10));
System.out.println(“Hello “ + method1(500));
System.out.println(method1(method2(10)));
}
}
More Than One Method
public class TestClass {
public static int method1(int total) {
return total + 10;
}
public static int method2(int t) {
return t * 2;
}
public static void main (String [] args) {
System.out.println(method2(10));
System.out.println(“Hello “ + method1(500));
System.out.println(method1(method2(10)));
}
}
Note: You can embed a method call inside
another method call.
Random Number Generation
• Java Random object can generate
(pseudo)random numbers.
• Based on a function with very long period.
public static void main (String[] args) {
Random r = new Random();
r is a Random object that can
generate pseudorandom numbers
System.out.println(r.nextInt());
System.out.println(r.nextInt());
int x = r.nextInt(50);
}
nextInt is a method in Random
that returns a pseudorandom integer.
If you send an argument to nextInt,
the number it returns will be between 0 (incl)
and argument’s value (excl). [0 – 49] in this case
Related documents