[an error occurred while processing this directive]
Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Goals for today:
return
values to send data between methodsif
, else if
and else
to have
different branches of executionScanner
to create interactive programs that read user
inputThe scores you receive on labs are available for you to view in Canvas. You should be able to view your scores here. If that link doesn't work, you can check your scores by:
You earn 1 point for each lab that you attend/show up on time for. Receiving credit for CSE 190 requires attending 8-10 labs. You should see scores for lab01 and lab02.
If you believe there is a problem with one of your scores, please email (none) at shtiuif@cs.washington.edu.
A return value is information that a method gives back to the code that called it. Every method can only return one value: when a method returns, it stops executing (and we resume where we left off before the method was called). For the code that called the method to use the returned value, the returned value must be stored in a variable or used immediately.
public static type methodName(parameters) { // calling methodName returns expression
...
return expression;
}
type variableName = methodName(parameters); // variableName stores return value
Method | Return | Example |
---|---|---|
Math.abs
|
absolute value |
Math.abs(-308) returns 308
|
Math.ceil
|
ceiling (rounds upward) |
Math.ceil(2.13) returns 3.0
|
Math.floor
|
floor (rounds downward) |
Math.floor(2.93) returns 2.0
|
Math.max
|
max of two values |
Math.max(45, 207) returns 207
|
Math.min
|
min of two values |
Math.min(3.8, 2.75) returns 2.75
|
Math.pow
|
power |
Math.pow(3, 4) returns 81.0
|
Math.round
|
round to nearest integer |
Math.round(2.718) returns 3
|
Math.sqrt
|
square root |
Math.sqrt(81) returns 9.0
|
Scanner
Method name | Description |
---|---|
nextInt()
|
reads the next token, returns it as an int , if possible
|
nextDouble()
|
reads the next token, returns it as double , if possible
|
next()
|
reads the next token, returns it as a String
|
nextLine()
|
reads an entire line, returns it as a String
|
Example: import java.util.*; // so you can use Scanner ... Scanner console = new Scanner(System.in); // "System.in" = scanner reads from the console System.out.print("How old are you? "); // prompt int age = console.nextInt(); // reads from the console System.out.println("You typed " + age);
A cumulative algorithm involves incrementally accumulating a value by repeatedly adding, multiplying, dividing, etc., while storing the result in a variable.
Key aspects of a cumulative algorithm: A loop, and a variable declared outside the loop whose value is modified inside the loop.
Example: Cumulative algorithm to sum the numbers 1-100:int sum = 0; // safe default value, 0 doesn't affect a sum for (int i = 1; i <= 100; i++) { sum = sum + i; } System.out.println(sum); // 5050
Conditional
StatementsConditional statements lets your program choose to do different things based on the results of tests, which evaluate certain parts of a program. There are 3 structures that we can use:
if
statements
else
statements
else if
statements
if
statementsif
statements are composed of a test, and some code to execute if that test is true (ex. 2 + 2 = 4 is true).
if (test) { statement(s); // executes if test is true } System.out.println("yay!") // prints yay, business as usual outside the if statement
Example:
if (gpa >= 2.0) {
System.out.println("Welcome to Mars University!"); // perform this code is gpa >= 2.0
}
if
statements
1 2 3 4 5 |
x = 5; if (x < 0) { System.out.println("inside if branch!"); } System.out.println("outside if branch!"); |
Output: outside if branch! |
if
statements
1 2 3 4 5 |
x = -2; if (x < 0) { System.out.println("inside if branch!"); } System.out.println("outside if branch!"); |
Output: inside if branch! outside if branch! |
if
statements
1 2 3 4 5 6 7 8 |
x = -2; if (x < 0) { System.out.println("inside if branch!"); } if (x < -1) { System.out.println("inside this branch!"); } System.out.println("outside if branch!"); |
Output: inside if branch! inside this branch! outside if branch! |
Note: it's possible for any if
statement to execute, or not. If you have a couple if
statements next to each other, 0 of them, 1 of them, or both of them could execute.
if/else
statementsif/else
statements are statements that include two branches, one of which is always executed. They allow us to control our program's behavior when the if
statement's test evaluates to false.
if (test) {
statement
} else { // implicitly, test is false
statement
}
Example:
if (gpa >= 2.0) { System.out.println("Welcome to Mars University!"); // perform this code is gpa >= 2.0 } else { System.out.println("Please apply again soon."); // perform this code is gpa < 2.0 }
if-else
practice
1 2 3 4 5 6 7 8 9 |
public static void main(String[] args) { int x = 5; if (x < 5) { System.out.println("abc"); } else { System.out.println("def"); } System.out.println("ghi"); } |
Output: def ghi |
if-else
practice
1 2 3 4 5 6 7 8 9 |
public static void main(String[] args) { int x = 2; if (x < 5) { System.out.println("abc"); } else { System.out.println("def"); } System.out.println("ghi"); } |
Output: abc ghi |
Note: With if-else
structures, exactly one branch will execute. It is impossible for neither the if
branch or the else
branch to execute. It is also impossible for both to execute.
if/else
statementsNested if/else
statements allow you to write code that executes if its test is met, but only in the case that an if
statement before it has already evaluated to false.
if (test1) { statement // executes if test1 == true } else if (test2) { statement // executes if test1 -= false AND test 2 == true }Example:
if (gpa >= 2.0) { System.out.println("Welcome to Mars University!"); // perform this code is gpa >= 2.0 } else if (gpa < 0.7) System.out.println(":*("); // perform this code when gpa < 2.0 and < 0.7 }
if
/else
factoringif/else
, exactly 1 branch executes. This make a new kind of redundancy possible!
if (x < 30) { a = 2; x++; System.out.println("CSE 142 TAs are awesome! " + x); } else { a = 2; x--; System.out.println("CSE 142 TAs are awesome! " + x); }Because the red code will happen no matter what (in the
if
and the else
case), it can be factored out:
a = 2; if (x < 30) { x++; } else { x--; } System.out.println("CSE 142 TAs are awesome! " + x);
If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.
You can view an exercise, type a solution, and submit it to see if you have solved it correctly.
Choose some problems from the book and try to solve them!