Professional Documents
Culture Documents
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
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:
SCP-CC103 | 1
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY
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
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
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
SCP-CC103 | 5
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY
int count = 0;
String pal = JOptionPane.showInputDialog(null, "Enter a String");
Your Output:
SCP-CC103 | 6