Today, you'll get your hands on basic Java concepts like data types, string methods, and conditionals through a fun and simple coffee-themed exercise.
Imagine you're helping to program a digital display for a coffee shop. This display shows different messages based on the coffee type.
You will create a simple Java program that prints a message for each type of coffee. This will help you understand how to use strings and conditionals.
- Data Types: Use the
String
data type to store types of coffee. - String Methods: Use string methods to manipulate and display text.
- Conditionals: Use if-else statements to decide what message to print based on the coffee type.
- Create a variable to store the type of coffee. Assume the coffee type is "Latte" for now.
- Use an if-else statement to check the type of coffee:
- If it's "Espresso", print a message saying it's strong.
- If it's "Americano", print a message noting it's a classic choice.
- If it's "Latte", print a message about it being creamy and delicious.
- Try changing the
coffeeType
variable to different options and see what messages print. - Think about how you might use string methods like
toLowerCase()
ortoUpperCase()
to make the input more flexible.
Learn to use arithmetic operators to calculate the strength of the coffee based on user preferences.
- Define variables for coffee base (amount of coffee in grams) and water ratio.
- Calculate the strength by dividing the coffee base by the water ratio.
- Print out the strength with a suitable message.
Use relational operators to warn the user if the coffee might be too hot to drink immediately.
- Define a variable for the temperature of the coffee.
- Use an if-else statement to check if the temperature is above a certain threshold and warn the user.
Learn to use the addition and multiplication operators to estimate the price of a coffee order.
- Define variables for the number of coffees ordered and the price per cup.
- Calculate the total cost and print it.
Learn to use a for
loop to iterate through an array of coffee orders and count how many of each type there are.
- Define an array of strings where each element is a type of coffee ordered by customers.
- Use a
for
loop to iterate through the array and count the number of each type of coffee. - Print the counts for Espresso, Latte, and Americano.
Use a while
loop to simulate checking a coffee machine’s stock and remind to replenish if stock goes below a certain threshold.
- Start with a certain number of coffee pods.
- Use a
while
loop to decrease the number of coffee pods as they are used, one at a time. - Print a reminder to replenish the pods when only 2 remain.
Demonstrate a do-while
loop by printing each type of coffee until the end of a list is reached.
- Define an array of coffee types like Espresso, Americano, Latte, and Cappuccino.
- Use a
do-while
loop to print each type until you've gone through the list.
Learn to use a for-each
loop to iterate through an array of coffee additives and print a message for each.
- Define an array of additives such as "vanilla", "caramel", "cinnamon".
- Use a
for-each
loop to iterate through the array and suggest adding each to a coffee to enhance the flavor.