CS401 - Intermidiate Programming Using JAVA (Recitation)

 

Lab 1: Introduction to Unix and Java Programs


Introduction

This lab will introduce Unix-commands, a simple editor to create Java programs, and how to compile and execute Java programs (applications and applets). You should be sure to master all of the material since you will use it on future assignments. Pay attention on the submission section about how and when you need to submit this lab assignment.


Unix Commands

1) In 6110 Sennott Square, login by entering your username and password.2) Right-click the mouse on the screen to see the Workspace menu choices. Choose Tools by clicking the mouse, then choose Terminal to open a "Command Window."3) To see a directory listing type at the dollar prompt,

$ ls    

To change to your private directory type,                       

$ cd  private    

Make a new directory called Java by typing,

$ mkdir  Java    

To change to the Java directory type,                       

$ cd  Java

Make three new directories called programs, documents, and labs (do you remember how?)Show a directory listing to see if these directories now exist (do you remember how?) Change to the programs directory (do you remember how?)
To make sure you are in the programs directory, type the command below to see the default or working directory (last directory should be programs),

$ pwd  

4) Now create a java program called HelloWorld.java, use the text editor pico by typing at the command,                      

$ pico  HelloWorld.java

Type the following Java application program exactly as shown

public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!!!");
}
}


To save this file, press CTRL-x, then confirm that you want to save the changes made by typing y (this will return you to the command prompt).

5) In order to compile this program type,                         

$ javac  HelloWorld.java  

If you have not made any errors, you will be returned to the command prompt. However, if you see an error message, you must correct the error. So, type

$ pico  HelloWorld.java

to re-enter the text editor.  If there are no errors or you think you have remove all errors compile the program again. Repeat until you have an error-free compilation.To see the effect of the compilation process type,                        

 $ ls

Does a file named HelloWorld.class appear in the listing? That is, do you seeHelloWorld.java    HelloWorld.class

6) To run or execute this program type,

$ java  HelloWorld  

The output should be: Hello World!!!

7) To display the file HelloWorld.java type, 

$ cat  HelloWorld.java

8) To send the output of this program to a file named output.txt rather than to the command window type,                           

$ java  HelloWorld  >   output.txt  

9) Additional Commands To change to the parent of the default directory type,

$ cd  ..

Don't forget a space after "cd". To remove HelloWorld.class from the default directory type,

$ rm  HelloWorld.class

Change to the Java directory. To remove the documents directory type,

$ rm  -r documents  

To see a calendar for 2006 type,

$ cal  2006  

To get help on a command like ls type,

$ man  ls

Change to the programs directory again. To make a copy of HelloWorld.java and name the copy HelloWorld2.java type,

$ cp  HelloWorld.java   HellowWorld2.java

To move the file HelloWorld2.java to directory labs type,

$ mv  HelloWorld2.java   ../labs


Running Applets

A large part of Java's popularity and success comes from the fact that small Java programs called Applets can be linked to HTML documents and run by web browsers. These programs are often graphical and can be used to implement Graphical User Interfaces (GUI's).
One complication of Applets is that they are not run directly. Rather, they are linked to an HTML document and run with either the appletviewer (for development and debugging), or from a web browser.
Here is the Hello World! Applet:

 
     import java.awt.* ;
     import java.applet.Applet ;
     public class Hello extends Applet {
       public void paint (Graphics page) {
         setBackground(Color.red) ;
         page.drawString("Hello World!",50,150) ;
       }
     }

Change to the programs directory before you proceed. You should put this code in the file Hello.java and compile it with javac. Note the two import statements near the beginning of the file.
Applets do not have a main() method. In simple Applets, execution starts in the init() method (if it is present), then goes to the paint() method.
Now, create a file Hello.html with the contents:

 
     <HTML>
     <HEAD><TITLE>My First Applet</TITLE></HEAD>
     <BODY>
     <HR>
     <CENTER><H1>My First Applet</H1></CENTER>
     <HR>
     <APPLET CODE="Hello.class" WIDTH=300 HEIGHT=300></APPLET>
     <HR>
     </BODY>
     </HTML>

You can run the Applet with the command:

     appletviewer Hello.html

