Module-3-Selections-and-LoopsUpdated

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Republic of the Philippines

Romblon State University


Romblon, Philippines

Event-Driven
Programming

Selections & Loops

Event-Driven Programming | 1
Republic of the Philippines
Romblon State University
Romblon, Philippines

Chapter 3: Selections and Loops

LEARNING OUTCOMES:

• To generate random numbers using System.currentTimeMillis()


and Math.random()
• To write programs using System.currentTimeMillis()and Math.random()
• To write programs for executing statements repeatedly using a while loop

ABSTRACTION

3.1 Selection Statements


Selection statements use conditions that are Boolean expressions. A Boolean expression is an
expression that evaluates to a Boolean value: true or false.

3. 2 System.currentTimeMillis()
The java.lang.System.currentTimeMillis() method returns the current time in milliseconds. The unit
of time of the return value is a millisecond, the granularity of the value depends on the underlying
operating system and may be larger.

Examples:

package javaActivities;
import java.util.Scanner;
public class AdditionQuiz {

public static void main(String[] args) {


// TODO Auto-generated method stub

int number1 = (int)(System.currentTimeMillis() % 10);


int number2 = (int)(System.currentTimeMillis() / 10 % 10);

// Create a Scanner
Scanner input = new Scanner(System.in);

System.out.print("What is " + number1 + " + " + number2 + "? ");

int answer = input.nextInt();

System.out.println( number1 + " + " + number2 + " = " + answer + " is " +
(number1 + number2 == answer));

}
}

SAMPLE OUTPUT:

Event-Driven Programming | 2
Republic of the Philippines
Romblon State University
Romblon, Philippines

3. 3 Math.random()
The java.lang.Math.random( ) is used to return a pseudorandom double type number greater than or equal
to 0.0 and less than 1.0. The default random number always generated between 0 and 1.

Examples:

package javaActivities;
import java.util.Scanner;
public class SubtractionQuiz {

public static void main(String[] args) {


// TODO Auto-generated method stub

// 1. Generate two random single-digit integers


int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);

// 2. If number1 < number2, swap number1 with number2


if (number1 < number2) {
int temp = number1;
number1 = number2;
number2 = temp;
}

// 3. Prompt the student to answer "What is number1 – number2?"


System.out.print
("What is " + number1 + " − " + number2 + "? ");
Scanner input = new Scanner(System.in);
int answer = input.nextInt();

// 4. Grade the answer and display the result


if (number1 - number2 == answer)
System.out.println("You are correct!");
else {
System.out.println("Your answer is wrong.");
System.out.println(number1 + " − " + number2 +
" should be " + (number1 - number2));
}
}
}

SAMPLE OUTPUT:

Event-Driven Programming | 3
Republic of the Philippines
Romblon State University
Romblon, Philippines

3. 4 Loops
A loop can be used to tell a program to execute statements repeatedly.

Suppose you need to display a string (e.g., Welcome to Java!) a hundred times. It would be tedious
to have to write the following statement a hundred times:

So, how do you solve this problem?


Java provides a powerful construct called a loop that controls how many times an operation or a
sequence of operations is performed in succession. Using a loop statement, you can simply tell the
computer to display a string a hundred times without having to code the print statement a hundred times,
as follows:
SAMPLE OUTPUT:

package javaActivities;

public class WhileLoop {

public static void main(String[] args) {


// TODO Auto-generated method stub

int count = 0;
while (count < 100) {
System.out.println("Welcome to Java!");
count++;
}
}
}

The variable count is initially 0. The loop checks whether count < 100 is true. If so, it executes the loop body
to display the message Welcome to Java! and increments count by 1. It repeatedly executes the loop body until count
< 100 becomes false. When count < 100 is false (i.e., when count reaches 100), the loop terminates, and the next
statement after the loop statement is executed.

Loops are constructs that control repeated executions of a block of statements. The concept of looping is
fundamental to programming. Java provides three types of loop statements: while loops, do-while loops, and for
loops.

Event-Driven Programming | 4
Republic of the Philippines
Romblon State University
Romblon, Philippines

3. 5 Applying While Loops

Example #1:

package javaActivities;
import java.util.Scanner;
public class AdditionQuizV2 {

public static void main(String[] args) {


// TODO Auto-generated method stub

int number1 = (int)(Math.random() * 10);


int number2 = (int)(Math.random() * 10);

// Create a Scanner
Scanner input = new Scanner(System.in);

System.out.print(
"What is " + number1 + " + " + number2 + "? ");
int answer = input.nextInt();

while (number1 + number2 != answer) {


System.out.print("Wrong answer. Try again.\n\n"
+ "What is " + number1 + " + " + number2 + "? ");
answer = input.nextInt();
}
System.out.println("You got it!");
}
}

SAMPLE OUTPUT:

Event-Driven Programming | 5
Republic of the Philippines
Romblon State University
Romblon, Philippines

Example #2: SAMPLE OUTPUT:

package javaActivities;
import java.util.Scanner;
public class SubtractionQuizLoop {

public static void main(String[] args) {


// TODO Auto-generated method stub
final int NUMBER_OF_QUESTIONS = 5; // Number of questions
int correctCount = 0; // Count the number of correct answers
int count = 0; // Count the number of questions
long startTime = System.currentTimeMillis();
String output = ""; // output string is initially empty
Scanner input = new Scanner(System.in);

while (count < NUMBER_OF_QUESTIONS) {


// 1. Generate two random single-digit integers
int number1 = (int)(Math.random() * 100);
int number2 = (int)(Math.random() * 100);

// 2. If number1 < number2, swap number1 with number2


if (number1 < number2) {
int temp = number1;
number1 = number2;
number2 = temp;
}

// 3. Prompt the student to answer "What is number1 – number2?"


System.out.print(
"What is " + number1 + " - " + number2 + "? ");
int answer = input.nextInt();

// 4. Grade the answer and display the result


if (number1 - number2 == answer) {
System.out.println("You are correct!\n");
correctCount++;
}
else
System.out.println("Your answer is wrong.\n" + number1
+ " - " + number2 + " should be " + (number1 - number2) + "\n");

// Increase the count


count++;

output += "\n" + number1 + "-" + number2 + "=" + answer +


((number1 - number2 == answer) ? " correct" : " wrong");
}

long endTime = System.currentTimeMillis();


long testTime = endTime - startTime;

System.out.println("Correct count is " + correctCount +


"\nTest time is " + testTime / 1000 + " seconds\n" + output);
}
}

Event-Driven Programming | 6

You might also like