topics strings loops methods complex conditions loose pseudocode to code coding choices evaluation nested if vs if ese if borders outside loop vs inside loop using if statements 1

The output must be the exact same as the sample runs.

TASK 1 – VOWEL COUNT

Objective: Write a method that matches a specific signature. See an example of your method being called on user input and hardcoded data.

File: HW5_VowelCount.java
Finish the HW5_VowelCount.java program by writing the method called vowelsCt that takes one String argument and returns the number of vowels in that string. vowelsCt does NOT read any user input. Please read the main method code and see the comments explaining how your method is called. You will have to do similar calls in future tasks and exams.

For this assignment, the vowels are: a,e,i,o,u,y. (y will be considered a vowel). Vowels should be recognized and counted in both uppercase or lowercase form.
If available, you can use existing code (from class or your or the posted homework solution) to implement this assignment.
You must match the output as shown: first read the 2 words and then call the method on each of them and in the cardcoded string.

---------- Sample run:_x000D_
This program reads two strings and then calls a method to count how many vowels are in each one of them._x000D_
Enter a string: strEam_x000D_
Enter a string: cat_x000D_
Number of vowels in string strEam : 2_x000D_
Number of vowels in string cat : 1_x000D_
Number of vowels in the hardcoded string "I love coffee." : 6_x000D_

Grading criteria:

  1. 3 pts – code indentation
  2. 3 pts – correct method signature
  3. 4 pts – correct vowel count
  4. 2 pts – method returns the count
  5. 2 pts – method does not overwrite the given string and does not read user input
  6. 1 pt – other

TASK 2

Objective: call your method on data, save the result, use the saved result in future computations.

File: HW5_UpDownNone
In this program write a method called upDown that takes three integers as arguments and returns one of these 3 strings:

  • “increasing” if they are in strictly increasing order (note that 3,3,4 – are not strictly increasing),
  • “decreasing” if they are in strictly decreasing order.
  • “none” otherwise.
  • I recommend you use a complex condition to check for this (that is, have a single if statement with one big question composed of smaller ones). Can you write a single condition that checks that the numbers are in strictly increasing order? What is that in math? WHat is that in Java code? How about for strictly decreasing?

As you are designing your code, if you can accomplish the task with either nested if statements or multiple case (if-else-if), which one would you choose? Which one do you think makes the program less complex, easier to read and test for correctness? (TO DISCUSS FURTHER IN CLASS the complexity of nested if statements vs multiple choice.)
In the main method do the following (the instructions below are a looser form of pseudocode = instructions that tell what the program should do, without using the specific syntax of a programming language):

  • (3pts) read three integers from the user and call upDown for those integers, save the result in a variable, res1, and then print it.
  • (3pts) read again three integers from the user and call upDown for those integers, save the result in another variable, res2, and then print it.
  • (3pts) compare res1 and res2 and print “same” or “different”

Grading criteria

  1. 9 pts from above
  2. 3 pts – indentation (method, loop, if statement)
  3. 1 pt – other
  4. 6 pts – correct logic. Correct output for cases: strictly increasing (2pts), strictly decreasing (2pts), none (2pts).
  5. 6 pts – method signature and return:
    1. 2 pts – method header
    2. 2 pts – method does not overwrite arguments and does not read data from the user
    3. 2 pts – method retruns the string result
---------- Sample run 1:_x000D_
Enter three integers separated by spaces: 2 0 9_x000D_
These numbers are in order: none_x000D_
Enter three integers separated by spaces: 17 90 567_x000D_
These numbers are in order: increasing_x000D_
different_x000D_
_x000D_
---------- Sample run 2:_x000D_
Enter three integers separated by spaces: 90 9 1_x000D_
These numbers are in order: decreasing_x000D_
Enter three integers separated by spaces: 7 3 1_x000D_
These numbers are in order: decreasing_x000D_
same_x000D_
_x000D_
---------- Sample run 3:_x000D_
Enter three integers separated by spaces: 3 3 4_x000D_
These numbers are in order: none_x000D_
Enter three integers separated by spaces: 8 1 16_x000D_
These numbers are in order: none_x000D_
same_x000D_

TASK 3

Objective: Extend class work.
File: HW5_Cheer.java

The program below contains defines and calls a method called printCheer() (similar to what we did in class). Add to the program one more method called printCheerDiag will print the cheer diagonally. After you implement it, you should uncomment the line that calls printCheerDiag in main(). The program should run and print in both ways.

