Skip to main content

Section 2.31 Unit 2A Projects

These projects were created by Leigh Anne Fitz and Lisa Ferran to provide engaging, standards-aligned learning experiences for AP Computer Science A students.
Now that you’ve learned how to represent data and build simple algorithms, it’s time to make your programs think. In these projects, you’ll explore how selection statements allow programs to make decisions by evaluating conditions. You’ll write algorithms that use Boolean expressions, relational and logical operators, and conditional statements to control the flow of execution.
Each project is designed to reinforce these concepts through authentic problem-solving tasks. As you complete them, focus on developing clear algorithms, testing multiple scenarios, and thinking carefully about how different inputs affect the decisions your program makes. Strong programmers don’t just write codeβ€”they design algorithms that make the right decision every time.

Subsection 2.31.1 Review: if and else if Statements

Selection statements allow a program to make decisions based on whether a condition is true or false.
  • Use an if statement to execute code only when a condition is true.
  • Use an else if statement to check another condition if the previous condition was false.
  • Use an else statement to provide a default action when none of the previous conditions are true.
Conditions are evaluated from top to bottom, and only the first condition that is true will execute.

Activity 2.31.1.

What is printed when the following code executes?
int temperature = 72;

if (temperature > 80)
{
    System.out.println("Hot"); 
}
else if (temperature >= 70)
{
    System.out.println("Warm");
}
else
{
    System.out.println("Cold");
}
  • Hot
  • Check whether 72 is greater than 80. Remember that > means strictly greater than.
  • Warm
  • The first condition, temperature > 80, is false because 72 is not greater than 80. The program then checks the next condition, temperature >= 70, which is true because 72 is greater than or equal to 70. Since this condition is true, the program prints Warm and skips the remaining else block.
  • Cold
  • The else block only executes if all previous conditions are false. Is temperature >= 70 true or false?
  • Nothing is printed
  • One branch of an if/else if/else statement will always execute when an else is present.

Subsection 2.31.2 Unit 2A Project 1 - Concert Tickets

Write a program that displays the price of a concert ticket. The concert ticket price is based on the seat location, as shown in the following table (use them as ints, not doubles). The user should be able to enter the seat location using either an uppercase (B, P, or L) or a lowercase (b, p, or l) letter.
Hint: use toLowerCase() or toUpperCase() on the variable or use OR statements.
Make sure the price in your output has a $ in front of it.
Seat location       Concert ticket price ($)
B (box)             $75
P (pavillion)       $30
L (lawn)            $21
If the user enters any other seat location, the program should display an β€œInvalid location” message.

Activity 2.31.2.

Write a program that displays the price of a concert ticket. The concert ticket price is based on the seat location, as shown in the following table (use them as ints, not doubles). The user should be able to enter the seat location using either an uppercase (B, P, or L) or a lowercase (b, p, or l) letter.

Subsection 2.31.3 Using Math.random() to Generate Random Integers

The Math.random() method generates a random decimal greater than or equal to 0.0 and less than 1.0.
To generate a random integer in a specific range, use this formula:
(int)(Math.random() * (max - min + 1)) + min
Where:
  • min is the smallest possible value.
  • max is the largest possible value.
  • (max - min + 1) is the number of possible integers in the range.
Generate a random integer from 1 to 6: int roll = (int)(Math.random() * 6) + 1;
Generate a random integer from 10 to 20: int num = (int)(Math.random() * 11) + 10;

Activity 2.31.3.

Which statement correctly generates a random integer from 25 to 40, inclusive?
  • (int)(Math.random() * 15) + 25
  • Count how many integers are in the range 25-40, including both endpoints. Is it 15 or 16?
  • (int)(Math.random() * 25) + 16
  • The number added after Math.random() should be the minimum value in the range.
  • (int)(Math.random() * 16) + 25
  • To generate integers from 25 to 40: Minimum = 25, Maximum = 40, Number of possible values = 40 - 25 + 1 = 16. Substitute these into the formula: (int)(Math.random( ) * 16) + 25. This produces every integer from 25 through 40, with each value equally likely.
  • (int)(Math.random() * 40) + 25
  • The multiplier should be the size of the range, not the maximum value.

Subsection 2.31.4 Unit 2A Project 2 - Lottery

Write a Java program that picks a lottery number for the Virginia PowerBall Lottery game. In this lottery, the computer randomly chooses 5 numbers between 1 and 59 inclusive and a power ball number between 1 and 39 inclusive.
Add code to allow the user to enter their numbers, one value at a time. Then compare the user choices with the winning combination. For every matching number, the person wins $50, except for the PowerBall match, in which they win $500. Output the user’s numbers, the winning numbers, and any prize money modeled after the examples below. (Note: the user’s choice must match in the same order to win)
Example 1:
The winning number combination is:	
5	22	1	45	55	Power Ball: 25

Your number combination is:	
8	22	3	12	30	Power Ball: 25

Congratulations!  Your Prize is $550
Example 2:
The winning number combination is:	
8	14	9	32	15	Power Ball: 33

Your number combination is:	
14	26	34	42	51	Power Ball: 19

Sorry you did not win.

Activity 2.31.4.

Write a Java program that picks a lottery number for the Virginia PowerBall Lottery game. In this lottery, the computer randomly chooses 5 numbers between 1 and 59 inclusive and a power ball number between 1 and 39 inclusive.
Add code to allow the user to enter their numbers, one value at a time. Then compare the user choices with the winning combination. For every matching number the person wins $50 dollars, with the exception of the PowerBall match in which they win $500. Output the user’s numbers, the winning numbers, and any prize money modeled after the examples above.
You have attempted of activities on this page.