You are on page 1of 6

ST.

JOHN PAUL II COLLEGE OF DAVAO


COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

MidTerm Laboratory No. 2: Loop Structures

Objectives
1. To understand the concepts and theory of 3 different loop structures.
2. To build programs using 3 different loop structures
3. To know how to trace the program using different loops

Background

In programming languages, loops are used to execute a set of instructions/functions


repeatedly when some conditions become true. There are three types of loops in Java.

1. for loop
2. while loop
3. do-while loop

The Java for loop is used to iterate a part of the program several times. If the number
of iteration is fixed, it is recommended to use for loop.

while loop is used to iterate a part of the program several times. If the number of
iteration is not fixed, it is recommended to use while loop.

The Java do-while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed and you must have to execute the loop at least once, it
is recommended to use do-while loop.

The Java do-while loop is executed at least once because condition is checked after
loop body.

General Instructions:

1. Answer the activities in the succeeding pages.


2. Submit it in a .PDF format with a filename convention
<SURNAME>_<ACT#> such that BASTE_ACT5.
3. The deadline of submission will be 5 days after it was discussed.

Learning Resources and Tools

SCP-CC103 | 1
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

1. Laboratory Manual or SCP


2. Ballpen
3. PC/Laptop
4. Internet
5. IDE (Eclipse, Netbeans, ect)

NOTE: Put your complete name in all of your program as a


comment and in your outputs. You may use JOptionPane.

Activity 2.1. Create a program that will iterate the statement to accept any character or string value.
Afterwards, the program will then count how many special characters (excluding spaces),
consonants, vowels, spaces, zero,odd and even digits entered from a console or a JOptionPane.
Note that a null value will not be accepted, so prompt a warning message to notify the user. Your
program should NOT be case sensitive. (50PTS)

Expected output:

SCP-CC103 | 2
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

Your Source Code:


import javax.swing.JOptionPane;
public class stringcount {

public static void main(String[] args) {


// NIÑA MAE TERIOTE
String str1=" ", str2 ="";
char ch;
int num_zero = 0, spc = 0, cons = 0, vow = 0, even = 0, odd = 0;
str1 = JOptionPane.showInputDialog("Type a Word");
str1 = str1.toLowerCase();
str1 = str1.toUpperCase();
if (str2.equals(str1)||"".equals(str1)) {
JOptionPane.showMessageDialog(null, "Please aenter a word");

}else {for (int i =0; i<str1.length(); i++) {


ch = str1.charAt(i);

if (Character.isLetter(ch)) {
if (ch=='a'|| ch =='e'|| ch=='i'|| ch=='o' || ch == 'u' ||
ch =='A' || ch == 'E' || ch == 'I' || ch == 'O'|| ch == 'U') {
vow++;
}
else
cons++;
}
else if(Character.isDigit(ch)) {
if (ch % 2 == 0) {
even++;
}
else if (ch == 0) {
num_zero++;
}else
odd++;
}
else spc++;

}
JOptionPane.showMessageDialog(null, "Entered Word :" + str1 + "\nVowels: " + vow +
"\nConsonant:" + cons + "\nZero: " + num_zero + "\nodd: " + odd +
"\nEven: " + even + "\nSpecial: "+ spc );

}
}
}

Your Output:

SCP-CC103 | 3
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

Activity 2.2. Write a program that determines an entered String whether it is a Palindrome or non-
Palindrome. (50PTS)

Expected output:

SCP-CC103 | 4
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

SCP-CC103 | 5
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

Your Source Code:


import javax.swing.JOptionPane;
public class stringcount {

public static void main(String[] args) {


// NIÑA MAE TERIOTE

int count = 0;
String pal = JOptionPane.showInputDialog(null, "Enter a String");

int temp = pal.length();

String rev = new StringBuffer(pal).reverse().toString();

for (int i = 0; i<temp; i++) {


if (pal.charAt(i)== rev.charAt(i))
count++;
}
if(count == temp) {
JOptionPane.showMessageDialog(null, pal + " is Palindrome");
} else {
JOptionPane.showMessageDialog(null, pal + " s not Palindrome");
}
}
}

Your Output:

SCP-CC103 | 6

You might also like