Chapter 03 Lab

1. IF Condition Types

1.1 Program #1

You are creating a program for a fast food company. The customer shall provide her first name (so the employee can call you when the order is ready) and then choose a meal by its number, such as 1, 2, and so on. Table 1 below show what each meal number contains. Create a program that requests the user to provide her name and a number via JOptionPane dialog boxes and your program shall print out (also via JOPtionPane) the customer name, meal number, and what the meal has.

Notes:

a) if the customer types an invalid number, such as 5, your program must print an error message and abort execution.

b) Search over the internet on “how to check if a string is a number in java” and look at means of checking on what the user typed can be converted to an integer number. Use what you learned from this search and update your code so that it does not crash when the customer types “one” instead of “1”. It should provide a meaningful error message and abort execution.

 

Meal Number Meal contents
1 Double cheese burger, fries, and soda
2 Chicken nuggets, orange juice, apple pie
3 Salad and water

 

c) Try entering a space before or after a valid option, such as “ 1” (space at the beginning of the number 1), and see what happens. How can you trim this input, i.e., remove spaces before and after the number?  Search for it in the internet and apply to your program.

d) If the customer press OK in the JOptionPane button without entering his/her name, your program should let the customer know that the order is canceled and abort. Search on the internet how to check if a String is empty and use it in your code.

Java topics: IF-ELSE_IF structure, boolean variables, boolean expressions, methods that return boolean values, input validation, trimming inputs, ways of entering data into your program (JOptionPane).

 

1.2 Program #2 – Logical Operators

Your manager has asked you to create a program that categorizes customer ratings from 0 to 10 into three groups: poor, average, great. Table 2 gives the ranges for each group. Create a program that prints in the terminal window the category of a given input number (via keyboard).

Minimum Value Maximum Value Category
0 3 Poor
4 7 Average
8 10 Great

 

Java topics: IF-ELSE-IF structure, boolean variables, boolean expressions, methods that return boolean values, input validation, trimming inputs, ways of entering data into your program (keyboard), Logical operators.

 

2. Switch Structure

2.1. Program #3 – Converting an IF structure into a SWITCH one

Re-write program #1 above converting the meal selection IF structure into a SWITCH one.

Java Topics: SWITCH structure

 

3. Magic Numbers

3.1 Program #4

Re-write program #2 such that magic numbers are avoid in the IF statement.

Example:

Instead of writing something such as

 if(customerRating >= 0 && customerRating < 3)

create meaningful variables and use them in the IF statement such as

int lowestPoorRate = 0;
int highestPoorRate = 3;
if(customerRating >= lowestPoorRate && customerRating < highestPoorRate)

Java Topics: Magic Numbers

 

4. Number Formatting

Commerce Stores have to print purchase receipts. Some stores print them in a wide paper, that can hold about 50 characters; other stores print in a narrow paper, that can hold 30 characters. Your manager has request that you create a program that prints the receipts such as it uses all available space in the paper. Your manager also wants you to have the two decimal values for all the prices and that the prices are aligned by the decimal point. Your program shall request the size of the paper, such as 30, and it shall print the contents of Table 3 as specified above.

Item

Value
Hamburger $ 3.00
Fillet Mignon $ 25.00
Small French Fries $ 0.75

 

For a 25-character paper, your program shall print something like the following:

Hamburger            3.00
Fillet Mignon       25.00
Small French Fries   0.75

While for a 30-character paper, it would be:

Hamburger                 3.00
Fillet Mignon            25.00
Small French Fries        0.75

Hint: create String variables for each of the items above and then use String length to get how many characters it has. The DecimalFormat returns a number in a String format, so check its length also via String length and then subtract those lengths from the paper length and that is the number of spaces that you need to have in between the item and its price. Search for “how to generate x numbers of spaces in java” to generate the necessary spaces in your program.

Java Topics: String length and number formatting.