Introduction to Java using NetBeans
Section 5 : Decisions, Decisions
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.
Reminder: Always use == instead of = in an if statement condition. Using = will cause an error and generate a warning message.
The if..else if Statement
Assignment 5.2 - Printing Prices
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
Hint: Start with the 1000 and work your way down using else if. Do NOT use compound if statements.
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
Enter the number of copies to be printed: 1001
Price per copy is: $0.25
Total cost is: $250.25
No Above and Beyond Required
Assignment 5.3 - Guessing Game
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
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.
In the Math class, there is a method that you can use to find the absolute (positive) value of a number. You will need to use this method to help you determine how far the guess is from the secret number. This will allow you to determine which message you should report for a hint.
Sample code below:
int intNum = -12; // Number
int intAbsolute = Math.abs(intNum); // Absoute Value of the Number
System.out.println(intNum); // output: -12
System.out.println(intAbsolute); // output: 12
No Above and Beyond Required