CS 1054: Laboratory Exercise 3

Objective

This exercise's objective is to familiarize you with the use of control statements in Java. You will work with three different kinds of control statements: if-else statements, while loops and for loops. You will also learn how to use some basic input methods of the Scanner class and some formatting techniques of the printf method.

To Receive Credit

You will do four small tasks within one program. After each one, show your results to the GTA.

Activities

After launching the Eclipse IDE, create a new project and name it Lab3. Create a class within this project called LabExercise3. In the pop-up window for specifying the name of this class, click on the check-box next to the option for creating the main method. Within this class, do the following four tasks.

  1. Scanner, println and printf: In this task, you will use the Scanner class and System.out's printf method. Within the main method of your class, create a Scanner object
    Scanner scanner = new Scanner(System.in);
    Some of the methods within the Scanner class include next(), nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(), or nextBoolean() which can used to obtain to a string, byte, short, int, long, float, double, or boolean value. For example, the code below can be used to obtain a double value from the console input and store in the variable d.
    System.out.print("Enter a double value: "); 
    Scanner scanner = new Scanner(System.in); 
    double d = scanner.nextDouble();
    1. Declare three variables of type int and two variables of type double.
    2. Use the scanner's nextInt() and nextDouble() methods in the same fashion as indicated above to obtain input values and store them in the variables that you declared.
    3. Declare some more variables of different data-types including the String data-type and experiment with the methods of the Scanner class.
    4. Use the System.out.println method to print out the values that you just input and verify that they are indeed correct. For example, to see the value of a variable named val, you might use the code shown below:
      System.out.println("val = " + val);
    5. Use the new JDK 1.5 printf statement.
      System.out.printf(format, item);
      Where format is a string that may consist of substrings and format specifiers. A format specifier specifies how an item should be displayed. An item may be a numeric value, character, boolean value, or a string. Each specifier begins with a percent sign. The table below indicates the specifiers for different data types.
      Specifier Output Example
      %b a boolean value true or false
      %c a character 'a'
      %d a decimal integer 200
      %f a floating-point number 45.460000
      %e a number in standard scientific notation 4.556000e+01
      %s a string "Java is cool"
      Use this method to format the values that you entered. Enter the numbers 45.6754345 and -9877.2342197 as values for the doubles. To use the printf statement and print the value of the same variable val that is of type int, you could use the code shown below:
      System.out.printf("val = %d", val);
      Note the difference between this code and how you you use the System.out.println method.
    6. You can see that by default, the %f specifier generates floating point numbers with 6 trailing numbers after the decimal point. You can cut down on these by specifying exactly how many numbers you want displayed. For example, if you want only two numbers displayed after the decimal point, you use the following notation %.2f instead of using just %f. Experiment with different specifiers for the two double numbers. Also use the other specifiers for different types of variables.
    7. Show your results to the GTA.
  2. Comment out all the source code that you wrote so far.
    if-else statement: (Exercise 3.6 from your textbook) Write the source code that prompts the user to enter an integer and checks whether the number is divisible by both 5 and 6, either or just one of them. Here is some sample output for input 10, 30 and 23.
    10 is divisible by 5 or 6, but not both
    30 is divisible by both 5 and 6
    23 is not divisible by either 5 or 6
    
  3. Comment out all the source code that you wrote so far.
    while loop: Consider the series of odd numbers 1, 3, 5, ... . Use the while loop to print out the first 100 odd numbers and their sum. You can refer to your class notes on how to use the while loop. An example of the while loop from the class notes is shown below
    int count = 0;
    while (count < 100) {
      System.out.println("Welcome to Java!");
      count++;
    }        
    Show your work to the GTA.
  4. Comment out all the source code that you wrote so far.
    for loop: Do the same task indicated above using a for loop. Again, refer to your class notes on how to use the for loop. Show your work to the GTA. An example of the for loop from your course notes is shown below.
    int i;
    for (i = 0; i < 100; i++) {	 
      System.out.println(
         "Welcome to Java!"); 
    }

© Mir Farooq Ali 2005.