Assignment 15
Due Thursday, May 31 at 1:20pm on Canvas

Assignment 15.1: Loopy Characters (10 pts)

  • Counting loops are very commonly used in programming to ... count things.
  • In this program we will explore various ways to count in Java using for loops.
  • Copy and paste the starter code into a file called LoopChars.java:
import java.util.Scanner;

public class LoopChars {

    public static void main(String[] args) {
        int n; // the integer number
        String ch; // the single character
        Scanner input = new Scanner(System.in);
        System.out.println("** Loopy Characters!**\n");
        System.out.print("Enter an integer between 1 and 20: ");
        n = input.nextInt();

        //Put your code here to test for input mismatch exception (String entered, not number)

        System.out.print("Enter a single character: ");
        ch = input.next();
        System.out.println();

        // Repeating the char n times with a for-loop.
        System.out.println("#1. Printing " + ch + " " + n + " times:");
        // Put your code here
        System.out.println("\n");

        // Repeating the char n times with stars on odd indexes.
        System.out.println("#2. Printing " + ch + " character " + n + " times substituting '*' on odd indexes:");
        // Put your code here
        System.out.println("\n");

        // Repeating the character n times with tick marks (+) every 5 chars
        System.out.println("#3. Printing " + ch + " character " + n + " times substituting (+) every fifth character:\n");
        // Put your code here
        System.out.println("\n");

        System.out.println("#4. Printing " + n + " lines of the previous loop:");
        // Hint: put your for-loop from the previous challenge inside another
        // for-loop that has a different counting variable.
        // Put your code here

       input.close();
    }
}

  • User input is already coded into the worksheet, consisting of an integer number, n, and a single character.
  • Do not add any other input commands or change the input order.
  • Use a while loop to test for input mismatch exception if the user enters a String instead of integer data
  • (#1) Use a for-loop to print the character entered by the user the number of times specified by the integer number typed in. See the Example Run for an example.
  • (#2) Use a third for-loop to print the character n / 2 times with a '*' instead of the character on even counts of the loop as shown in the Example Run.
    • Hint: Use and if-statement to test for even or odd counts of the loop.
  • (#3) Use a fourth for-loop to print the character n times with a tick mark '+' substituted every fifth character as shown in the Example Run.
    • Hint: Use and if-statement to test for the every fifth count of the loop. Start the for-loop count from 1, but check the end condition! Only print the number if nothing else is printed.
  • (#4) Use yet another loop to print the previous loop n times. Thus, this problem should use two loops.
  • Hint: put your for-loop from the previous challenge inside another for-loop that has a different counting variable. Print a newline character inside the outer loop after the inner loop completes.
  • Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.


**Loopy Characters!**

Enter an integer between 1 and 20: ten

Please enter numbers not characters.

Enter an integer between 1 and 20: bob

Please enter numbers not characters.

Enter an integer between 1 and 20: 10

Enter a single character: A

#1. Printing A 10 times:
AAAAAAAAAA

#2. Printing A character 10 times substituting '*' on odd indexes:
A*A*A*A*A*

#3. Printing A character 10 times substituting (+) every fifth character:
AAAA+AAAA+

#4. Printing 10 lines of the previous loop:
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+
AAAA+AAAA+

    

  • In the above example run, the user entered the values shown in italics (for emphasis) to produce the output. Your program does NOT print the characters in italics, nor does the user input appear in italics.

  • After displaying the output, exit the program. Submit your file to Canvas when you are finished.

Assignment 15.2: More Sentences (10 pts)
  • Write a program that allows a user to enter a sentence and then the position of two characters in the sentence.
  • The program should then report whether the two characters are identical or different.
  • When the two characters are identical, the program should display the message:
<char> and <char> are identical!
  • Note that <char> should be replaced with the characters from the String. See example output below for more information.
  • When the two characters are different, the program should display the message:
<char> and <char> are unique characters.
  • Note that <char> should be replaced with the characters from the String. See example output below for more information.
  • Name your program Sentences.java
  • Your program must use a do-while loop to allow the user to enter a series of sentences, or "X" to exit.
    • The program should accept both lower and upper case "X" as an indication the user wishes to exit the program.
    • Please see example output below
  • It should also verify that the user provides numerical input for the positions of the characters in the String.
    • Hint: Use a while loop and !input.nextInt() to check for input mismatch exception.
    • See Lesson 15 notes for a discussion on input mismatch exception
    • You will need two while loops, one for each input.nextInt() statement for the numeric location of each character in the String
  • Important: Note that you will need to use input.nextLine() to account for the fact that the loop causes an input.nextInt() to come before an input.nextLine().
    • See lesson 15 notes regarding the problem that can occur when you use input.nextInt() before an input.nextLine()
  • When your program runs identically to the example output below, submit it to Canvas.

Your output should look identical to the following, except user input will vary:

Welcome!
This program compares two letters in a sentence.

Enter a sentence or X to exit: Let's have cake and ice cream for dinner!
Enter the numeric location of the first letter: 1
Enter the numeric location of the second letter: 9

e and e are identical.

Enter a sentence or X to exit: "Flight Behavior" is a good book.
Enter the numeric location of the first letter: six

Error! Enter a number, not text!
Enter the numeric location of the first character: six

Error! Enter a number, not text!
Enter the numeric location of the first character: six

Error! Enter a number, not text!
Enter the numeric location of the first character: 6
Enter the numeric location of the second letter: 10

t and h are unique characters.

Enter a sentence or X to exit: Summer vacation is almost here!
Enter the numeric location of the first letter: 2
Enter the numeric location of the second letter: three

Error! Enter a number, not text!
Enter the numeric location of the second character: three

Error! Enter a number, not text!
Enter the numeric location of the second character: three

Error! Enter a number, not text!
Enter the numeric location of the second character: 3

m and m are identical.

Enter a sentence or X to exit: x
Goodbye!