Professional Documents
Culture Documents
Itprog3L: (Object-Oriented Programming For I.T.)
Itprog3L: (Object-Oriented Programming For I.T.)
Itprog3L: (Object-Oriented Programming For I.T.)
ITPROG3L
(Object-oriented Programming for I.T.)
EXERCISE
3
CONTROL STRUCTURES IN JAVA PROGRAMS
OBILLO, Alec M.
Student Name
September 3, 2016
Date Performed
September 3, 2016
Date Submitted
LAB 2: Control Structures in Java
Programs
Learning Outcomes
These Lab sheet encompasses of activities which is 3A, 3B, 3C, 3D, 3E, 3F and 3G
b. Looping structures:
i. do..while
ii. while
iii. for
Activity 3A
Activity Outcome: Write program using control structures (if statement)
class InputValue
{
public static void main(String args[])
{
int num;
num = Integer.parseInt(args[0]);
if(num<100)
{
System.out.println("Number is less than 100");
}
}
}
Procedure:
Step 1: Type the above program, compile and execute. What is the output?
Output:
Activity 3B
Activity Outcome: Write program using control structures (if statement)
Program Marks.java below illustrates the execution of a simple if statement based on two
conditions. This program checks whether the marks is between 90 and 100 to print the Grade.
class Marks
{
public static void main(String args[])
{
int marks;
marks = Integer.parseInt(args[0]);
if ((marks>90) && (marks<100) )
System.out.println("Grade is A");
}
}
Procedure:
Step 1: Type the above program, compile and execute. What is the output?
Output:
Activity 3C
Activity Outcome: Write program using control structures (if..else statement)
Program sample.java below illustrates the use of the if-else statement. This program accepts a
number, checks whether it is less than 0 and displays an appropriate message.
class sample
{
public static void main (String args[])
{
int num;
num=Integer.parseInt(args[0]);
if (num<0)
System.out.println("Negative");
else
System.out.println("Positive");
}
}
Procedure:
Step 1: Type the above program, compile and execute. What is the output?
Output:
Activity 3D
Activity Outcome : Write program using control structures (if..else nested statement)
The program highest.java below illustrates the use of nested if statements. The
program accepts three integers from the user and finds the highest of the three.
class highest
{
public static void main (String args[])
{
int x;
int y;
int z;
x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);
z = Integer.parseInt(args[2]);
if(x>y)
{
if(x>z)
Procedure:
Step 1: Type the above program, compile and execute. What is the output?
Output:
Activity 3E
Activity Outcome: Write program using control structures (switch..case statement)
Program SwitchDate.java below illustrates switch case execution. In the program, the
switch takes an integer value (number of days) as input and displays the appropriate month.
import java.io.*; public
class SwitchDate
{
public static void main(String[] ags) throws Exception
{
BufferedReader mon = new BufferedReader (new InputStreamReader(System.in));
String m;
System.out.print("Enter days [28,30 and 31 days] : ");
m = mon.readLine();
byte month = Byte.parseByte(m);
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("There are 31 days in that month.");
break;
case 2:
System.out.println("“There are 28 days in that
month."); break;
case 4:
case 6:
case 9:
case 11:
System.out.println("There are 30 days in the
month."); break;
default:
System.out.println("Invalid month");
break;
}
}
}
Step 1: Open a new text file, and then enter the following code to choose a month and display output
of days for that month:
Step 2: Save the program as SwithDate.java, and compile and then run the program.
Output:
Activity 3F
Activity Outcome: Write program using looping structures (while, do..while, for
statements)
Output:
I’m clever
I’m clever
I’m clever
I’m clever
I’m clever
i. while loops
Procedures:
Step 1: Open a new text file, and then enter the above code.
Procedures:
Step 1: Open a new text file, and then enter the above code.
Step 3 : Make a conclusion, for every loop programs that you have observer.
Conclusion:
All the outputs are the same but only the codes are different because it has different
kinds of looping structures.
Activity 3G
Activity Outcome: Write program using looping structures (while, do..while, for
statements)
##########
##########
##########
i. do..while loops
int colCount = 0;
int rowCount = 0;
do {
colCount = 0;
do {
System.out.print("#");
colCount++;
}
while (colCount < width);
System.out.println();
rowCount++;
}
while (rowCount < height);
}
}
Procedures:
Step 1: Open a new text file, and then enter the above code.
System.out.println();
}
}
}
Procedures:
Step 1: Open a new text file, and then enter the above code.
Step 3 : Make a conclusion, what are the comment future for every loop programs that
you have observer.
Conclusion:
All the outputs are the same but only the codes are different because it has different kinds
of looping structures.
Activity 3H
Activity Outcome : Write program using sentinel value
Procedures:
Step 1: Analyze the input of the problem above. (The loop for sentinel value is negative
score).
Step 2: Input the score through keyboard using input stream (BufferedReader or
Scanner).
Step 3: Use relational and logical operators to evaluate the score (eg. score >= 90 &&
score <= 100 will be grade A). Do evaluation for other grades.
Step 4: Program below illustrates the above problem. Open a new text file, and then
enter the below code.
import java.io.*;
class grade
{
public static void main(String args[]) throws IOException
{
String input;
int mark,gradeA=0,gradeB=0,gradeC=0,gradeD=0,gradeE=0;
int allGrade=0;
BufferedReader obj1 = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter score (to stop enter -1) : ");
do
{
input = obj1.readLine();
score = Integer.parseInt(input);
if (score >= 90 && score <= 100)
gradeA++;
else
if (score >= 80 && score <= 89)
++gradeB;
else
Take a Challenge
1. Write a program which reads a two-digit integer and checks whether the two digits are same
or not. Using Scanner class.
Source code:
import java.util.Scanner;
public class equal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x,y;
System.out.println("Please Enter a number consists of 2 digits
only : ");
x = input.nextInt();
y = input.nextInt();
if (x==y){
System.out.println("Two digits are equal!!! ");
}
else {
System.out.println("Two digits are not equal!!! ");
}
}
2. Design and implement a Java program that prompts the user to enter 2 numbers of type int
then displays a menu in a JOptionPane dialoge on the screen as shown below:
a. Print sum
b. Print sum and average
c. Print sum, average, and max
- Choose an option [a, b, c]: _
The program reads the user’s option as a String using JOptionPane input message and then
display the result.
Source Code:
import javax.swing.JOptionPane;
public class Option {
num1= Double.parseDouble(JOptionPane.showInputDialog("Enter
First Integer: "));
num2= Double.parseDouble(JOptionPane.showInputDialog("Enter
Second Integer"));
choice= Double.parseDouble(JOptionPane.showInputDialog("1.Print
Sum \n2.Print Sum and Average \n3. Print Sum, Average and Max
\nEnter your choice: "));
sum = num1+num2;
ave = sum/2;
max = num1 > num2 ? num1:num2;
if (choice == 1)
JOptionPane.showMessageDialog(null,"Sum" +sum);
else
if(choice == 2)
JOptionPane.showMessageDialog(null,("Sum: " +sum+
"Average: " +ave));
else
if (choice == 3)
JOptionPane.showMessageDialog(null,"Sum: " +sum+
"Average: " +ave+ "Max: " +max);
}
}
3. Certain instructor assigns letter grade for his course based on the following table:
Score Grade
>= 90 A+
>= 85 A
>= 80 B+
>= 75 B
>= 65 C+
>= 60 C
>= 55 D+
>= 50 D
< 50 F
Write a program that reads the score and prints the letter grade according to the table. Using
Scanner Class.
Source Code:
import java.util.Scanner;
public class Grade {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
double x;
System.out.println("Enter your score: ");
x = input.nextDouble();
if (x>=90){
System.out.println("Your grade is A+");
}
else if (x>=85){
System.out.println("Your grade is A+");
}
else if (x>=80){
System.out.println("Your grade is A");
}
else if (x>=75){
System.out.println("Your grade is B+");
}
else if (x>=70){
System.out.println("Your grade is B");
}
else if (x>=65){
System.out.println("Your grade is C+");
}
else if (x>=60){
System.out.println("Your grade is C");
}
else if (x>=55){
System.out.println("Your grade is D+");
}
else if (x>=50){
System.out.println("Your grade is D");
}
else if (x<50){
System.out.println("Your grade is F");
}
}
}
Sample output:
2. For you, which is preferably the most convenient control structure to be used in
comparisons, IF-ELSE or SWITCH?
For me I prefer IF-ELSE because it is compatible to all situations.
3. Do SWITCH and IF-ELSE have differences?
The switch statement can have a number of possible execution paths. A switch works with
the byte, short, char, and int primitive data types. An if-then-else statement can test
expressions based on ranges of values or conditions, whereas a switch statement tests
expressions based only on a single integer, enumerated value, or String object.
For me IF-ELSE is the easiest way in looping because you will put the condition in the if
statement then if the condition is wrong the output of else will appear.
REFLECTION (Conclusion)
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________