import java.util.Scanner; /* YOUR NAME Loop Hwk #1 Comp 110 * Assigned: Mon 2/21 * Due: Fri 2/25 at start of lab * You may work together on these problems, but each student * must turn in their solution on Sakai. * This homework is just practice writing loops in Java. * You do not need an Implementation Plan or a Test Plan. * Just complete the Java code for each of the methods. */ public class LoopHwk1SOLUTION { public static void main(String[] args) { // Change the name of this method (e.g. question1) to // run your code for a different question on this assignment. question0(); } /* * Ask the user to "Enter a positive integer" Print out the sum of the first * n odd integers where n is the number entered by the user. For example, if * the user enters 5, print out 25 = 1 + 3 + 5 + 7 + 9 */ public static void question0() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a positive integer"); int n = keyboard.nextInt(); // Development of solution to question 0 on web page for class 11 } /* * Question 1 Ask the user to enter a string. Print out the characters of * the string one per line followed by the number of characters. For * example, input "cat" produces output c a t 3 */ public static void question1() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a string "); String input = keyboard.nextLine(); // Development of solution to question 1 on web page for class 12 } /* * Question 2 Ask the user to enter a string. Print the characters in * reverse order on a single line followed by the number of characters. For * example, input "cat" produces output tac 3 */ public static void question2() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a string "); String input = keyboard.nextLine(); int index = input.length() - 1; while (index >= 0) { System.out.print(input.charAt(index)); index--; } System.out.println(" " + input.length()); } /* * Problem - count the number of 'e's in a string Ask the user to enter a * string. Print the number of times the letter 'e' appears in the string. * For example, input "seerer" produces the output the letter e occurs 3 * times in seerer */ /* * Question 3 Count the number of 'e's using a 'while' loop. */ public static void question3() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a string "); String input = keyboard.nextLine(); int index = 0; int count = 0; while (index < input.length()) { if (input.charAt(index) == 'e') count++; index++; } System.out.println("e occurred " + count + " times"); } /* * Question 4 Count the number of 'e's using a 'for' loop. */ public static void question4() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a string "); String input = keyboard.nextLine(); int count = 0; for (int index = 0; index < input.length(); index++) if (input.charAt(index) == 'e') count++; System.out.println("e occurred " + count + " times"); } /* * Problem - count the number of vowels in a string. Ask the user to enter a * string. Print the number of vowels in the string. Count only aeiou not y. * For example, the input "anapolis" produces output * * there are 4 vowels in anapolis */ /* * In questions 5, 6, and 7, you may use any type of loop you wish: while, * do-while, or for */ /* * Question 5 - count vowels. Use only nested if statements (no compound * conditions) */ public static void question5() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a string "); String input = keyboard.nextLine(); int count = 0; for (int index = 0; index < input.length(); index++) { if (input.charAt(index) == 'a') count++; else if (input.charAt(index) == 'e') count++; else if (input.charAt(index) == 'i') count++; else if (input.charAt(index) == 'o') count++; else if (input.charAt(index) == 'u') count++; } System.out.println(count + " vowels found"); } /* * Question 6 - count vowels. Use only one if statement with a compound * condition */ public static void question6() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a string "); String input = keyboard.nextLine(); int count = 0; for (int index = 0; index < input.length(); index++) { if (input.charAt(index) == 'a' || input.charAt(index) == 'e' || input.charAt(index) == 'i' || input.charAt(index) == 'o' || input.charAt(index) == 'u') count++; } System.out.println(count + " vowels found"); } /* * Question7 - count vowels. Use only chained if statements (no compound * conditions) */ public static void question7() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a string "); String input = keyboard.nextLine(); int count = 0; for (int index = 0; index < input.length(); index++) { if (input.charAt(index) == 'a') count++; if (input.charAt(index) == 'e') count++; if (input.charAt(index) == 'i') count++; if (input.charAt(index) == 'o') count++; if (input.charAt(index) == 'u') count++; } System.out.println(count + " vowels found"); } }