Scott Grissom
School of Computing & Info Systems
Grand Valley State University
picture

Lab: Ch 7 Class Methods

Preparation

Do the following before arriving at lab:

  • Read section 7.15 and Appendix E
  • Bring your textbook to lab

Objectives

After completing this lab, you should be able to:

  • describe the roll of the public static void main method in a java application
  • invoke an application from the command line
  • compile a java source file at the command line

Exercises

Getting Started
  1. Open your Project 4 in BlueJ (or any other project you have available)
  2. Open the GUI class
  3. Add the main method provided below
  4. Compile the GUI class
  5. Do not instantiate a GUI object, but invoke the main method by right clicking on the GUI class
Sample main method
public static void main(String [ ] args){
      GUI g = new GUI();
}     
 
Invoking an application from the command line

You have discovered that BlueJ is a handy tool to write and run java programs. However, you can also create java programs with a simple text editor. It is important you have a basic understanding of this process.

  1. Open the CSIS folder
  2. Open the "JDK Prompt 5.0" program (this may take a while)
  3. When the application completes the load process you can put the CSIS window away
  4. Within the black window, change to your N: drive by typing "n:" and pressing return
  5. Review the files and folders by typing "dir" and pressing return
  6. Move to your Project 4 folder by typing "cd directory" and pressing return
  7. Start the application by typing "java GUI".

 

Create a program away from BlueJ

Traditionally, the first program a student writes is called Hello World. It simply prints a message to the screen. You have written much more advanced programs with the help of BlueJ. You will now write your first java program without the help of BlueJ.

  1. Within the black terminal window, type "edit Hello.java". This will create a new file called Hello.java.
  2. Write the class as provided below.
  3. Save and exit the edit program.
  4. Compile the java program by typing "javac Hello.java". If all goes well, a new file was created called "Hello.class".
  5. Type "dir", do you see the new file?
  6. Now run the program by typing "java Hello ".
  7. Did it work? If not repeat the above steps until it does.
Hello World class
/**
This is comment about the class


@author Scott Grissom
@version November 28, 2006
*/
public class Hello{
public static void main(String [] args){
    System.out.println("Scott says 'Hello World'");
}
}    
A double-clickable application

Another way to invoke an application outside of BlueJ is to double click on a .jar file. However, this jar file must be created. You can easily do this within BlueJ.

  1. Open your Project 4 in Blue J
  2. Select "Create jar file..." from the Project menu
  3. Select the class that has the main method (probably GUI)
  4. Click "continue"
  5. Provide a file name (pay attention to where the file is saved).
  6. click "finished"
  7. Close BlueJ
  8. You can now maneuver through Windows to find the newly created file and double click on it!

 

Grading Criteria

This lab is worth a possible 10 points. Turn in a printout of Hello World source code (including brief comments).

  • method (8 pts)
  • coding style (2 pts)