Week 3 Exercise
In this exercise, you complete simple programming problems similar to your Lab 2, but this time using conditional statements and loops.
Setup
For this assignment you must use IntelliJ and follow the instructions in the Lab 3 Pre-Lab regarding installing Java and IntelliJ. You must also create a new project for this assignment and package with your MSOE username. Download the provided Java file and copy or move it into your package.
The downloaded code contains 10 methods stubs that you must implement based on the provided JavaDoc. When you run run the program, the main() method will test each method to see if it's return value matches an expected value. It will then print out the expected and actual value in green or red depending on whether they matched or not.
You must implement all the methods such that all the tests pass and produce green text. You should only modify the method stubs and should not change main() or the testMethod() method.
Complete the following program
The program below is intended to count the number of vowels in a given String, but is incomplete. Your job is to:
- Complete the "helper" methods that print out the results and check if a character is a vowel
- In the main method, write a for loop to iterate through the String and count how many vowels it contains
- Repeat Step 2, but this time using a while loop
- Repeat Step 2, but this time using a do-while loop
/*
* Course: CS1110
* Loop Exercise
* Fall 2023
*/
import java.util.Scanner;
/**
* This class will count the number of vowels (a, e, i, o, u)
* in a given word and display the result. It will do this
* three times, using a different type of loop each time
*/
public class Exercise3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean done = false;
do {
System.out.print("Enter a word of at least 3 letters or 'q' to quit: ");
String word = in.nextLine();
if(word.equalsIgnoreCase("q")) {
done = true;
}
if(word.length() >= 3) {
int vowels = 0;
// TODO: for loop
// TODO: while loop
// TODO: do-while loop
}
} while(!done);
System.out.println("Exiting...");
}
private static void report(String word, int vowels) {
// TODO: print out the results like this -> for the word Penguin, "Penguin contains 3 vowels."
}
private static boolean isVowel(char c) {
// TODO: return true if the character is an a, e, i, o, or u
}
}
Submission Instructions:
Submit Exercise3.java to Canvas.