import java.util.Scanner;_x000D_
public class HW5_Cheer {_x000D_
    public static void main(String[] args) {_x000D_
        Scanner kb = new Scanner(System.in);_x000D_
        String banner, text;_x000D_
        int reps = 4;        _x000D_
        System.out.print("Enter your cheer: ");_x000D_
        text = kb.nextLine();        _x000D_
        System.out.print("Enter the number of repetitions: ");_x000D_
        reps = kb.nextInt();        _x000D_
        System.out.println("Calling printCheer");_x000D_
        printCheer(text, reps); // printCheer(text, 5);        _x000D_
        System.out.println("Calling printCheerDiag");_x000D_
        //printCheerDiag(text, 5);_x000D_
    }  // end of main()_x000D_
    _x000D_
    public static void printCheer(String cheer, int N){_x000D_
        int i;_x000D_
        for(i = 0; i ≤ N; i++){_x000D_
            System.out.println(cheer);_x000D_
        }_x000D_
    }  // end of printCheer method_x000D_
	_x000D_
    // Write the code for printCheerDiag_x000D_
}_x000D_

Here is a sample run of the program AFTER the new method is implemented. (I.e. this is a sample output of the solution.)

Enter your cheer: Go Mavs!_x000D_
Enter the number of repetitions: 5_x000D_
Calling printCheer_x000D_
Go Mavs!_x000D_
Go Mavs!_x000D_
Go Mavs!_x000D_
Go Mavs!_x000D_
Go Mavs!_x000D_
Calling printCheerDiag_x000D_
Go Mavs!_x000D_
 Go Mavs!_x000D_
  Go Mavs!_x000D_
   Go Mavs!_x000D_
    Go Mavs!_x000D_

Extra (no points for it): How can you print diagonally in the other direction. E.g.:

    Go Mavs!  _x000D_
   Go Mavs!_x000D_
  Go Mavs!_x000D_
 Go Mavs!_x000D_
Go Mavs!_x000D_

Grading criteria:

  1. 2 pts – indentation (method, loop)
  2. 1 pt – variable names
  3. 6 pts – method signature
  4. 6 pts – method printCheerDiag prints correctly

TASK 4

Objective: Loops and method calls,
File: HW5_RectangleFull.java

Write a program that has a method:

 _x000D_
public static void printRectangleFull(int height)_x000D_

and it prints a rectangle of 3xheight stars. E.g. for height = 5 it prints:

***_x000D_
***_x000D_
***_x000D_
***_x000D_
***_x000D_

In the main() method, repeatedly ask the user to enter height and use the printRectangleFull() to print a rectangle of height height.
If the user enters height ≤0, stop the repetition.

--------- Sample run:_x000D_
This program will repeatedly get an integer from the user and print a rectangle of that height.)_x000D_
Enter height: 5_x000D_
***_x000D_
***_x000D_
***_x000D_
***_x000D_
***_x000D_
Enter height: 2_x000D_
***_x000D_
***_x000D_
Enter height: 7_x000D_
***_x000D_
***_x000D_
***_x000D_
***_x000D_
***_x000D_
***_x000D_
***_x000D_
Enter height: 0_x000D_
Bye_x000D_

