CSCI 200

Laboratory Exercise 1

Goal

The goal of this exercise is to gain experience with the following:

There is no design writeup for this lab.

Compiling and Runing Java Programs:

The Java environment is already set up on aludra, but the default Java is not Java 1.5. In addition, Java apparently has some problems on Solaris that requires lots of virtual memory (you'll learn about virtual memory in CS 402). You must do the following to get Java to work properly.

  1. After logging in, type the command 'limit'. You will see lots of lines of text. One of the lines will either be memorysize, or vmemoryuse. Remember this, as you will need this below.
  2. Edit your .cshrc file to add the following line:
        source /usr/usc/jdk/1.5.0_06/setup.csh
    
    
    If you saw memorysize for a limit, also add the following line:
        limit memorysize 2000000
    
    
    If you saw vmemoryuse for a limit, add the following line:
        limit vmemoryuse 2000000
    
  3. Exit from emacs and enter this Unix command:
        source .cshrc
    
    
  4. Lastly, if you saw vmemoryuse, then you must use the following java compile statement:
        javac.real -J-Xmx64m MYJAVASOURCEFILE
    
    

The normal compile for java is 'javac MYCLASSFILE.java'. This will produce the byte code file. To run the bytecode type "java FILENAME" (not "java FILENAME.class" or "java FILENAME.java"). Note that FILENAME must match the name of the class defined in the file!

You can compile multiple files at once with "javac FILE1.java FILE2.java" or compile all files in a directory with at once with "javac *.java".

Grading

The lab exercise is worth 10 points.

Java Programs for You to Compile and Debug

There are 8 programs for you to compile and debug. The are each worth 0.5 points. For each of these programs you are to debug it, then run it to demonstrate to the TA that you successfully resolved all the problems. I tell you where the errors are for the first 3 programs. Tell you how many errors there are in the fourth program and give you a hint, but you're on your own on the last three. Good luck!

The program source code is below, you do not need to type this in by hand, if you use emacs, you can copy and paste the code into emacs. To copy the code, just select it with the mouse, then click in your emacs window and hit CTRL-Y to paste it into emacs.

Program 1

Program 2

Program 3

Program 4

Program 5

Program 6

Program 7

Program 8

Practice with Strings (2 points)

Suppose that s1, s2, s3, and s4 are four strings, given as follows:
    String s1 = "Welcome to Java";
    String s2 = s1;
    String s3 = new String("Welcome to Java");
    String s4 = s3.intern();

What are the results of the following expressions - prove this by writing the code?

s1 == s2
s1 == s3
s1.equals(s2)
s2.equals(s3)
s1.compareTo(s2)
s2.compareTo(s3)
s1 == s4
s1.charAt(0)
s1.indexOf('j')
s1.indexOf("to");
s1.substring(5,11);
s1.toLowerCase();
s1.toUpperCase();
"    Welcome      ".trim();
s1.substring(5)

Practice Converting Strings to Numbers and Numbers to String (2 points)