Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this program, we will explore the many uses of for loops - including nested for loops. (Please use all the basic methods and concepts

  • In this program, we will explore the many uses of for loops - including nested for loops. (Please use all the basic methods and concepts to write the program, this is intro to Java. don't use high level method and concept )
  • Copy and paste the starter code into a file called StringLoops.java:

/** * @author * CIS 36A, Assignment 13.1 */ 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!** "); 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 + " "); // Repeating the sub n times with a for-loop. System.out.println("#1. Printing " + sub + " " + n + " times: ");

//Put your code here

System.out.println(" "); // Repeating the sub n times with dot on odd indexes. System.out.println("#2. Printing " + sub + " " + n + " times substituting '.' on odd indexes: "); // Put your code here System.out.println(" "); // Repeating the character n times with minus sign (-) every 5 chars System.out.println("#3. Printing " + sub + " " + n + " times substituting (-) every fifth character: "); // Put your code here System.out.println(" "); 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 System.out.println(); System.out.println("#5. Printing all the vowels in " + word + ": "); // Hint: use the concepts from lesson 13 to write a for loop //to traverse a String. Note that your for loop should have at least //one if statement. See Activity 13.2 for an example

//Put your code here

System.out.println(vowels + " "); //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: ");

//Put your code here

System.out.println(" "); 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual C# And Databases

Authors: Philip Conrod, Lou Tylee

16th Edition

ISBN: 1951077083, 978-1951077082

More Books

Students also viewed these Databases questions