ASSIGNMENT 0 - Hello World
The goal of this assignment is to introduce you to how to program in Java.
You will learn how to write, compile and run your very own program.
You will become familiar with the basic features of the Eclipse IDE.
Part 1: Hello world!
As is traditional, your first Java program will be a Hello world program.
In this part, you will develop a program HelloWorld.java that prints out "Hello world!".
If you are working from home or on your own laptop, first setup Java and the Eclipse IDE as described on the resources page.
Since this is your first time using Eclipse, we'll show you step-by-step what to do in Eclipse.
1) Start by launching Eclipse.
On lab machines, you'll have to change the location of the Eclipse workspace to be somewhere in your own home directory.
The workspace is the folder which Eclipse stores the programs you'll write, so make a note of the folder's location.
2) Select File -> New -> Java Project. Give the project a name such as "Assignment0". Press the Finish button.
Note: we recommend selecting "Use project folder as root for sources and class files" as this will simplify your life once we start working on the command line in a few assignments.
Note: we recommend you avoid spaces or other crazy non-alphanumeric characters in project names, it will only cause you pain.
3) The previous step only creates an empty project with no place to put code.
We now need to create a source file in our Assignment0 project.
Select File -> New -> Java Class.
Give the class the name HelloWorld. Note the case. In programming generally case matters, so pay close attention!
4) You can now start adding code to your HelloWorld.java file.
In the screenshot below, note how I've added the required header comments at the top.
We require headers on every source file you submit in the course so get use to it.
You'd be surprised how quickly in real-world programming you forget what a program you wrote does.
Providing a good descriptive header is a good habit.
It also helps us keep track of things during grading.
5) Once you finish typing in your program, you can compile and run you program by pressing the leftmost green play button in the toolbar.
You can do the same thing by selecting the first option in the Run menu.
If all goes well you should see the output of your program in the console tab the bottom of the Eclipse window:
Part 2: Greetings
Create a program Greetings.java that takes four command line arguments.
The first command line argument is a general greeting word such as "Hello" or "Greetings".
The second, third, and fourth command line arguments are the names of three people.
The program outputs three lines of text.
Each line of text has the greeting word, followed by a space, followed by the name of one of the three people, followed by an exclamation mark.
Here is an example output given the input "Hello Keith Justin Joy":
A key difference of this program versus HelloWorld is the output of Greetings depends on the input.
So running the exact same program but with the different input "Hola Alice Bob Carol" results in the output:
How do I specify command line arguments in Eclipse?
For reasons no one understands, in order to specify the command line arguments in Eclipse, you must first run your program without any arguments.
So run your program using the green play button, or by selecting Run from the Run menu.
If your program makes use of args, ..., args, it may crash with the error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException.
That's okay just ignore it.
You can now actually add the command line arguments for the Greetings Java program by going to Run -> Run Configurations....
Select the (x)= Arguments tab and type the arguments in the top box.
Here is a screenshot of what you'd put in to run the first example above:
Note: be sure to select the Greetings program in the left area, select the correct tab, and enter the command line arguments into the text box.
The four arguments can be on a single line separated by a space, or you can put them on separate lines; it doesn't really matter.
Do I need to create a new project for my Greetings program?
No, you can have multiple Java source files in the same project.
Whatever Java file is highlighted in the Package Explorer on the left side of Eclipse is the program that will be executed when you hit the run button.
We only require you submit the Java source files, so how your organize your workspace and projects is up to you.
But we recommend having a separate project for each assignment in the course.
How do I get at the command line arguments in Java?
Check out LoveHate.java, LoveHate2.java, LoveHate3.java, and LoveHate4.java for basic examples of using command line input.
Notice that while the first three programs are quite different from each other, all three produce identical output for the same input.
Just like in English writing, there is more than one way to express the same thing.
We'll talk more about code style next week.
When I run my program I get the message Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException, what is going on?
Make sure you added four words as command line arguments in your Run Configuration.
Also remember that we count from zero in computer science.
So the first command line argument is args, the second is args, and so on.
In this program, there are only four command line arguments, so using args is a bad idea!
My program's output isn't quite the same as yours for the example input above. Is that okay?
Learning to program involves a lot of attention to detail.
You should strive to exactly match our example outputs.
There will be plenty of opportunity for creativity in future assignments and extra-credit levels.
The assignment page is the "contract", sticking to the exact program specification will make grading easier for the TA, and that is good for all involved!
I accidently named my program Greeting or greetings or ... Is that okay?
No. This will break our grading script. This is a surefire way to make your TA grumpy.
Plus it is really easy to fix.
Right click on the Java source file in the package explore, select Refactor -> Rename, and give it the correct name.
What should my program do if the user provides less than four command line arguments?
Your program can just crash with an error.
You do NOT need to provide error handling in this assignment.
You can assume the user will provide exactly four command line arguments.
Part 3: Stairway
Create a program Stairway.java that calculates the total number of steps in a building's stairway as well as the total vertical height of the stairway.
The program takes three command line arguments as input:
So for example, for the input "10 13 0.75" your program would print:
- Number of flights of stairs in the building, given as an integer (e.g. 10).
- Number of steps per flight, given as as integer (e.g. 13).
- Vertical height of a step in feet, given as a floating-point number (e.g. 0.75).
For the input "5 21 0.81" your program would print:
Total steps: 130
Total height in feet: 97.5
How do I read an integer or floating-point command line arguments?
The user's input, args, ..., args, comes into your program as text (a variable of type String).
You cannot perform mathematical calculations using String variables.
So you must first convert the first two command line arguments to int variables, and the third arguments into a double variable.
For an example of how this is done, see ArgsExample.
Total steps: 105
Total height in feet: 85.05000000000001
What is with the crazy formatting of the total vertical height in the output for certain input values?
This results from the way Java represents an infinite precision floating-point value in a finite amount of memory.
We'll eventually learn how to format floating-point numbers nicely, but for now we'll just live with it.
Why can't I just represent the number of flights or steps per flight as a double instead of an int?
Technically the program would still work, but in your programs it is best for a variable to use a data type that best reflects what it can hold.
Our program isn't intended to support building under construction with partial flights of stairs, or flights that contain partial steps.
If we used double variables, someone reading our program might think that such things were supported.
Additionally, as we will see, int variables allow us to exactly check things such as if the building has 10 flights of stairs.
Such checks are trickier with double variables.
Submit all three programs HelloWorld.java, Greetings.java, and Stairway.java.
Be sure each submitted source file has the required header with your name, email address, and a description of the program.
Also be sure to submit the .java source files and not the .class files!
Page last updated: February 01, 2015