Assignment 16
Due Tuesday, June 5 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 16.1: Vowels Again

  • Write a method with the following signature:
    public static int countVowels(string str) 
  • The method returns a count of the vowel letters in the string parameter that are vowels (A, E, I, O, U, a, e, i, o, u)
  • Hint: You've done this same thing on your previous assignment, just not with a method.
  • For this assignment we are going to "outsource" the work of counting vowels to a method named countVowels.
  • Copy and paste the starter code inside the {} of a class named Vowels either above or below the main method:

public static int countVowels(string str)
{
    //add the code for your method here

}

  • Cut and paste the for loop from main and place it inside the {} of the method.
  • Then, in main, call the method to replace the for loop.
  • Verify that your new version of the program still works identically to Assignment 14.3
Welcome! Give me a sentence and I will count its vowels.

Please enter a sentence or q to quit: Power to the people!
There are 7 vowels in your sentence.

Please enter a sentence or q to quit: Chillax!
There are 2 vowels in your sentence.

Please enter a sentence or q to quit: Animals are friends not food.
There are 10 vowels in your sentence.

Please enter a sentence or q to quit: q

Goodbye!

  • When you are certain it is working properly submit to Canvas.


Assignment 16.2: Getting Methodical (10 pts) <--Due Thursday, June 7

  • Copy and paste the starter code into a new file called Methodical.java
  • Write the required methods as described by the comments (i.e. you fill in the body of the method).
  • Then, run the code when you are finished to see if you wrote the methods correctly.
  • Check the test results and make any alterations to your methods as necessary.
  • When all of the tests pass, upload your code to Canvas.


/*
 * @author
 * CIS 36A
 *
 */

public class Methodical {
    /*
     * Given two int values, returns their sum.
     * Unless the two values are the same, then return double their sum.
     * sumDouble(1, 2) → 3
     * sumDouble(3, 2) → 5
     * sumDouble(2, 2) → 8
     */
   
    public static int sumDouble(int a, int b){
        return 0;
    }
   
    /*
     * Given 2 ints, a and b, return true if one of them is 10
     * or if their sum is 10.
     * makes10(9, 10) → true
     * makes10(9, 9) → false
     * makes10(1, 9) → true
     */

    public static boolean makes10(int a, int b) {
        return false;
    }
    /*
     * We have two monkeys, a and b, and the parameters
     * aSmile and bSmile indicate if each is smiling.
     * We are in trouble if they are both smiling
     * or if neither of them is smiling.
     * Return true if we are in trouble.
     * monkeyTrouble(true, true) → true
     * monkeyTrouble(false, false) → true
     * monkeyTrouble(true, false) → false
     */

    public static boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
        return false;
    }
   
    /*Return true if the given non-negative number is a multiple of 3
    * or a multiple of 5. Use the % "modulus" operator
    * or35(3) → true
    * or35(10) → true
    * or35(8) → false
    * */

    public static boolean or35(int a) {
        return false;
    }

    /*
     * Given a string, return a new string where "not " has been added to the front.
     * However, if the string already begins with "not", return the string unchanged.
     * notString("candy") → "not candy"
     * notString("x") → "not x"
     * notString("not bad") → "not bad"
     */
   
    public static String notString(String str) {
        return "";
    }

    /*
     * Given a string, return a new string where the first and last chars have been exchanged
     * frontBack("code") → "eodc"
     * frontBack("a") → "a"
     * frontBack("ab") → "ba"
     */
   
    public static String frontBack(String str) {
        return "";
    }
   
    /*
     * We'll say that a number is "teen" if it is in the range 13..19 inclusive.
     * Given 3 int values, return true if 1 or more of them is/are teen.
     * hasTeen(13, 20, 10) → true
     * hasTeen(20, 19, 10) → true
     * hasTeen(20, 10, 13) → true
     */

    public static boolean hasTeen(int num1, int num2, int num3) {
        return false;
    }
   



    public static void main(String[] args)
    {
        int result;
        boolean answer;
        String value;
        System.out.println("***Testing sumDouble***\n");
        result = sumDouble(1, 2);
        System.out.println("Should print 3: " + result);
        result = sumDouble(3, 2);
        System.out.println("Should print 5: " + result);
        result = sumDouble(2, 2);
        System.out.println("Should print 8: " + result +"\n");
      
        System.out.println("***Testing makes10***\n");
        answer = makes10(9, 10);
        System.out.println("Should be true: " + answer);
        answer = makes10(9, 9);
        System.out.println("Should be false: " + answer);
        answer = makes10(1, 9);
        System.out.println("Should be true: " + answer + "\n");
          
        System.out.println("***Testing monkeyTrouble***\n");
        answer = monkeyTrouble(true, true);
        System.out.println("Should be true: " + answer);
        answer = monkeyTrouble(false, false);
        System.out.println("Should be true: " + answer);
        answer = monkeyTrouble(true, false);
        System.out.println("Should be false: " + answer + "\n");
      
        System.out.println("***Testing or35***\n");
        answer = or35(3);
        System.out.println("Should be true: " + answer);
        answer = or35(10);
        System.out.println("Should be true: " + answer);
        answer = or35(8);
        System.out.println("Should be false: " + answer + "\n");
      
        System.out.println("***Testing notString***\n");
        value = notString("candy");
        System.out.println("Should be not candy: " + value);
        value = notString("x");
        System.out.println("Should be not x: " + value);
        value = notString("not bad");
        System.out.println("Should be not bad: " + value + "\n");
      
        System.out.println("***Testing frontBack***\n");
        value = frontBack("code");
        System.out.println("Should be eodc: " + value);
        value = frontBack("a");
        System.out.println("Should be a: " + value);
        value = frontBack("ab");
        System.out.println("Should be ba: " + value + "\n");
      
      
        System.out.println("***Testing hasTeen***\n");
        answer = hasTeen(13, 20, 10);
        System.out.println("Should be true: " + answer);
        answer = hasTeen(20, 19, 10);
        System.out.println("Should be true: " + answer);
        answer = hasTeen(20, 10, 13) ;
        System.out.println("Should be true: " + answer);
        answer = hasTeen(20, 10, 45) ;
        System.out.println("Should be false: " + answer + "\n");
      
        System.out.println("***End of Tests***");
      
    }
   
}