Java Programming

Sagat
InstructionsforLabAssignment2.pdf

Copyright © 2022, Dallas College. All Rights reserved. Page 1 of 4

Lab Assignment 2 Decision Structures and Loops

Lab Objectives: • Be able to construct boolean expressions to evaluate given conditions • Be able to compare String objects • Be able to use a flag • Be able to construct if and if-else-if statements to perform a specific task • Be able to construct a switch statement • Be able to format output • Be able to write a while loop • Be able to write a do-while loop • Be able to write a for loop • Be able to use the Random class to generate random numbers Introduction

Up to this point, all the programs you have written had a sequence structure. This means that all statements are executed in sequence, one after another. Sometimes we want the computer to make decisions, based on the data. A decision structure allows the computer to decide which statement to execute. In order to have the computer make a decision, it needs to do a comparison. To do this, we will work with writing boolean expressions. boolean expressions use relational operators and logical operators to create a condition that can be evaluated as true or false.

Once we have a condition, we can conditionally execute statements. This means that there are statements in the program that may or may not be executed, depending on the condition. We can also chain conditional statements together to allow the computer to choose from several courses of action. We will explore this using nested if-else statements as well as a switch statement. In this lab, we will be editing a pizza ordering program. It creates a pizza ordered to the specifications that the user desires. It walks the user through ordering, giving the user choices, which the program then uses to decide how to make the pizza and how much the cost of the pizza will be. The user will also receive a $2.00 discount if his or her name is Mike or Daphne. The second group of tasks in this assignment uses the simulation of rolling six-sided dice. Actual results approach theory only when the sample size is large. So, we will need to repeat rolling the dice a large number of times (we will use 10,000). The theoretical probability of rolling doubles of a specific number is 1 out of 36 or approximately 278 out of 10,000 times that you roll the pair of dice. Since this is a simulation, the numbers will vary a little each time you run it. Section 4.11 of the text demonstrates how to generate a number between 1 and 6. We will continue to use control structures that we have already learned, while exploring control structures used for repetition. We shall also continue our work with algorithms, by translating a given algorithm into java code, in order to complete our program. We will start with a while loop, then use the same program, changing the while loop to a do-while loop, and then a for loop. Save a screen shot of the output after each successful execution.

Copyright © 2022, Dallas College. All Rights reserved. Page 2 of 4

Task #1 The if Statement, Comparing Strings, and Flags 1. Copy the file PizzaOrder.java program from the Blackboard course.

2. Compile and run PizzaOrder.java. You will be able to make selections, but at this point, you will always get a Hand-tossed pizza at a base cost of $10.99 no matter what you select. However, you will be able to choose toppings, and they should add into the price correctly. You will also notice that the output does not look like money. So, we need to edit PizzaOrder.java to complete the program so that it works correctly.

3. Construct a simple if statement. The condition will compare the String input by the user as his or her first name with the first names of the owners Mike and Daphne. Be sure that the comparison is not case sensitive.

4. If the user has entered either of the first names, set the discount flag to true. This will not affect the price at this point.

Task #2 The if-else-if Statement 1. Write an if-else-if statement that lets the computer choose which statements to execute

by the user input size (10, 12, 14, or 16). For each option, the cost needs to be set to the appropriate amount.

2. The default else of the above if-else-if statement should print a statement that the user input was not one of the choices, so a 12 inch pizza will be made. It should also set the pizza size to 12 and the cost to 10.99.

3. Compile, debug, and run. You should now be able to get correct output for the pizza size and price (it will still have Hand-tossed crust, the output won’t look like money, and no discount will be applied yet). Run your program multiple times ordering a 10, 12, 14, 16, and 17 inch pizza.

Task #3 The switch Statement 1. Write a switch statement that compares the user’s choice with the appropriate characters

(make sure that both capital letters and small letters will work). 2. Each case will assign the appropriate string indicating crust type to the crust variable. 3. The default case will print a statement that the user input was not one of the choices, so a Hand-

