Home CPSC 240

Lab 1: Java on the Command Line

 

Objective

To solve a simple programming problem with Java, and to become comfortable compiling and running Java programs on the command line.


 

Compiling Java Programs

To compile a Java program, we use the command javac followed by the name of the source code file. For example, if there is a program called "Hello.java", then the following command would compile it:

$ javac Hello.java

This only compiles the program. The compiler translates your Java source code into bytecode. It doesn't actually run your program. You can use the ls command to see that this command makes a file called "Hello.class":

$ ls
Hello.java  Hello.class

In order to run the program, we'll need to use the java command, followed by the name of main class of the program. For programs with just one file, that means the name of the file with no ".java" at the end. For the example above, it would look like this:

$ java Hello
Hello World!

This will take the compiled .class file, and actually execute it, doing whatever the program is supposed to do.


 

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.


 

Details


 

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

To submit your file, you will need to copy it from your virtual machine to your own computer so that you can send it to me. The easiest way to do that is to transfer it with a file transfer program.

The instructions here show you how to do that with FileZilla.

When you have your .java file on your own computer, email it to ifinlay@umw.edu.

Copyright © 2019 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.