Bill Teter
Office:          149 Redcay
Telephone:    2782
Office Hours:  Tuesday, Thursday 9:00-12:00

email:  william.teter@plattsburgh.edu

       

 

lab 1  Objectives: 
            Set up svn
            Write and run applications with input from command line args, System.in and a file.
            Create a recursive object to compute factorial.

 

0.  Set up your directory to contain working copies of code that will be committed to the svn repository.  From a terminal window execute the command

svn checkout https://server.cs.plattsburgh.edu:8443/repos/csc223/

This should create a directory for csc223 and a subdirectory for your username.  Make your username subdirectory your working directory, and in this directory create a new directory called lab1.  Change working directory to lab1.  When you are done with this lab, change directory to parent of lab1 and then execute the following commands:
    svn add lab1
    svn commit lab1 -m ""
The "svn add lab1" command will register lab1 and all its files as part of the repository.  The "svn commit lab1" will make copies of all the registered files and directories in the repository. When you do a commit you have the opportunity to attach a message to the committed files.  the -m "" part of the commit command indicates that no message will be attached.  If you leave this out you will be expected to write a message in whatever is the default text editor of the system you are using.   A directory or file should only be added once.  So, if you have already added the directory and want to add another file in that directory called Horse.java then, from that directory do:
    svn add Horse.java

1.  Create a java class called Fact with one property of type long called num and one method (behavior) called value() that is the factorial of num.  There will be one constructor with a parameter of type long.  The value() method should be written recursively with Fact objects as I explained in lab.

2.  Write a program called Fact1 which prints the factorial of its first command line argument.  Run this program with the command line:
        java Fact1 4
The output should be 24.  This program will use the Fact class that you wrote in 1.

3.  Write a program called Fact2 that prompts the user to enter a number and then prints out the factorial of that number.

4.  Create a file called data that contains numbers, one number per line.  Write a program called Fact3 that opens the file called data, reads each line of the data file, and prints the factorial of each of the numbers encountered on its own line.

5.  Write a program called Fact4 which is just like Fact3 except that the name of the data file will be passed as a command line argument.  So, you would run this program with:
        java Fact4 data

Background

(for 2)   Remember that the args array is an array of strings.  You will need to convert args[0] from a String to a long.  You can do this with the static method Long.parseLong(args[0]).  See the Long class on the java api.

(for 3)  To open the keyboard input stream you will need to import classes from java.io and use a try/catch block.  Here is a program called echo that will prompt user to enter a line and then echo it back.

 

import java.io.*;

public class Echo{

    public static void main(String[] args){
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        try{
            System.out.print("Enter a line:   ");
            String line = input.readLine();
            System.out.println(line);
        }
        catch(IOException e){
        }
   }
}

System.in is java's name for the keyboard.  Look in the java API in the BufferedReader class to see the available methods for a BufferedReader object like input, including readLine().

(for 4)  To read from a file is like reading from the keyboard except a FileReader is used instead of an InputStreamReader and the FileReader object must be created in the try block so an exception can be thrown and caught if the file does not exist.  Here is a Echo program that reads from a file called data which is then echoes.

mport java.io.*;

public class Echo{

    public static void main(String[] args){
       String line;
        try{
            BufferedReader input = new BufferedReader(new FileReader("data"));
            line = input.readLine();
            while(line != null){
                System.out.println(line);
                line = input.readLine();
            }
        }
        catch(IOException e){
        }
   }
}