Assignment 12
Due Tuesday, May 26 at 11:59pm on Canvas


Important: To receive credit, you must only use concepts and methods taught in class to complete these assignments - or You Will Receive a 0

Pair Programming Required (or You Will Receive a 0)
  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document(s) along with your assignment code to Canvas.
  • No extra credit if you do not submit the contract along with your code to Canvas
  • Please make sure both your names are on your file (hint: use a comment).
  • If you need help finding a partner, please contact me as soon as possible.

Assignment 12: Strings in a Loop (10 pts)

  • In this program we will explore the many uses of for loops - including nested for loops!
  • Copy and paste the starter code into a file called StringLoops.java:
/**
 * @author
 * @author
 * CIS 36A, Assignment 12
 */

import java.util.Scanner;

public class StringLoops {

    public static void main(String[] args) {
        int n; // the integer number
        String word, sub, vowels = "";
        Scanner input = new Scanner(System.in);
        System.out.println("** Strings in a Loop!**\n");
        System.out.print("Enter an integer between 1 and 20: ");
        n = input.nextInt();

        System.out.print("Enter a word that is at least 2 letters long: ");
        word = input.next();
        System.out.println();

        sub = //find substring of first two letters in word here;

        System.out.println("The first two letters of " + word + " are: " + sub + "\n");

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

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

        // Repeating the character n times with minus sign (-) every 5 chars
        System.out.println("#3. Printing " + sub + " " + 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:\n");
        // Hint: put your for-loop from the previous challenge inside another
        // for-loop that has a different counting variable.
        // Put your code here
        System.out.println();

        System.out.println("#5. Printing all the vowels in " + word + ":\n");
        // Hint: use the concepts from lesson 12 to write a for loop
        //to traverse a String. Note that your for loop should have at least
        //one if statement. See Activity 12.2 for an example
        //Put your code here
       
        System.out.println(vowels + "\n");
       
        //Printing out the vowels n times, each set of vowels on its own line
        System.out.println("#6. Printing all the vowels in " + word + " " + n + " times:\n");
        //Put your code here
        System.out.println("\n");

        input.close();
    }
}
  • User input is already coded into the worksheet, consisting of an integer number, n, and a single word.
  • Do not add any other input commands or change the input order.
  • First, extract the first two characters in the word and save them as the sub variable
  • (#1) Use a for-loop to print sub the number of times specified by the integer number typed in. See the Example Run for an example.
  • (#2) Use a  for-loop to print sub n times with a '.' instead of the character on odd 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 for-loop to print sub n times with a minus sign '-' 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.
  • (#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.
  • (#5) Use a for-loop to go from character-to-character in word and extract the vowels. For each vowel you find, concatenate it (+) to the vowels variable. Inside the loop, you will need 5 if statements - one for each vowel - or one long if statement, with 5 test conditions
  • (#6) Use a for-loop, print the vowels variable n times.
Example Output 1:

** Loopy Strings!**

Enter an integer between 1 and 20: 15
Enter a word that is at least 2 letters long: Evidently

The first two letters of Evidently are: Ev

#1. Printing Ev 15 times:

EvEvEvEvEvEvEvEvEvEvEvEvEvEvEv

#2. Printing Ev 15 times substituting '.' on odd indexes:

.Ev.Ev.Ev.Ev.Ev.Ev.Ev.

#3. Printing Ev 15 times substituting (-) every fifth character:

EvEvEvEv-EvEvEvEv-EvEvEvEv-

#4. Printing 15 lines of the previous loop:

EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-
EvEvEvEv-EvEvEvEv-EvEvEvEv-


#5. Printing all the vowels in Evidently:

Eie

#6. Printing all the vowels in Evidently 15 times:

Eie
Eie
Eie
Eie
Eie
Eie
Eie
Eie
Eie
Eie
Eie
Eie
Eie
Eie
Eie


Example Output 2:

** Loopy Strings!**

Enter an integer between 1 and 20: 9
Enter a word that is at least 2 letters long: amazing

The first two letters of amazing are: am

#1. Printing am 9 times:

amamamamamamamamam

#2. Printing am 9 times substituting '.' on odd indexes:

.am.am.am.am.

#3. Printing am 9 times substituting (-) every fifth character:

amamamam-amamamam

#4. Printing 9 lines of the previous loop:

amamamam-amamamam
amamamam-amamamam
amamamam-amamamam
amamamam-amamamam
amamamam-amamamam
amamamam-amamamam
amamamam-amamamam
amamamam-amamamam
amamamam-amamamam


#5. Printing all the vowels in amazing:

aai

#6. Printing all the vowels in amazing 9 times:

aai
aai
aai
aai
aai
aai
aai
aai
aai


  • 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.
  • In the above example output, 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.