tossed crust will be made. 4. Compile, debug, and run. You should now be able to get crust types other than Hand-tossed.

Run your program multiple times to make sure all cases of the switch statement operate correctly.

Task #4 Using a Flag as a Condition 1. Write an if statement that uses the flag as the condition. Remember that the flag is a

boolean variable, therefore is true or false. It does not have to be compared to anything.

2. The body of the if statement should contain two statements:

a. A statement that prints a message indicating the user is eligible for a $2.00 discount. b. A statement that reduces the variable cost by 2.

Copyright © 2022, Dallas College. All Rights reserved. Page 3 of 4

3. Compile, debug, and run. Test your program using the owners’ names (both capitalized and not) as well as a different name. The discount should be displayed correctly at this time.

Task #5 Formatting Output 1. Edit the appropriate lines in the main method so that any monetary output has 2 decimal

places. Section 3.10 of the text demonstrates how to display formatted output. 2. Compile, debug, and run. Your output should be completely correct at this time, and numeric

output should look like money.

Task #6 The while Loop 1. Copy the file DiceSimulation.java from the Lab Assignment folder in the Blackboard course.

DiceSimulation.java is not complete. Since there is a large part of the program missing, the output will be incorrect if you run DiceSimulation.java.

2. All the variables have been declared. You need to add code to simulate rolling the dice and keeping track of the doubles. Convert the algorithm below into Java code and place it in the main method after the variable declarations, but before the output statements. You will be using several control structures: a while loop and an if-else-if statement nested inside another if statement. Use the indenting of the algorithm to help you decide what is included in the loop, what is included in the if statement, and what is included in the nested if-else- if statement.

3. To “roll” the dice, use the nextInt method of the random number generator to generate an integer from 1 through 6. Repeat while the number of dice rolls are less than the number of times the dice should be

rolled. Get the value of the first die by “rolling” the first die Get the value of the second die by “rolling” the second die If the value of the first die is the same as the value of the second die

If value of first die is 1 Increment the number of times snake eyes were rolled

Else if value of the first die is 2 Increment the number of times twos were rolled

Else if value of the first die is 3 Increment the number of times threes were rolled

Else if value of the first die is 4 Increment the number of times fours were rolled

Else if value of the first die is 5 Increment the number of times fives were rolled

Else if value of the first die is 6 Increment the number of times sixes were rolled

Increment the number of times the dice were rolled

4. Compile and run. You should get numbers that are somewhat close to 278 for each of the different pairs of doubles. Run it several times. You should get different results than the first time, but again it should be somewhat close to 278. Capture a screen shot of the output.

Copyright © 2022, Dallas College. All Rights reserved. Page 4 of 4

Task #7 Using Other Types of Loops 1. Change the while loop to a do-while loop. Compile and run. You should get about the

same results. Capture a screen shot of the output. 2. Change the do-while loop to a for loop. Compile and run. You should also get about the

same results. Capture a screen shot of the output.

When you submit a Java source code file that you have created or modified, include a comment line that contains your full name.

Submit the following files:

• PizzaOrder.java • DiceSimulation.java • A word processing document that is compatible with Microsoft Word containing three

screen shots. The screen shots should show the successful execution of the DiceSimulation.java program. One screen shot showing successful execution using the while loop, a second for the do-while loop, and a third for the for loop.

• Name the word processing file containing the screen shots “XYLab2.docx”, where “X” and “Y” are your first and last initials. When Bill Edwards submits Lab assignment 2, the word processing file will be named “BELab2.docx”.

  • Lab Assignment 2
  • Lab Objectives:
    • Task #1 The if Statement, Comparing Strings, and Flags
    • Task #2 The if-else-if Statement
    • Task #3 The switch Statement
    • Task #4 Using a Flag as a Condition
    • Task #5 Formatting Output
    • Task #6 The while Loop
    • Task #7 Using Other Types of Loops