Except where otherwise noted, the contents of this document are Copyright 2019 Stuart Reges and Marty Stepp.
Goals for this problem set:
if/else
statements to select between multiple code actionsif/else
statementsif/else
StatementsAn if/else statement lets your program choose between 2 or more options.
if (test) { statement(s); } else { statement(s); }
Example:
if (gpa >= 2.0) { System.out.println("Welcome to Mars University!"); } else { System.out.println("Please apply again soon."); }
if
/else
mysteryConsider the following Java code. Fill in the boxes with the output produced by each of the method calls.
public static void mystery(int n) { System.out.print(n + " "); if (n > 10) { n = n / 2; } else { n = n + 7; } if (n * 2 < 25) { n = n + 10; } System.out.println(n); } |
|
Write a method named numUnique
that accepts three integers as
parameters and that returns the number of unique integers among the three.
For example, the call numUnique(18, 3, 4)
should return 3
because the parameters have 3 different values. By contrast, the
call numUnique(6, 7, 6)
would return 2 because there are only
2 unique numbers among the three parameters: 6 and 7.
Compare your solution to your neighbors'. Did you all solve it the same way?
Write a method quadrant
that accepts a pair of real numbers x and y and returns the quadrant for that point:
For example, quadrant(-2.3, 14.2)
returns 2. If the point falls directly on either axis, return 0.
if
/else
factoringif
/else
. For example:
if (x < 30) { a = 2; x++; System.out.println("CS is awesome! " + x); } else { a = 2; System.out.println("CS is awesome! " + x); }
else
went away!)
a = 2; if (x < 30) { x++; } System.out.println("CS is awesome! " + x);
if
/else
Factoringmain
and run it to make sure it works properly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class IfOops { public static void main(String[] args) { int a = 7, b = 42; minimum(a, b); if {smaller = a} { System.out.println("a is the smallest!"); } } public static void minimum(int a, int b) { // returns which int is smaller if (a < b) { int smaller = a; } else (a => b) { int smaller = b; } return int smaller; } } |
if
statement should use ()
parentheses, not {}
brackets=
should be ==
smaller
is out of scope herevoid
should be int
=>
should be >=
(or better
yet, no if
test is needed)int
when
returning itint smaller
is out of scope here (declare
outside if
or return directly)public class IfOops { public static void main(String[] args) { int a = 7, b = 42; int smaller = minimum(a, b); if (smaller == a) { System.out.println("a is the smallest!"); } } public staticvoidint minimum(int a, int b) { // returns which int is smaller int smaller; if (a < b) {intsmaller = a; } elseif (a >= b){intsmaller = b; } returnintsmaller; } }
public class AgeCheck { public static void main(String[] args) { int myAge = 19; // I am 19; let me see if I can drive message(myAge); } // Displays message about driving to user based on given age public static void message(int age) { if (myAge >= 16) { System.out.println("I'm old enough to drive!"); } if (myAge <= 16) { System.out.println("Not old enough yet... :*("); } } }
if
and else
in a clumsy way.
Improve the style of the code.
public class AgeCheck { public static void main(String[] args) { int myAge = 19; // I am 19; let me see if I can drive message(myAge); } // Displays a message about driving to user based on given age public static void message(int age) { if (age >= 16) { System.out.println("I'm old enough to drive!"); } else { System.out.println("Not old enough yet... :*("); } } }
AgeCheck
program's message
method with:
// Possibly prints some message(s) to the user based on the given age public static void message(int age) { if (age >= 21) { System.out.println("I can legally purchase alcohol!"); } else if (age >= 17) { System.out.println("I can purchase a ticket to an R-rated movie."); } else if (age >= 16) { System.out.println("I can get my driver's license!"); } }
if
s
and else
s in this method to behave properly.
You're thinking about going with your friends to a
movie. Write a Java method seeMovie
that
accepts two parameters: the cost of a ticket in dollars,
and the rating number of stars the movie received out of
5. The method should print how interested you are (very, sort-of, or not).
Use the following criteria: