Dana Vrajitoru
C101/I210 Computer Programming

C101/I210 Lab 8 B

Date: Wednesday, March 10, 2020.
Due date: Wednesday, March 25, 2020.

Objective: To define methods in Java and work with characters and strings.

Ex. 1. String Manipulation

In this project we will write a few methods to test things related to characters and strings.

Create a Java project in Eclipse and call it lab8b. Create a class in this project called StringCount and containing the main method. Write your name, course, semester, and homework number at the top as a comment. Add the code for the definition of a scanner.

a. Uppercase Letter

Below the main, write a method that checks if a character is an uppercase letter and returns true if it is, and false otherwise. Since all uppercase letters have codes in a sequence, it is sufficient to test if the character is between 'A' and 'Z'. The header of the function should be

static boolean isUpper(char a)

b. Lowercase Letter

Similarly, write a method that checks if a character is a lowercase letter. All the lowercase letters are in the range from 'a' to 'z'.

c. Digit

Similarly, write a method that checks if a character is a digit. All the digits are in the range from '0' to '9'.

d. Vowel

Write a function that checks if a character is a vowel. The header of the function should be

static boolean isVowel(char a)

For this, we want to use the built-in method contains from the String class. We will use the string "aeiouAEIOU" containing all the vowels to call this function. The parameter of this function needs to be a string, not a character. The easiest way to convert it to a string would be to add it to an empty string:

"aeiouAEIOU".contains("" + a)

e. Main

Going back to the main, declare a String variable called text and assign it a piece of text of your own choosing. We will proceed to count the lowercase letters, uppercase letters, vowels, and digits in this string.

For this, declare variables to count each of these, such as countUpper, countLower, countDigit, countVowel. Initialize all of them to 0.

Then write a for loop going by 1 from 0 to the length of the text: text.length(). In the loop, assign text.charAt(i) to a char variable c. Then use a separate conditional for each of the 4 things to test. For each of them, call the respective method with c as a parameter, and if it returns true, then increment the corresponding counter. For example, for uppercase letters we would write

if (isUpper(c))
    countUpper++;

After the loop, add an output statement where you output the values of all the counters.

Test the program to make sure it runs and outputs the counts correctly. This is the end of this lab.

Turn in:

Upload StringCount.java to Canvas in Assignments - Homework 8, or wait until you have completed the homework to upload all the files at the same time.