Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

   Lecture 6

Program Flow Control: Answers

 

Use the code to find the answers to the activities. 

 

Activity A

Activity B

Activity C

Activity D


Activity A

 

Activity 6A

State if the following expressions return a true or false.

int i = 2, j = 1;

  1. 20 < -5

  2. 20 <= (-5 + 25)

  3.  i > j++

  4.  i > ++j

 

To check your answers; try the following code:-

 

public class Activity6A {

    public static void main (String[] args) {

        int i = 2, j = 1;

        


        System.out.println ( "20 < -5 evaluates to " +  ( 20 < -5 ) );
        System.out.println ( "20 <=  (-5 + 25) evaluates to " +  ( 20 <=  (-5 + 25)) );

        System.out.println ( "i > j++ evaluates to " +  (i > j++) );
        System.out.println ( "i > ++j evaluates to " +  (i > ++j) );

    }
}


Activity B

 

Activity 6B

Given the following declaration, evaluate the Boolean expressions and state if the result is a true or a false.

int a = 2, b = 6;

  1. (a < b) && (a <=2)

  2. (a != b) || (b == 6)

  3. !(a > b) && ( ( b > = 6) || ( b == a) ) 

  4. Examine the code below.  What do you think the output will be?  Now type in, compile and run the code..  What happens?

  5. Make one correction to the code so that the program will run.

public class TestAndOperator  {

     public static void main (String args[] ) {

        int a = 0;

        if ( a != 0 & 10/a > 1 ) {

            System.out.println ( "evaluated to true");

        }

        else {

            System.out.println ( "evaluated to false");

        }

    }

}

 

Answers-

  1. true

  2. true

  3. true

  4. you will get a compiler error because...

  5. change the AND operator in your code from a & to a && so the second expression does not get evaluated if the first expression is true.


Activity C

 

Activity 6C

  1. Write a program that reads in a numbers between 1 and 10 and displays the phrase "number is a prime" or "number is not a prime" as appropriate. Prime numbers between 1 and 10 are 1, 2, 3, 5 and 7. 

You can use the line

num = System.in.read() - 48;

to get a number input 

You must also include this line.

public static void main (String[] args) throws java.io.IOException {

   

   

Here is my code:-

public class Activity6C {

    public static void main (String[] args) throws java.io.IOException {
        int num;
        System.out.println( "Enter a number between 1 and 10");
        num = System.in.read() - 48;

        if (num == 4 || num == 6 || num == 8  ||  num == 9) {
            System.out.println ( num + " is not a prime");
        }
        else {
            System.out.println ( num + " is a prime");
        }
    }
}


Activity D

 

Activity 6D

  1. Create code for displaying the two times table in an applet window using a For loop.

  2. Create code for displaying the two times table in an applet window using a while loop.

  3. Create code for displaying the two times table in an applet window using a do-while loop.

Remember, you can use the g.drawString method in the paint method to display output in an applet window

Check out my code after you have created your own.

 

Here is my code for activity 6D1:-

import java.awt.*;
import java.applet.Applet;

public class Activity6D1 extends Applet{

    public void paint(Graphics g) {

        for (int i = 1; i<11; i++) {

            g.drawString(i + " times 2 is " + (i*2), 10,20*i);

        }
    }
}

Have a quick look at it running.  Activity 6D2 and 6D3 produce identical results but I will keep my code to myself.  I am sure you can create your own.

 

        


That is folks!!

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002