Extra (no points for it): How would you make this code more flexible, in particular:

  • Take the width also from the user (and pass it to the printRectangleFull())
  • Take the symbol also from the user. (E.g. print a rectangle of # or $). You can modify the printRectangleFull() method as needed.

Grading criteria

  1. 2 pts – indentation
  2. 6 pts – printRectangleFull()
    1. 2 pts – method header
    2. 2 pts – method does NOT read any user input
    3. 2 pts – rectangle is printed correct for N
  3. 7 pts – main
    1. 2 pts – prints program description at start and Bye at the end
    2. 2 pts – repeatedly read N from user (1 pts) and correctly call printRectangleFull (1 pt)
    3. 3 pts – loop repeats until user enters 0 (or less) for N

TASK 5

Objective: Loops, reproduce given output of a somewhat complex pattern. Use methods.
File: HW5_Rectangles.java

This solution must use methods.
It is recommended to use methods for this solution.
Write a program that reads an integer and displays, using asterisks, a filled and hollow square, placed next to each other. For example, if the side length is 5, the program should display:

*****  *****_x000D_
*****  *   *_x000D_
*****  *   *_x000D_
*****  *   *_x000D_
*****  *****_x000D_

You can assume that the user will enter a side length that is at least 2. If user enters a number smaller than 2, and your program has an incorrect behaviour, it is ok.
Write the methods:

  • fullBar that takes as argument one integer, N, and prints N stars (e.g. for N = 6 ******). (Should it or should it also move to a new line? Consider the final pattern you want to produce.) This method will NOT read any user input and does not return anything.
  • emptyBar that takes as argument one integer, N, and prints 2 stars with N-2 spaces between them (e.g. for N = 6 * *).
    This method will NOT read any user input and does not return anything.
    Treat the special cases (printing the starting star and the ending star) different from the repeated case (printing a space). Use a loop to print a space and before and after the loop print the stars.

In main use a loop and call these methods to produce the pattern shown in the sample run. As for emptyBar,use a loop to prit the identical lines (witha full and an empty bar) and print the special first and last line (with both bars full) outside that loop. This will make yoru code EASIER TO READ and understand.
Imagine you were given instructions to someone who cannot see the pattern (e.g. over the phone) to reproduce the pattern What would you tell them?. (INSTRUCTOR should discuss in class alternative bad code with if statment inside the loop. )
Grading criteria

  1. 3 pts – indentation
  2. 2 pts – comments & programm description
  3. 3 pts – matches sample output: prints program description, gets side length from user, prints Bye
  4. 5 pts – fullBar method:
    1. 2 pts – method signature. Both points will be lost if either of these happens: N is not an argument and method takes user input; or N is an argument but it is overwritten by either hardcoded data r user input.
    2. 3 pts – pattern,
  5. 7 pts – emptyBar method:
    1. 2 pts – method signature. Both points lost if N is not used as a proper function argument. (Same as for fullBar )
    2. 3 pts – pattern,
    3. 2 pts – first * and last * OUTSIDE THE LOOP.
  6. 9 pts – main method:
    1. 2 pts – prints first and last lines OUTSIDE THE LOOP
    2. 3 pts – methods are called correctly in main
    3. 4 pts – pattern
  7. 1 pt – other (e.g. other issues related to method usage, or program correctness or coding style, variable names)
------ Sample run 1_x000D_
This program will display a full and an empty square given the side length._x000D_
Enter the side length: 6_x000D_
******  ******_x000D_
******  *    *_x000D_
******  *    *_x000D_
******  *    *_x000D_
******  *    *_x000D_
******  ******_x000D_
Bye_x000D_
------ Sample run 2_x000D_
This program will display a full and an empty square given the side length._x000D_
Enter the side length: 10_x000D_
**********  **********_x000D_
**********  *        *_x000D_
**********  *        *_x000D_
**********  *        *_x000D_
**********  *        *_x000D_
**********  *        *_x000D_
**********  *        *_x000D_
**********  *        *_x000D_
**********  *        *_x000D_
**********  **********_x000D_
Bye_x000D_
------ Sample run 3_x000D_
This program will display a full and an empty square given the side length._x000D_
Enter the side length: 3_x000D_
***  ***_x000D_
***  * *_x000D_
***  ***_x000D_
Bye_x000D_

If you are having a hard time solving this problem, consider (solve) these simpler subproblems. It may give you a better understanding of the problem and what you can do to solve it.:

  1. Print only the filled square.
    *****_x000D_
    *****_x000D_
    *****_x000D_
    *****_x000D_
    *****_x000D_
    
  2. Print only the hollow square.
    *****_x000D_
    *   *_x000D_
    *   *_x000D_
    *   *_x000D_
    *****_x000D_
    
  3. Print the filled square first and the hollow one after.
    *****_x000D_
    *****_x000D_
    *****_x000D_
    *****_x000D_
    *****_x000D_
    _x000D_
    *****_x000D_
    *   *_x000D_
    *   *_x000D_
    *   *_x000D_
    *****_x000D_
    
  4. Print them next to each other as required:
    *****  *****_x000D_
    *****  *   *_x000D_
    *****  *   *_x000D_
    *****  *   *_x000D_
    *****  *****
 
Do you need a similar assignment done for you from scratch? We have qualified writers to help you. We assure you an A+ quality paper that is free from plagiarism. Order now for an Amazing Discount!
Use Discount Code "Newclient" for a 15% Discount!

NB: We do not resell papers. Upon ordering, we do an original paper exclusively for you.