Building Java Programs

Lab 4: if/else, Scanner, and return

Except where otherwise noted, the contents of this document are Copyright 2010 Stuart Reges and Marty Stepp.

lab document created by Whitaker Brand and Marty Stepp

Today's lab

Goals for today:

Exercise : Math Expressions

Write the results of each of the following expressions. Make sure to use the proper type (.0 for a double, " " for a String).

Math.pow(2, 4)
Math.floor(12.73) + Math.max(8, 5)
Math.abs(Math.min(-1, -3))
-Math.pow(2, 2) + Math.pow(-2, 3) + Math.pow(2, -2)
Math.round(4.25) + Math.round(5.38) + Math.round(6.49)

Exercise : AgeCheck

Exercise - things to fix

Exercise - answer

Exercise : AgeCheck2

Exercise : if/else mystery

Consider 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;
40 20
8 15
0 17
12 16
20 20

Exercise : area practice-it

Write a method named area that accepts the radius of a circle as a parameter and returns the area of a circle with that radius. For example, the call area(2.0) should return 12.566370614359172.

Exercise : WazzuAdmit

Write a complete program WazzuAdmit with the behavior shown below. Use the Scanner to read user input for a student's grade point average and SAT exam score. A GPA of 1.8 or an SAT score of 900 or above (or both) will cause the student to be accepted; anything less will cause him/her to be rejected.

Washington State University admission program
What is your GPA? 3.2
What is your SAT score? 1280
You were accepted!

Exercise - answer

import java.util.*;   // for Scanner

public class WazzuAdmit {
    public static void main(String[] args) {
        Scanner console = new Scanner(;
        System.out.println("Washington State University admission program");
        System.out.print("What is your GPA? ");
        double gpa = console.nextDouble();
        System.out.print("What is your SAT score? ");
        int sat = console.nextInt();
        if (gpa >= 1.8 || sat >= 900) {
            System.out.println("You were accepted!");
        } else {
            System.out.println("You were rejected!");

Exercise : Syntax errors

Exercise - answer

  1. line 5: if statement should use () parentheses, not {} brackets
  2. line 5: = should be ==
  3. line 5: smaller is out of scope here
  4. line 10: void should be int
  5. line 13: => should be >= (or better yet, no if test is needed)
  6. line 16: should not write variable's type of int when returning it
  7. line 16: int smaller is out of scope here (declare outside if or return directly)

Exercise - Corrected version

Exercise : pay practice-it

Write a method named pay that accepts a real number for a TA's salary and an integer for the number of hours the TA worked this week, and returns how much money to pay the TA. For example, the call pay(5.50, 6) should return 33.0.

The TA should receive "overtime" pay of 1 ½ normal salary for any hours above 8. For example, the call pay(4.00, 11) should return (4.00 * 8) + (6.00 * 3) or 50.0.

Exercise : seeMovie practice-it

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:

if/else Factoring

Exercise : if/else Factoring

Exercise : numUnique practice-it

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.

Exercise : pow practice-it

Write a method named pow that accepts a base and an exponent as parameters and returns the base raised to the given power. For example, the call pow(3, 4) returns 3 * 3 * 3 * 3 or 81. Do not use Math.pow in your solution; use a cumulative algorithm instead. Assume that the base and exponent are non-negative.

Exercise : smallestLargest practice-it

Write a method named smallestLargest that prompts the user to enter numbers, reads them, then prints the smallest and largest of all the numbers typed in by the user. For example:

How many numbers do you want to enter? 4
Number 1: 5
Number 2: 11
Number 3: -2
Number 4: 3
Smallest = -2
Largest = 11

If you finish them all...

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 Chapter 4 and try to solve them!