CSci 101: Lab 1

Objectives

The objective of this lab is to get familiar with your programming environment. By the completion of this assignment, you should be able to:

Description

The following is a series of steps that will familiarize you with editing, debugging, compiling and running a program in the Linux environment. All text in italics is what you must type at the command prompt.
  1. Log in to your Linux account. Your username should be the first initial of your first name followed by your last name. Your initial password should be the first 8 digits of your social security number. If you signed in to the class late, we'll have to create an account during the lab.

  2. Click (once) on the "command line" icon so you can give linux commands. The first to learn is how to change your password. At the prompt, enter yppasswd (which stands for "yellow pages password"). You'll need to enter your old password and your new password twice. Remember your new password.

  3. Create a subdirectory (i.e., folder) in your main directory for your labs and change to that directory. Use the linux commands: md labs and cd labs. The first one creates the the directory (md stands for make directory), and the second one changes your default directory to it.

  4. Find this page on the web: Click on either the "Communicator Browser" (Netscape) or the "Konqueror Web Browser" to get a browser window. Go to the class directory http://babbage.clarku.edu/~cs101/, and click on "Labs" in the menu on the left to get the class Lab Sessions page, then on "Lab 1" to get here.

  5. Download the file SavitchIn.java and save it in your labs directory. (Right click your mouse with "save link as" on the link to save the file.)

  6. Compile your copy of SavitchIn.java. In the command line window enter javac SavitchIn.java After compiling this file, you should see the byte code file "SavitchIn.class" in your current directory. The linux command to see what's in your directory is ls

  7. Now to create a program. There are various editors you can use. On the lower left of your screen is a "K" in front of the screen; it's gives you a menu of available programs. Under "editors" you'll see "advanced editor", "kate", and "emacs", among others. Select Kate. ( You could use another but Kate's nice.) You'll get a window with parts, the one on the right being an editor window. Even though you haven't entered any text, save the file in your labs directory with the name lab1.java. Now enter the following program and save it when you're finished. Type it in as is with the exception of filling in the comment section at the top with appropriate values (your name, login ID, etc.) This source code does contain a couple of intended syntax errors, but leave them in for now. You might accidentally create another syntax error if you make a typing mistake (which is easy to do, of course), but try not to.
    /******************************************************************
     *
     *   lab1.java: My first Java Program
     *
     *   Name:     <your name>
     *   Login:    <your login id>
     *   Course:   CSci 101
     *   Section:  <your section>
     *   Date:     <todays date>
     *
     *   Description:
     *     This program reads in a length in feet from the keyboard
     *   and converts the length to a number of inches, which is
     *   output to the console screen.  It is assumed that the
     *   input length is a positive integer.
     *
     *******************************************************************/
    
    public class lab1 {
    
       public static void main(String args[])
       {
          int foot;
          int inches;
          System.out.print("\n  Enter length in feet: ");
          foot = SavitchIn.readLineInt();
    
          /* this is the conversion
          inches = 12 / foot
    
          System.out.print("  " + foot + " feet is equal to ");
          System.out.println(inches + " inches.");
       }
    }
    

  8. Use the javac compiler to compile your file. Linux command: javac lab1.java

  9. Your program probably did not compile. This is because there were syntax errors in your program. The compiler does not necessarily list all of the errors, so be careful!

    In your program there are two (intended) syntax errors. Note that the compiler actually only lists one of the errors. On line 26, the comment is not terminated, i.e., it is missing a "*/" . As a first step, fix your file by putting the terminating "*/" at the end of line 26.

  10. Save the file and re-compile.

  11. Now, instead of no errors you get a different one! In this case, you are missing a ";" (semicolon) at the end of line 26. So, edit your file and fix the error. Then save the file and re-compile

  12. This time your program compiled (unless you added a error as you typed it in). There should now be 2 byte code files: SavitchIn.class and lab1.class.

  13. Run your program by typing java lab1 on the command line at the Linux prompt. The program will prompt you for a length in feet. Try it out. Does this give you the right answer? Probably not. There is a semantic (that is, a run-time) error in your program, meaning the program is working, but it is calculating something incorrectly. Semantic errors are generally harder to find and fix than syntax errors.

    In your program, to convert feet to inches, you need to multiply feet by 12. Instead, if you look at the code, the program divides feet by 12 instead. To fix this, change the line from:
      inches = foot / 12;
    to:
      inches = foot * 12;


  14. Save and recompile your program. Then execute your program and confirm that you are now getting correct results.

  15. You're all done! Print the source code and the run of the program; hand them in. Go home and study.