CS 1054: Laboratory Exercise 2

Objective

This exercise's objective is to help you understand variable declarations and their use in a Java program. You will also learn how to create both arithmetic and boolean expressions involving variables. One other objective of this lab is to familiarize you with the use of comments in Java.

To Receive Credit

Record the results you get after each exercise and have the TA check it by the end of lab. Also, show your working program to the TA.

Activities

  1. After launching the Eclipse IDE, create a new project and name it Lab2. Create a class within this project called LabExercise2. Delete the automatic code that is inserted by Eclipse automatically and copy the entire code shown below inside the class. Once you have pasted this code, you can see certain sections of the code in green. Those are commented out lines. You can notice that there are two kinds of comments: 1) Single line comments that start out with // and 2) Block comments that start with /* and end with */
    /*
     * Created on May 26, 2005
     * Author: Mir Farooq Ali
     * Purpose: Describe file here
     * Programming Environment: Eclipse 3.1
     * 
     * Programmer: farooq
     */
    import java.util.Scanner;
    
    public class LabExercise2 {
    
        public static void main(String[] args) {
            //Reading block starts here
            int firstnum, secondnum; 
            
            Scanner inputScanner = new Scanner(System.in);
            
            System.out.print("Enter the first number: ");
            firstnum = inputScanner.nextInt();
            System.out.print("Enter the second number: ");
            secondnum = inputScanner.nextInt();
            //Reading block ends here
            
            // First block starts
            // Perform some basic arithmetic operations here
            System.out.println("(firstnum + secondnum) = " + 
                    (firstnum + secondnum));
            System.out.println("(firstnum - secondnum) = " + 
                    (firstnum - secondnum));
            System.out.println("(firstnum * secondnum) = " + 
                    (firstnum * secondnum));
            System.out.println("(firstnum / secondnum) = " + 
                    (firstnum / secondnum));
            System.out.println("(firstnum % secondnum) = " + 
                    (firstnum % secondnum)); // First block ends
            
           /* // Second block starts
    
            boolean x, y, z;
    
            x = (firstnum > secondnum); // test if firstnum is greater secondnum
            System.out.println("x = " + x);
    
            y = (firstnum <= secondnum); // test if firstnum is less than or equal to secondnum
            System.out.println("y = " + y);
            
            //z = (); // test for equality of firstnum with secondnum
            //System.out.println("z = " + z);
            
            // z = (); // test if firstnum is greater than or equal to secondnum
            // System.out.println("z = " + z);
    
           */ // Second block ends
            
           /* // Third block starts
             double p, q, r, s, t, val; 
             p = 20.0; 
             q = 10.0;  
             r = 7.0;
             s = 13.0; 
             t = 17.0;
             
             // val =                // Complete this statement
             System.out.println("value = " + val);  
             
            */ // Third block ends
        }
    }
           
            
  2. Run the code by selecting Run from the main menu, then 'Run as' followed by 'Java Application'. This program expects you to enter two integer values. To do this, select the console tab at the bottom right portion of the workspace and click in the space there. Type in some number and press enter. Enter a second number after the second message and press enter. You should see a few messages with the results of some basic arithmetic operations on the two numbers. Experiment by running the program many times giving different values for the two numbers. Enter the number zero (0) as the value for the second number. At what point do you see an error message? Write down the error message that you see on the screen on a sheet of paper. Why do you think you go the error message?
  3. Comment out the entire first block of code that you just executed in the above step by inserting block comments /* and */ around the statements that start with // First block starts and end with ... // First block ends Uncomment the second block of statements by removing the block comments /* and */. Run this code and test it by entering different values for firstnum and secondnum. Uncomment the statements towards the end of this second block by removing the // in front of the four statements. Complete the boolean expression for z as indicated in the comments. Write down the expressions on a sheet of paper. Run this code again and test it by entering different values for firstnum and secondnum.
  4. Comment out the entire second block of code that you just executed in the above step by inserting block comments /* and */ around the statements that start with // Second block starts and end with ... // Second block ends Also comment out the reading block at the beginning by inserting comments around the statements that start with //Reading block starts here and end with //Reading block ends here Uncomment the third block of statements by removing the block comments /* and */.

    Complete the statement for the variable val based on the equation shown above. Write this down on a sheet of paper. With the values of p, q, r, s and t shown above, the variable val's value should be 4.0.

  5. Show your answers to the tasks 2, 3 and 4 above to the GTA to get credit for this assignment.

© Mir Farooq Ali 2005.