For this assignment, we will write a simple Java program in the DrJava environment. You should look at the program Lect121a.java that was written in lecture on 1/21. This program shows the basics of input, calculations and output which you will need to do for this program.
When doing Java Programming,it is very important to follow a basic Java Template. Below is a file called Template.java
/** * Class for creating a template for a simple Java program * * @author Pat Troy: troy AT uic DOT edu */ // Note the name of the class in the following line MUST // match the name of the file. This is stored in a file // named: Template.java public class Template { public static void main (String[] args) { System.out.println("Begin Java Exection"); System.out.println(""); // put your Java Program here System.out.println(""); System.out.println("End Java Exection"); } } // end of Template class
To use the code provided with the text book, you need to place the bookClasses directory in a writable space on the computer. The bookClasses directory is available on the disk provided with the book or in this zip file: bookClasses10-1-07.zip or at bookClasses-7-22-09.zip
To use the bookClasses, you will need to add an extra classpath to refer to the bookClasses directory. If you click on the Edit menu item and DrJava and then select Preferences, you will open the Preference's window. Click on Add below the Extra Classpath textarea and specify the location of the bookClasses directory. This information is also discuss in Chapter 2.2 of the text book (don't forget you can find a PDF file of the first 4 chapters from the main web page for the course).
~i101/setupDrJavaOnly enter the above command once.
After the bookClasses directory has been set up, enter the following command to start DrJava:
~i101/runDrJavaOnce DrJava is up and running, it should work the same as it has on the ACCC's windows machine. Actually is should work better. At least, it will run faster.
The common UNIX commands that you may need for this lab are:
End your connection to UNIX. It is very important to disconnect when you are finished so no one else can come along after you and use your account.
List the contents of the current directory. Note: a UNIX directory is the same as a Windows folder.
The ls command has a special usage that will give more information about the files in the current directory. This is the long listing. This uses the -l option to the ls command:
ls -lThe first thing given on the long listing is the permissions for each file. This is important when making web pages viewable on the internet.
Allows the contents of file in <filename> to be changed. The editor pico is the counter part to the email reader pine. The ACCC's web page combine these two programs together under the name of pine. For a web page with a description of pico and how to use it, check out the North Carolina State Pico Page or the University of South Dakota Pico Page.
Change to the directory given in <directoryName>. This command is normally used to change to a sub-directory of the current directory (change one level down in the hierarchical file system).
The cd command has two special usages. These are:
By just typing in cd, you will change back to your Home directory from where ever you are at in the hierarchical file system. Your Home directory is the directory you are in when you first log onto the UNIX system.
This command will move you up one level in the hierarchical file system. The directory that is one level up from the current directory is referred to as the parent directory of the current directory.
This command allows the user to change the password to log onto the CS account. It is strongly recommended to change your password from the default password of your UIN.
Display the contents of the file indicated by <filename> to the screen.
Display the contents of the file indicated by <filename> to the screen one page at a time.
Creates a duplicate of the file in <filename1> with the name <filename2>.
Renames the file in <filename1> with the name <filename2>.
Deletes the file in <filename> from the current directory. Note: once a file is deleted is cannot be restored. So use this command with caution!
Sends the file in <filename> to be printed at the printer with name <printerName>. This command also has a ACCC web page describing its use. Note: The lpr command will assume the file is stored using the ASCII character format (i.e. plain text). So don't try to print files that are not plain text.
The actual command stands for Present Working Directory. This command will list the full path name of the current directory. Each slash in the full path name represents one level in the hierarchical file system.
List the contents of the current directory. Note: a UNIX directory is the same as a Windows folder.
This command will create a new directory with the name indicated by <directoryName> as a sub-directory of the current directory.
This command will remove/erase the directory with the name indicated by <directoryName> that is an existing sub-directory of the current directory. Note, the sub-directory must be empty (contain no files or other directories) before it can be removed.
The Secret of 42
You are to write a program that will always produce the result of 42. This is not really a useful thing, except you can maybe cause someone to think that you can read their mind. This type of trick is sometimes called a calculator problem. The idea was that the person was to be given a calculator, told to do pick some number they wished to use, and told to perform various operations using that number. At some point in time the person was told what number would be on the calculator. Thus trying to make the person think that their mind had just been read.
Note the program only has one value it needs to read in from the user (this is done in step 2). The other steps are with just print statements (steps 1 and 8) or print statments with some calculations (steps 3 through 7). Be sure to display the various results as we work our way through the problem.
Submission of the Lab
The lab must be submitted electronically to the Digital Drop Box inside of Blackboard. Obviously, this needs to be to the Digital Drop Box for the CS 101 course. You will only need to submit the java source code file. You are to name your file with both the lab number and your NetID so we can find it easier. For someone with the NetId of tsmith14 the file should be named:
Lab2tsmith14.java