Itprog3L: (Object-Oriented Programming For I.T.)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

INFORMATION TECHNOLOGY EDUCATION DEPARTMENT

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

By the end of this lab, students should be able to:


1. Write program using control structures:
a. Selection structures:
i. If..else
ii. Switch..case

b. Looping structures:
i. do..while
ii. while
iii. for

Activity 3A
Activity Outcome: Write program using control structures (if statement)

Program InputValue.java below illustrates the execution of a simple if statement. The


program checks whether the given number is greater than 100.

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)

System.out.println(x + " is the highest");


else
System.out.println(z + " is the highest");
}
else
{
if (y>z)
System.out.println(y + " is the highest");
else
System.out.println(z +" is the highest");
}
}
}

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.

Step 3: Correct errors if any and run the program.

Step 4: Observed the output.

Output:
Activity 3F
Activity Outcome: Write program using looping structures (while, do..while, for
statements)

Programs below illustrates looping structures to print the output:

Output:

I’m clever
I’m clever
I’m clever
I’m clever
I’m clever

i. while loops

public class WhileTest {


public static void main(String[] args)
{ int i = 0;
while (i<5){
System.out.println("I'm clever");
i++;
}
}
}

Procedures:
Step 1: Open a new text file, and then enter the above code.

Step 2: Observer the output. Try to understand it’s.

ii. do..while loops

public class doWhileTest {


public static void main(String[] args)
{ int i = 0;
do{
System.out.println("I'm clever");
i++;
}
while (i<5);
}
}
Procedures:
Step 1: Open a new text file, and then enter the above code.

Step 2: Observer the output. Try to understand it’s.

iii. for loops

public class forTest {


public static void main(String[] args) {

for (int i=0; i< 5; i++)


System.out.println("I'm clever");
}
}

Procedures:
Step 1: Open a new text file, and then enter the above code.

Step 2: Observer the output. Try to understand it’s.

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)

Programs below illustrates looping structures to print the output:

##########
##########
##########
i. do..while loops

public class DoWhileRectangle {

public static int height = 3;


public static int width = 10;

public static void main(String[] args) {

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.

Step 2: Observer the output. Try to understand it’s.

ii. while loops

public class WhileRectangle {


public static int height = 3;
public static int width = 10;

public static void main(String[] args)


{ int colCount = 0;
int rowCount = 0;

while (rowCount < height)


{ colCount = 0;
while (colCount < width)
{ System.out.print("#"
); colCount++;
}
System.out.println();
rowCount++;
}
}
}
Procedures:
Step 1: Open a new text file, and then enter the above code.

Step 2: Observer the output. Try to understand it’s.

iii. for loops

public class ForRectangle


{
public static int height = 3;
public static int width = 10;
public static void main(String[] args)
{
for (int rowCount=0; rowCount<height; rowCount++)
{
for (int colCount=0; colCount<width; colCount++)
System.out.print("#");

System.out.println();
}
}
}
Procedures:
Step 1: Open a new text file, and then enter the above code.

Step 2: Observer the output. Try to understand it’s.

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

Write a program to read a list of exam scores (integer


percentages in the range 0 to 100) and to output the total number
of grades and the number of grades in each letter-grade category
(90 to 100 = A, 80 to 89 = B, 70 to 79 = C, 60 to 69 = D, and 0
to 59 = F). The end of the input is indicated by a negative score
as a sentinel value. (The negative value is used only to end the
loop, so do not use it in the calculations). For example, if the
input is
98
87
86
85
85
78
73
72
72
72
70
66
63
50
-1

The output would be:

Total number of grades = 14


Number of A’s = 1
Number of B’s = 4
Number of C’s = 6
Number of D’s = 2
Number of F’s = 1

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

if(score >= 70 && score <= 79)


++gradeC;
else
if (score >= 60 && score <= 69)
++gradeD;
else
if (score >= 0 && score <= 59)
++gradeE;

} while (mark != -1);


allGrade = gradeA + gradeB + gradeC + gradeD + gradeE;
System.out.println("Total number of grades : " +
allGrade); System.out.println("Number of A's = " +
gradeA); System.out.println("Number of B's = " + gradeB);
System.out.println("Number of C's = " + gradeC);
System.out.println("Number of D's = " + gradeD);
System.out.println("Number of E's = " + gradeE);
}
}
Step 5: Observer the output.
Output:

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.

Sample output 1: Sample output 2:

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 {

public static void main(String[] args)


{
double num1,num2,sum,choice,ave,max;

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:

QUESTION AND ANSWER:

1. What is the significance of using control structures?


 It shows you how to structure the flow of control through a PL/SQL program. You learn how
statements are connected by simple but powerful control structures that have a single entry and
exit point. 

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.

4. Which is the easiest way in looping? Explain.

 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)
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________
____________________________________________________________________

You might also like