Montana Tech of The University of Montana
Computer Science & Software Engineering

CSCI 135
Fundamentals of Computer Science I
Fall 2012



ASSIGNMENT #0

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!
In this part, you will develop a program HelloWorld.java that prints out "Hello world!". If you are working from home, 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. You may have to confirm the location of the Eclipse workspace. This 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.


3) 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!).


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.


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: Mad Lib   
Create a program MadLib.java that takes four command line arguments. The program uses the words from the command line arguments to fill in the slots of a Mad Lib. For example, if given the command line "Old Farm Cow Moo", your program should print:
Old Macdonald had a Farm, E-I-E-I-O
and on that Farm he had a Cow, E-I-E-I-O
with a Moo Moo here
and a Moo Moo there
here a Moo, there a Moo,
everywhere a Moo Moo,
Old Macdonald had a Farm, E-I-E-I-O
Note we added the color above just to help you to see where things go, you're output does not need to be in color! For best results, the first word should be an adjective, the second a noun, the third an animal, and the fourth a noise. Your program of course doesn't need to enforce these rules! Here is our output using "Ugly Wart Slug Hiccup"
Ugly Macdonald had a Wart, E-I-E-I-O
and on that Wart he had a Slug, E-I-E-I-O
with a Hiccup Hiccup here
and a Hiccup Hiccup there
here a Hiccup, there a Hiccup,
everywhere a Hiccup Hiccup,
Ugly Macdonald had a Wart, E-I-E-I-O
Do I need to create a new project for my MadLib 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.

How do I specify command line arguments in Eclipse? You can edit the command line arguments for a given Java program by going to Run -> Run Configurations.... Select the (x)= Arguments tab and type the arguments in the top box.

How do I get at the command line arguments in Java? Check out ArgsExample.java for an example.

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.

My program's output isn't quite the same as yours for the same input. Is that okay? Not really. Learning to program involves a lot of attention to detail. You should strive to exactly match our example outputs.


Part 3: Race Time
Create a program RaceTime.java that calculates the time difference in seconds between the winner of a 5000m track race and one of the losing runners. It also calculates the relative increase in percent the difference represents. The program's first three command line arguments are the winner's time (minutes, seconds, and hundredths of a second). The second three arguments are the loser's time (again in minutes, seconds, and hundredths of a second).

For example, assume the winning time was 12:37.35 (12 minutes, 37 seconds and 35 hundredths of a second) and the losing time was 13:08.77 (13 minutes, 8 seconds and 77 hundredths of a second). The input to your program would be "12 37 35 13 08 77". The program would output:
Absolute difference : 31.41999999999996
Relative increase   : 4.148676305539045%
For the input of "15 0 12 18 59 0", the output should be:
Absolute difference : 238.88
Relative increase   : 26.538683731058082%
What is with the crazy and inconsistent formatting of the numbers in the output? 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.

How do I calculate a relative increase in percent? Divide the difference by the winning value and multiply by 100.

Part 4: Moodle photo
Upload a recent head-and-shoulders photos of yourself to Moodle. This helps us learn your name faster.

Do I really need to use a head-and-shoulders photo? Yes. Moodle stores only very low-resolution images and we want to be able to recognize you in class from your photo.

Submission. Submit all three programs HelloWorld.java, MadLib.java, and RaceTime.java via Moodle. Be sure each submitted source file has the required header with your name, username, and a description of the program. Also be sure to upload a photo of yourself to Moodle.

Page last updated: December 26, 2012