CS 101 Laboratory #2
An Introduction to Java

Objective: To learn to use the DrJava IDE and to write some basic Java programs.

Due date is the beginning of your lab section next week:

Lab prep: Read this lab assignment.

Written Exercises: Read CS (Brookshear) chapter 1 through section 1.5, write up these exercises, and turn them in along with your lab work.

  1. Convert each of the following three binary numbers to its equivalent decimal (base 10) number: 1010, 01011101, and 1000011010.
  2. Convert each of the following three decimal numbers to its equivalent binary (base 2) number: 67, 253, and 1234.
  3. Look for the lowest-priced 2 GB RAM memory card (for any computer) and the lowest-priced 500 GB internal hard drive that you can find on the web (new, not used). Give the price and store's URL (e.g., www.amazon.com) for each of these. Compute the price per gigabyte for each of these. Based on price per gigabyte, how many times more expensive is RAM than hard disk space?
  4. Assuming no compression, show how to compute how many megabytes (MB) it would take to represent a 24-bit picture with 1366 x 768 resolution.

This lab will introduce you to the DrJava IDE (integrated development environment) and to programming using the Java language. You are encouraged to discuss the assignment with other students, but you must write your own code. At the top of the first page of your submission, write your own name and the names of the students with whom you discussed the assignment. (Some of the exercises below are from the Reges and Stepp text.)

For each program that you write, be sure to include comments at the beginning indicating your name, your lab section, the exercise number, and what the program does. Please turn in a printout of your Java source file for each program that you write, and please make sure all pages are stapled together.

  1. Start DrJava by clicking on the white "J" icon in the dock. In the Definitions pane, type in the text for Hello.java. We'd like you to save this program file (and all others you write for this assignment) to a new place instead of your public_html folder. To do this, click on the "Save" button. In the drop down menu towards the top, select "home". Then select your user name, which should have a house icon next to it. Click on the "New Folder" button, and name your new folder cs101. Inside your cs101 folder, create another new folder named hw02. Finally, save your file in the hw02 folder as Hello.java. In the future, you won't need to create another cs101 folder, but you should create new folders inside cs101 named hw03, hw04, etc., for each new assignment.

    Click on the "Compile" button, and assuming there are no syntax errors, click on the "Run" button to see the text output in the Interactions pane below. (If there are syntax errors, you will need to correct them and recompile.) Change the program to output a text string of your choice, recompile, and rerun to make sure you changed the program properly. Turn in a printout of your revised Hello.java program.

  2. Write a Java program called Syntax.java that outputs the text below, and turn in a printout of your program.
    A well-formed Java program has 
    a main method with { and } 
    braces. 
    
    A System.out.println statement 
    has ( and ) and usually a 
    String that starts and ends 
    with a " character. 
    (But we type \" instead!)
    
  3. Consider the OldMacDonald.java program that we started writing during Lecture 3. Modify the program so that it prints the song lyrics below, and turn in a printout of your program. Besides the main method, you should write a separate printVerse method that takes two parameters, an animal name and its associated sound. You may also want to write another method to print out "E-I-E-I-O". As much as possible, try to avoid repetition of identical or similar statements in your program.
    Old MacDonald had a farm
    E-I-E-I-O
    And on his farm he had some chicks
    E-I-E-I-O
    With a cluck-cluck here
    And a cluck-cluck there
    Here a cluck, there a cluck
    Everywhere a cluck-cluck
    Old MacDonald had a farm
    E-I-E-I-O
    
    Old MacDonald had a farm
    E-I-E-I-O
    And on his farm he had some cows
    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
    
    Old MacDonald had a farm
    E-I-E-I-O
    And on his farm he had some sheep
    E-I-E-I-O
    With a baa-baa here
    And a baa-baa there
    Here a baa, there a baa
    Everywhere a baa-baa
    Old MacDonald had a farm
    E-I-E-I-O
    

  4. Extra credit: Some people celebrate the Christmas holiday by singing "The Twelve Days of Christmas." Write a program named TwelveDays.java that outputs the complete lyrics for this song. Try to make your program as concise as possible. Here are the first two verses and the last (12th) verse of the song:
    On the first day of Christmas, 
    my true love sent to me 
    a partridge in a pear tree. 
    
    On the second day of Christmas, 
    my true love sent to me 
    two turtle doves, and 
    a partridge in a pear tree. 
    
    ... 
    
    On the twelfth day of Christmas, 
    my true love sent to me 
    Twelve drummers drumming, 
    eleven pipers piping, 
    ten lords a-leaping, 
    nine ladies dancing, 
    eight maids a-milking, 
    seven swans a-swimming, 
    six geese a-laying, 
    five gold rings; 
    four calling birds, 
    three French hens, 
    two turtle doves, and 
    a partridge in a pear tree. 
    
  5. About how long did it take you to complete this assignment?


Back to Computer Science 101 Home
Department of Computer Science