In general, you should use the appletviewer to develop your Applets. When you are finished, you can aim a web browser at the HTML file and view the Applet across a network connection.
Notice that a network browser will display more information (headings, text, lines, etc.) than the appletviewer, which ignores everything in HTML files except the Applets.


The Graphics Class

Here is an Applet that uses some very simple graphics:

     import java.awt.* ;
     import java.applet.Applet ;
     public class DrawBox extends Applet {
       public void paint (Graphics page) {
         setBackground(Color.red) ;
         page.setColor(Color.blue) ;
         page.fillRect(10,20,50,100) ;
       }
     }

To run this you will also need an appropriate HTML file.
You should create some of your own examples to draw simple shapes, use colors, and place text on the screen. Later labs will use simple graphics.


Passing Parameters to Applets

There are several ways to pass parameters to Applets. The simplest way is for the Applet to extract its parameters from the HTML file.
Create the file DrawDisk.html with contents:

 
     <HTML>
     <HEAD><TITLE>Draw a Disk</TITLE></HEAD>
     <BODY>
     <HR>
     <CENTER><H3>Draw a Disk</CENTER></H3>
     <HR>
     <APPLET CODE="DrawDisk.class" HEIGHT=500 WIDTH=700 RADIUS=50></APPLET>
     <HR>

and the file DrawDisk.java with contents:

 
     import java.awt.* ;
     import java.applet.Applet ;
     public class DrawDisk extends Applet {
        int height ;
        int width ;
        int radius ;
        public void init() {
          // Extract the height from the parameter list...
          String HEIGHT_PARAMETER = getParameter("HEIGHT") ;
          Integer tempHeight = Integer.valueOf(HEIGHT_PARAMETER) ;
          height = tempHeight.intValue() ;
          // Extract the width from the parameter list...
          String WIDTH_PARAMETER = getParameter("WIDTH") ;
          Integer tempWidth = Integer.valueOf(WIDTH_PARAMETER) ;
          width = tempWidth.intValue() ;           // Extract the radius from the parameter list...
          String RADIUS_PARAMETER = getParameter("RADIUS") ;
         Integer tempRadius = Integer.valueOf(RADIUS_PARAMETER) ;
          radius = tempRadius.intValue() ;
        }
        public void paint (Graphics page) {
         setBackground(Color.red) ;
         page.setColor(Color.green) ;
         page.fillOval(width/2-radius,height/2-radius,radius*2,radius*2) ;
       }
     }

Now compile the Applet and run it with the appletviewer. It should create an Applets with a red graphics page that is WIDTH by HEIGHT pixels in size, with a green disk of RADIUS centered in it.
You don't need to understand everything in this program, but you should have a general idea of how it works. Since this Applet has an init() method, execution starts with it. Parameters are always passed as strings, so several conversions are required. For instance, the Applet sets its height parameter by:

 
      String HEIGHT_PARAMETER = getParameter("HEIGHT") ;
      Integer tempHeight = Integer.valueOf(HEIGHT_PARAMETER) ;
      height = tempHeight.intValue() ;

Basically, this extracts the height as a string, converts it to an integer object, then converts it again to an ordinary integer value.


Submission and Grading

You should be sure that you do all the exercises and understand all the material. Take this opportunity to become comfortable with the tools you will be using throughout the term.

What you need to turn in are a DrawGraphic.java and a DrawGraphic.html file. In this DrawGraphic.java program, you need to draw a rectangle and a circle, The rectangle is located on the middle of the left-upper quarter of the whole picture and the circle is on the right-upper quarter of the whole picture. The program’s name, your name, your email address and the date are drawn on the lower part of the whole picture within two lines. A sample output of the program is listed below. You can choose the sizes and the colors to whatever you want. One more important thing is that the height, width of the whole picture, the radius of the circle, and the height and the width of the rectangle are passed through the DrawGraphic.html file. Please study the DrawDisk example carefully. You can go to the webpage: http://java.sun.com/j2se/1.5.0/docs/api/ to study how to use the drawLine function of the Graphic class (This is a very important exercise. Please try to do it by yourself first, you will find that this webpage is a very useful tool for your future study).  

How to turn in your assignment: Please follow the instruction of the professor to zip the two files as DrawGraphic_YourLastName.zip and submit the zip file to the digital drop box.

When this lab is due: It is due by at next week's lab. No late submission will be accepted.