Introduction to Java using NetBeans

Section 5 : Decisions, Decisions

Note: These examples are taken from a source that does not use the naming conventions that we do. Always remember to name your variables correctly

The if..else if Statement

Example Icon Example 3



Using if .... else if to determine a range


Tips icon Tips


Always use == instead of = in an if statement condition. Using = will cause an error and generate a warning message.

Compound Boolean Expressions

Complex Boolean expressions can be created by using the Logical AND and the Logical OR operators. Logical operators take two operands and return a Boolean result.

Logical Operator Meaning Result
&& AND Evaluates to true when both operands are true.
|| OR Evaluates to true if one or both operands are true.



Example Icon Example 4


Or using .equals to compare the strings



Assignment 5.2 - Printing Prices (Gui )

Printing Prices Program Specifications

Printing prices are typically based on the number of copies to be printed.  In this example, you will create a program that prompts a user for the number of copies to print and then displays the price per copy and total price for the job.

You must use if ... else if

Printing Prices
0-99 $0.30 per copy
100-499 $0.28 per copy
500-749 $0.27 per copy
750-1000 $0.26 per copy
Over 1000 $0.25 per copy

Sample Output

Enter the number of copies to be printed:  1001

Price per copy is: $0.25

Total cost is: $250.25



Assignment 5.3 - Guessing Game (GUI)

Create a guessing game application where the computer picks a random number between 1 and 100 and lets you keep guessing until you get it right!

The guessing game algorithm is as follows:

  • Determine a secret number between 1 and 100.
  • The player enters a guess.
  • The player pushes the enter button.
  • The guess is compared to the secret number.
  • If the guess matches the secret number, a message displays, You Got It!
  • If the guess is higher than the secret number, a message displays, Too High!
  • If the guess is lower than the secret number, a message displays, Too Low!

For your program to work properly you need to declare a variable that will hold a random number outside the scope of any event. It needs to be global. If you were to generate a random number from within the button click event then each time you click that button it would create a new random number and we do not want that.

Define the random number variable at the start of the main program.This will allow it to be used throughout the program as a global variable.

Hints after the user guesses

Enrichment Icon

Display a hint to the user.

  • Freezing more than 50 away.
  • Cold more than 25 away.
  • Cool more than 15 away.
  • Warm more than 10 away.
  • Hot more than 5 away.
  • Boiling between 1 and 4 away.

Note: You will need to figure out the absolute value of the difference


Sample code below:

int numberDifference = -12;
int numAbsoluteValue = Math.abs(numberDifference);
System.out.println(numAbsoluteValue); // output: 12

Use two different conditions to produce the two different messages.

i.e. Your Guess is too low, you are freezing.

Note: You can join two messages to form one message using the concatenate operator (+) and display the concatenated message in a text field.



Assignment 5.4- Game of 21

Create a Game of 21, application to simulate a simplified version of the game "21" against the computer. A deck with cards numbered 1 through 10 is used and any number can be repeated.

The program starts by dealing the user two randomly picked cards and itself three randomly picked cards. The computer's cards are not revealed until the Check Scores button is pressed.

The user may draw additional cards or stay and check the scores

The computer will continue drawing cards from the deck until the house hand has at least 17 points, or until it goes bust by going over 21.

If both scores are over 21 or under 21 and equal, the game is declared a draw. Otherwise, the winner is the one with the highest score. If one score is over 21 and the other is 21 or less, the player with 21 or less is declard the winner. If both scores are under 21 the winner is the one with the score closest to 21.

Add a score keeper that keeps track of the number of games the player and computer have won.