Lab 1: IntelliJ and Java
Objective
To solve a simple programming problem with Java, and to make sure you have
a working Java environment with IntelliJ.
Setting up IntelliJ
- Download and install IntelliJ Community edition onto your system.
If you have a mac, you will need to determine if you need the "Intel" or
"Apple Silicon" version. Open "About this Mac" and you look under where
it says "Chip". If it Apple, get Apple Silicon and if it says Intel, get
the Intel version.
- When you open the program, you should see a screen like this:
Click "New Project".
- Now you should see a window like this:
Under Project SDK, wither choose an existing Java SDK (if one shows up), or choose
Download JDK. The default selections (currently OpenJDK 17) should be fine, so
click download.
- When it finishes downloading, click "Next". Then click next again, using no template.
- Give the project whatever name you like, maybe "lab1".
- Now, we need to add a .java file. To do this, right click on "src" in the project tree
to the right, and choose "New -> Java Class". Name it something, maybe "Lab1".
- To get things running, just put in a basic "Hello World" program, like this:
- To run it, choose "Run -> Run..." from the menu. Then click "Lab 1"
in the popup window.
- You should now see the output in the terminal at the bottom of the screen:
Task
Your task for this lab is to write a program to generate the lyrics to the
song "99 Bottles of Beer". However, your program should read in the starting
number of bottles the user wants in the song, it won't always be 99.
Also, you should read in the user's favorite beverage to use in the lyrics,
instead of just using "beer" — some users may not be 21 (or indeed like
beer).
Details
- Ask the user how many bottles the song should start with. You can assume the user will enter a number 2 or greater.
- Next, ask the user to enter their favorite beverage.
- Write a loop to go through each verse of the song. Inside the loop, you
should print one verse, making sure to get the numbers right.
- The last verse (when there are 0 bottles) follows a different pattern than
the rest, so make sure to handle that case.
- Also, be sure to handle the plurality right. For example, you should not
print "1 bottles" during the song.
Example Run
Here is an example run of the program, so you can see how the program should work,
and to see the lyrics to the song in case you're not familiar with them:
How many bottles to start with? 5
What is your favorite beverage? iced tea
5 bottles of iced tea on the wall, 5 bottles of iced tea.
Take one down and pass it around, 4 bottles of iced tea on the wall.
4 bottles of iced tea on the wall, 4 bottles of iced tea.
Take one down and pass it around, 3 bottles of iced tea on the wall.
3 bottles of iced tea on the wall, 3 bottles of iced tea.
Take one down and pass it around, 2 bottles of iced tea on the wall.
2 bottles of iced tea on the wall, 2 bottles of iced tea.
Take one down and pass it around, 1 bottle of iced tea on the wall.
1 bottle of iced tea on the wall, 1 bottle of iced tea.
Take one down and pass it around, 0 bottles of iced tea on the wall.
0 bottles of iced tea on the wall, 0 bottles of iced tea.
Go to the store and buy some more, 5 bottles of iced tea on the wall.
Submitting
When you are finished, please submit the .java file (and not the .class or other files)
for the lab on Canvas.
Solution
Bottles.java provides an example solution for this lab.
Copyright ©
2024
Ian Finlayson | Licensed under a Creative Commons BY-NC-SA 4.0 License.