# Unit 2A Projects

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

*These projects were created by Leigh Anne Fitz and Lisa Ferran to provide engaging, standards-aligned learning experiences for AP Computer Science A students. The autograding tests were designed by Kate McDonnell.*

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.

## 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.

> Check your understanding: What is printed when the following code executes?
>
> ```java
> int temperature = 72;
>
> if (temperature > 80)
> {
>     System.out.println("Hot");
> }
> else if (temperature >= 70)
> {
>     System.out.println("Warm");
> }
> else
> {
>     System.out.println("Cold");
> }
> ```
>
> Answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/unit2a-projects.html#unit2a-project1-choose1

## 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.

### Exercise 1

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.

> Open the `01-unit2A-project1-concerttickets` folder and edit `ConcertTickets.java`. Press **Run** to try it; **Run Tests** checks it (10 tests). **Run** feeds it the input in `input.txt`; change that file to try other input.

## 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;`

> Check your understanding: Which statement correctly generates a random integer from 25 to 40, inclusive? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/unit2a-projects.html#unit2a-project2-choose1

## 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.
```

### Exercise 2

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.

> Open the `02-unit-2A-project2-lottery` folder and edit `Lottery.java`. Press **Run** to try it; **Run Tests** checks it (10 tests). **Run** feeds it the input in `input.txt`; change that file to try other input.
