|
Bill Teter email: william.teter@plattsburgh.edu |
|
|
lab 1 Objectives:
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: 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: 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: 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){ 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){ |