Java strings, loops, methods, complex conditions, loose pseudocode to code, coding choices evaluation

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:

This program reads two strings and then calls a method to count how many vowels are in each one of them.

Enter a string: strEam

Enter a string: cat

Number of vowels in string strEam : 2

Number of vowels in string cat : 1

Number of vowels in the hardcoded string "I love coffee." : 6

Grading criteria:

 

3 pts - code indentation

3 pts - correct method signature

4 pts - correct vowel count

2 pts - method returns the count

2 pts - method does not overwrite the given string and does not read user input

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

 

9 pts from above

3 pts - indentation (method, loop, if statement)

1 pt - other

6 pts - correct logic. Correct output for cases: strictly increasing (2pts), strictly decreasing (2pts), none (2pts).

6 pts - method signature and return:

2 pts - method header

2 pts - method does not overwrite arguments and does not read data from the user

2 pts - method retruns the string result

---------- Sample run 1:

Enter three integers separated by spaces: 2 0 9

These numbers are in order: none

Enter three integers separated by spaces: 17 90 567

These numbers are in order: increasing

different

 

---------- Sample run 2:

Enter three integers separated by spaces: 90 9 1

These numbers are in order: decreasing

Enter three integers separated by spaces: 7 3 1

These numbers are in order: decreasing

same

 

---------- Sample run 3:

Enter three integers separated by spaces: 3 3 4

These numbers are in order: none

Enter three integers separated by spaces: 8 1 16

These numbers are in order: none

same

 

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;

public class HW5_Cheer {

    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        String banner, text;

        int reps = 4;        

        System.out.print("Enter your cheer: ");

        text = kb.nextLine();        

        System.out.print("Enter the number of repetitions: ");

        reps = kb.nextInt();        

        System.out.println("Calling printCheer");

        printCheer(text, reps); // printCheer(text, 5);        

        System.out.println("Calling printCheerDiag");

        //printCheerDiag(text, 5);

    }  // end of main()

    

    public static void printCheer(String cheer, int N){

        int i;

        for(i = 0; i ≤ N; i++){

            System.out.println(cheer);

        }

    }  // end of printCheer method

 

    // Write the code for printCheerDiag

}

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!

Enter the number of repetitions: 5

Calling printCheer

Go Mavs!

Go Mavs!

Go Mavs!

Go Mavs!

Go Mavs!

Calling printCheerDiag

Go Mavs!

 Go Mavs!

  Go Mavs!

   Go Mavs!

    Go Mavs!

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

 

    Go Mavs!  

   Go Mavs!

  Go Mavs!

 Go Mavs!

Go Mavs!

Grading criteria:

 

2 pts - indentation (method, loop)

1 pt - variable names

6 pts - method signature

6 pts - method printCheerDiag prints correctly

 

TASK 4

Objective: Loops and method calls,

File: HW5_RectangleFull.java

 

Write a program that has a method:

 

 

public static void printRectangleFull(int height)

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

 

***

***

***

***

***

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:

This program will repeatedly get an integer from the user and print a rectangle of that height.)

Enter height: 5

***

***

***

***

***

Enter height: 2

***

***

Enter height: 7

***

***

***

***

***

***

***

Enter height: 0

Bye

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

 

2 pts - indentation

6 pts - printRectangleFull()

2 pts - method header

2 pts - method does NOT read any user input

2 pts - rectangle is printed correct for N

7 pts - main

2 pts - prints program description at start and Bye at the end

2 pts - repeatedly read N from user (1 pts) and correctly call printRectangleFull (1 pt)

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:

 

*****  *****

*****  *   *

*****  *   *

*****  *   *

*****  *****

 

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

 

3 pts - indentation

2 pts - comments & programm description

3 pts - matches sample output: prints program description, gets side length from user, prints Bye

5 pts - fullBar method:

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.

3 pts - pattern,

7 pts - emptyBar method:

2 pts - method signature. Both points lost if N is not used as a proper function argument. (Same as for fullBar )

3 pts - pattern,

2 pts - first * and last * OUTSIDE THE LOOP.

9 pts - main method:

2 pts - prints first and last lines OUTSIDE THE LOOP

3 pts - methods are called correctly in main

4 pts - pattern

1 pt - other (e.g. other issues related to method usage, or program correctness or coding style, variable names)

------ Sample run 1

This program will display a full and an empty square given the side length.

Enter the side length: 6

******  ******

******  *    *

******  *    *

******  *    *

******  *    *

******  ******

Bye

------ Sample run 2

This program will display a full and an empty square given the side length.

Enter the side length: 10

**********  **********

**********  *        *

**********  *        *

**********  *        *

**********  *        *

**********  *        *

**********  *        *

**********  *        *

**********  *        *

**********  **********

Bye

------ Sample run 3

This program will display a full and an empty square given the side length.

Enter the side length: 3

***  ***

***  * *

***  ***

Bye

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.:

 

Print only the filled square.

*****

*****

*****

*****

*****

Print only the hollow square.

*****

*   *

*   *

*   *

*****

Print the filled square first and the hollow one after.

*****

*****

*****

*****

*****

 

*****

*   *

*   *

*   *

*****

Print them next to each other as required:

*****  *****

*****  *   *

*****  *   *

*****  *   *

*****  *****

Need a custom answer at your budget?

This assignment has been answered 4 times in private sessions.

© 2024 Codify Tutor. All rights reserved