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.
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.
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.
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.
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.
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)
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
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.
Activity2.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.