CMPS 350: Lab 03 - Introduction to Java

Goal: Write, compile and execute entry-level Java programs

"Object oriented programming is a godsend for writing unmaintainable code... languages like Java go out of their way to make writing this sort of code easy." -- Roedy Green, author of How to Write Unmaintainable Code
resources:
developer's references
Rosetta Code
Java Sample Code
Java Language Specification
Java PrintStream Class
printf manpage

Java code is compiled into byte code. Byte code is not assembly or machine code but a portable low-level representation of the source that can be easily translated into machine code by the Java Virtual Machine for whatever machine the JVM is on. Byte code is thus portable. Java, unlike C++, is not a procedural language but is completely object-oriented. That means that no procedure (function) can exist outside a class. Exception handling (unlike C++) is built-in to the language. That means that exceptions will be thrown without you explicitly coding exception handling in. Finally, (unlike C/C++) there are no pointers in Java.

Step 1. If you wish to develop on a machine other than sleipnir, follow the instructions in this readme file to set your environment for the correct version of Java. You can develop on another machine but your code must run on sleipnir. To run your first Java program, copy these files into your lab03 directory:

   $ cp /home/fac/melissa/public_html/cs350-f15/Code/Java/Test.java .
   $ cp /home/fac/melissa/public_html/cs350-f15/Code/Java/FormatWriteFileApp.java .
   $ cp /home/fac/melissa/public_html/cs350-f15/Code/Java/Makefile .

Follow the instructions in README to compile and execute the Test class. Do the same with the FormatWriteFileApp class. Everything you need to know to complete this lab is somewhere in Test.java and FormatWriteFileApp.java.

Step 2. Once you know you can compile and execute the Test class, you can begin working on the Lab03 class. This class will use an existing Coin class and borrow code from the CoinGame class to create a Lab03 class. Lab03 will contain main(). In main you will simulate a series of coin tosses. Copy the Coin and the CoinGame class files into your lab03 directory.

      cp /home/fac/melissa/public_html/cs350-f15/Code/Java/Coin.java .
      cp /home/fac/melissa/public_html/cs350-f15/Code/Java/CoinGame.java .

Compile and execute the code. Because the CoinGame class includes the Coin class, compiling the CoinGame class into byte code will compile the Coin class as well. Sample session:

bash-2.03$ javac CoinGame.java
bash-2.03$ java CoinGame
Coin 1: Heads   Coin 2: Tails
Coin 1: Tails   Coin 2: Heads
Coin 1: Heads   Coin 2: Tails
Coin 1: Tails   Coin 2: Tails
Coin 1: Heads   Coin 2: Tails
Coin 1: Heads   Coin 2: Heads
Coin 1: Tails   Coin 2: Heads
Coin 1: Tails   Coin 2: Heads
And the Winner is:
Coin #2.

Create a new class called Lab03 in file Lab03.java. In the main function for the Lab03 class simulate a series of random tosses. This code is a simplified version of the CoinGame class, so you can look at CoinGame.java.

Read the number of times to toss the coin from the command line. If the user doesn't enter a command line argument, prompt the user for the number of times to toss the coin. Your game will display the face of the coin after each toss, and the totals at the end. You do not need to make any changes to the Coin class. Since Java control structures (if, for, while, etc.) are identical to C, you will be able to write lots of code without looking at any Java documentation. However, I/O and keywords pertaining to classes are not similar. Look at the Test.java file (which you also copied over) for sample code to read from stdin and write to stdout. For I/O you must include this line at the top of your file:

 import java.io.*;     

For more thorough documentation, the Java Language Specification provides a complete listing of the Java language. Sample output of the completed assignment is shown below:

bash-2.03$ javac Lab03.java
bash-2.03$ java Lab03 10
Heads
Tails
Heads
Tails
Tails
Tails
Tails
Heads
Tails
Heads
The number of tosses: 10
The number of heads: 4
The number of tails: 6

Step 3. Create a new Compute class. Each Java class needs its own compilation unit (i.e., file) thus the Compute class will need to be in file Compute.java. The Compute class will read in this data from an external file, display the data and compute the mean for each column to the screen. Exclude 0 entries from the computation. You can assume that you will have 4 columns in your input file. See Test.java (you should have copied it in Step 1) for the syntax necessary to set up a buffered input stream from an external file.

Once you get your array of lines, how do you parse the line into individual values? In Test.java look for how the 'split' method is used. Split parses a line according to a regular expression into an array of Strings. Then you can process the String objects as you walk through the array. For example, this splits the line by one or more whitespace characters:

 String[] line_values = line.split("\\s+"); 

Instantiate and call Compute from main() in Lab03 class. Use String.format() to control output passing a format string; i.e.,

       String format_str = "%-8s";  /* left justify 8 width */
       formatfile.format(format_str,"Exam" + (i + 1));

Sample output for Compute:

      Name: Santa Claus  CMPS 350 Lab03 

      Exam1   Exam2   Exam3   Exam4   
      =====   =====   =====   =====   
    	62      78      94      80        
    	54      72      74      72        
    	64      66      50      64        
    	60      78      66      72        
    	72      70      72      84        
    	66      76      84      80        
    	96      86      80      96        
    	62      64      70      56        
    	96      92      78      84        
    	100     74      0       78        
    	80      72      56      74        
    	74      82      78      94        
    	90      82      80      78        
    	78      90      92      92        
    	60      66      60      74        
    	82      96      92      96        
    	54      56      68      56        
     ======  ======  ======  ======  
Mean:  73      76      74      78      

Step 4. Once you have successfully achieved the steps above, add code to write the output from Compute() to a file instead of to the screen. Name the file out.file. Format the output into columns as shown above. See FormatWriteFileApp.java (you copied it in Step 1) for help. Keep output from Main() to the screen. Modify the Makefile to compile only the classes for lab03. I will use your Makefile to compile your code.

HOW YOUR CODE WILL BE GRADED

You do not need to send anything to me. The files for this lab must reside in this directory in your account:

  /home/stu/{username}/cs350/lab03/   

Files needed for this lab:
  Lab03.java
  Compute.java
  Coin.java
  Makefile
  in.file 

Sample run:
$ make
javac Lab03.java
javac Compute.java
$ java Lab03 
How many times do you want to toss?
5
Heads
Tails
Tails
Heads
Heads
The number of tosses: 5
The number of heads: 3
The number of tails: 2

$ cat out.file
        Name: Santa Claus CMPS 350 Lab 03 
        Exam1   Exam2   Exam3   Exam4   
        =====   =====   =====   =====   
      	62      78      94      80        
      	54      72      74      72        
      	64      66      50      64        
      	60      78      66      72        
      	72      70      72      84        
      	66      76      84      80        
      	96      86      80      96        
      	62      64      70      56        
      	96      92      78      84        
      	100     74      0       78        
      	80      72      56      74        
      	74      82      78      94        
      	90      82      80      78        
      	78      90      92      92        
      	60      66      60      74        
      	82      96      92      96        
      	54      56      68      56        
       ======  ======  ======  ======  
Mean:   73      76      74      78