CSCI 201: Intro to Programming (Java)

Lab 2: Working with conditional statements

Resources

Lab Task I

Write a Java program for a college's admission office.

Your program should print a message "Accept" if the student has any of the following:

If the student does not meet any of the qualifications, print "Reject".
Name the class file Admissions.java and save it in your cs201 directory.

Implementation hints

You can compose the admission condition by ANDing and ORing simpler conditions. You will need to OR four conditions corresponding to the four cases mentioned above. Each of the four conditions is, in turn, a combination of two other conditions (one for the grade point average and one for the admission test score).

To AND two or more simple conditions use the syntax

if (condition1 && condition2 && condition3) ...

To OR two or more simple conditions use the syntax

if (condition1 || condition2 || condition3) ...

Lab Task II

Modify your program to be run from console but provide a graphics interface to read the point average and the admission test score. For this you will need to perform the following steps:

  1. Create a file Admissions2.java and put the following code there:
    import javax.swing.JOptionPane;            // graphics dialog package
    
    public class Admissions2 
    {
       public static void main(String[] args)
       {
         // enter the grade point average
         String s = JOptionPane.showInputDialog("Enter the point average");
         double gpa = Double.parseDouble(s);   // parse the input string to double
    
         // enter the admission test score
         s = JOptionPane.showInputDialog("Enter the admission test score");
         int ats = Integer.parseInt(s);        // parse the input string to int
    
         // put your conditional statements
         // from the first task here
    
    
         // use the following code to print the rejection message
         JOptionPane.showMessageDialog(null, "Reject", "Results",
            JOptionPane.INFORMATION_MESSAGE);
    
         // use the following code to print the acceptance message
         JOptionPane.showMessageDialog(null, "Accept", "Results",
            JOptionPane.INFORMATION_MESSAGE);
       }
    }
    

  2. Compile your code and correct possible compilation errors.
  3. Run the code from within NetBeans.

Now do the compilation and launch from the command line interface.

  1. Open the Windows console by using Start -> Run -> cmd pattern. This will cause a DOS-looking window with a black background to appear.

  2. Navigate in the console window to My Documents folder by using the cd instruction.
    cd "My Documents"\NetBeansProjects\Admissions2

  3. Compile your java code by using the javac instruction:
    PATH_TO_JAVA\javac Admissions2.java
    You might need to provide the complete PATH to the file java.exe in the above command. Use the Windows file search utility to find this file. Its location depends, among other factors, on the version (and subversion) of Java installed. On my system, for example, the path is
    C:\"Program Files"\Java\jdk1.8.0_77\bin\java

    On your Windows system the path is mostly likely different. Find it with "My Computer" tool by exploring the "Program Files" folder.

    Hence, to run the code I would enter the following instruction:
    C:\"Program Files"\Java\jdk1.8.0_77\bin\java Admissions2
    You must put "Program Files" in quotes to kill the special meaning of the space delimeter in the folder name.

  4. Run the code:
    PATH_TO_JAVA\java Admissions2

Lab Task III (if you wish a challenge)

You can launch the application designed in Part II not just from a command line, but also over the network. Even more wonderful, is that no modification of the Java program is needed. This is possible due to the Java Network Launch Protocol (JNLP) that is supported by the Java plugin and most of the browsers. Change to your public_html folder on cs2.uwsuper.edu server.

  1. Copy the file compiled application file Admissions2.class in your public_html folder.

  2. Create the following manifest file Admissions2.mf that just tells the system which file has to run first, although it is obvious (for us) in our single-file case:
    Main-Class: Admissions2

  3. Turn the .class file into a JAR, which is a short for Java Archive. This file is just a ZIPed archive of the involved files.
    jar -cvfm Admissions2.jar Admissions2.mf Admissions2.class

  4. Create the following file admissions2.jnlp in your public_html folder:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <jnlp spec="1.0+" codebase="http://cs2.uwsuper.edu/~your_login/">
      <information>
        <title>Admissions Office</title>
        <vendor>CSCI 201</vendor>
        <description>Launching the Admission Application</description>
      </information>
      <resources>
        <j2se version="1.5+"/>
        <jar href="Admissions2.jar"/>
      </resources>
      <application-desc main-class="Admissions2">
      </application-desc>
    </jnlp>

  5. Open this file in your browser from the server and try how the application works over the network.
    http://cs2.uwsuper.edu/~your_login/admissions2.jnlp
    Make sure to put the tilde sign (~) in front of your login.

Does not work? Your browser has to be configured for Java Web Start. With modern browsers this step is most likely not needed, so skip it. Otherwise, You need to associate the application/x-java-jnlp-file MIME type with the javaws application (part of JRE). The configuration should be automatic after installing the JDK. If not, in the Firefox browser open the Edit -> Preferences menu and click on Downloads icon. Click the button View and edit Actions... in the Download Actions section. Make sure the following entry is set in the Actions table:

Extension File Type MIME Type Action
JNLP JNLP file application/x-java-jnlp-file PATH_TO_JAVAWS/javaws(.eve)

Still does not work? You computer might be needed to be configured to run applets from external sites. Google for information on how to enable this feature (after all, I told you that this is a challenge).