Question

In: Computer Science

The primary objective of this assignment is to reinforce the concepts of string processing. Part A:...

The primary objective of this assignment is to reinforce the concepts of string processing.

Part A: Even or Odd

For this first part, write a function that will generate a random number within some range (say, 1 to 50), then prompt the user to answer if the number is even or odd. The user will then input a response and a message will be printed indicated whether it was correct or not. This process should be repeated 5 times, using a new random number for each play, regardless of whether the user was correct or not.

Notes

  • A correct response for an even number can be any of the following: “even”, “Even”, “EVEN”, “e”, “E”. “EiEiO”. The main criterion is that a correct response begins with the letter “e”, regardless of case. The same goes for an odd number – a correct response should begin with the letter “o”.
  • If the user enters an invalid response, they should be notified. However, an invalid response still counts as one of the 5 plays.
  • The solution should contain a loop (for or while) and a few if statements. An example execution is shown below:

>>> assign2PartA()
Is 41 odd or even? Odd
   Correct
Is 48 odd or even? Even
   Correct
Is 3 odd or even? e
   Incorrect
Is 20 odd or even? o
   Incorrect
Is 42 odd or even? xyz
   You did not enter a correct reply.
     Please answer using Odd or Even

Part B: Vowel Counting

The second task is to write a function that counts, and prints, the number of occurrences for each vowel (a, e, i, o and u) in a given phrase and outputs a (new) phrase with the vowels removed. The phrase should be accepted as a parameter to the function. If the (original) phrase is empty, you should output an appropriate error message, and obviously with no vowel counts. If the phrase contains no vowels, a message should be displayed, and the count can be omitted – since there is no point in displaying 0 for each vowel! A message should also be displayed if the phrase contains only vowels, but the counts should still be displayed.

Notes

  • Recall that you can use the tab escape character (“\t”) for spacing to align the text, or use a given width in the f-string (print(f"{count_a:4}, ……”), where the value of count_a here is right justified in 4 columns.)
  • Be sure to correctly handle both upper- and lower-case letters (e.g. both “a” and “A” should be counted as an instance of the letter “A”.) The new phrase (with vowels removed) should preserve the letter cases from the original phrase. Be sure to show output for all possible scenarios in your submitted output. A few example executions are shown below. In the first example, the escape sequence \" allows for inclusion of a " within a string delimited by two ", see slide 9 in “Working with Strings.”

>>> assign2PartB("Remember that context here defines \"+\" as 'Concatenate'")
   A    E    I    O    U
   4   10    1    2    0

The original phrase is: Remember that context here defines "+" as 'Concatenate'
The phrase without vowels is: Rmmbr tht cntxt hr dfns "+" s 'Cnctnt'

>>> assign2PartB("bcdfghjklmnpqrstvwxyz")

The phrase contains no vowels: bcdfghjklmnpqrstvwxyz

>>> assign2PartB("aeiouAEIOU")
   A    E    I    O    U
   2    2    2    2    2

The phrase contains only vowels: aeiouAEIOU

>>> assign2PartB("")

The input phrase is empty!

>>>

Solutions

Expert Solution

Part A solution:

import java.util.*; 
  
public class PartA { 
  
    public static void main(String args[]) { 
        
        // create instance of Random class 
        Random rand = new Random(); 
        
        // Standard input stream
        Scanner sc = new Scanner(System.in);

        for(int i=0; i<5; i++) {
        
                // Generate random integers in range 0 to 50 
                int number = rand.nextInt(50) + 1;
                
                System.out.print("Is " + number + " odd or even : ");
                String res = sc.nextLine();
                String response = res.toLowerCase();

                
                // To check whether user enter a input or not
                if(response.isEmpty()) {
                        System.out.println("Please enter a response!!");
                        break;
                }
  
                if(number%2 == 0) {
                        if(response.charAt(0) == 'e')
                                System.out.println(" CORRECT ");
                        else
                                System.out.println(" Incorrect ");
                }

                else {
                        if(response.charAt(0) == 'o')
                                System.out.println(" CORRECT ");
                        else
                                System.out.println(" INCORRECT ");
                }
        }
    }
}

Output (PartA):

Part B Solution:

import java.util.*;

public class PartB { 
         
    // Function to check the Vowels, eliminate them in final phrase and getting a output into desired format.
    static void vowelCheck(String str) 
    { 
        String phrase = str.toUpperCase(); 
        int len = phrase.length();

        // counters to count the number of vowels individually
        int a_count = 0, e_count = 0, i_count = 0, o_count = 0, u_count = 0;
        String final_phrase = "";
        
        for(int i=0; i<len; i++) {
                if(phrase.charAt(i) == 'A' ) 
                        a_count++;
                else if(phrase.charAt(i) == 'E') 
                        e_count++;
                else if(phrase.charAt(i) == 'I') 
                        i_count++;
                else if(phrase.charAt(i) == 'O') 
                        o_count++;
                else if(phrase.charAt(i) == 'U') 
                        u_count++;
                else
                        final_phrase = final_phrase + str.charAt(i);
        }
        // for case if user enters nothing
        if(str.isEmpty())
                System.out.println("The input Phrase is empty.");

        // for case when user enters a phrase with no vowels
        else if(a_count ==0 && e_count == 0 && i_count == 0 && o_count == 0 && u_count == 0)
                System.out.println("This Phrase contains no vowels: " + final_phrase); 

        // for case when user enters a phrase with only vowels
        else if(final_phrase.isEmpty() && str != null ) {
                System.out.print("A \t E \t I \t O \t U \n");
                System.out.println(a_count + " \t " + e_count + " \t " + i_count + " \t " + o_count + " \t " + u_count );
                System.out.println("This Phrase contains only vowels: " +str);
        }

        // for every other case
        else {  
                System.out.print("A \t E \t I \t O \t U \n");
                System.out.println(a_count + " \t " + e_count + " \t " + i_count + " \t " + o_count + " \t " + u_count );
                System.out.println("The Phrase without vowels is: " + final_phrase);
        }    
    } 
       
    // Driver code 
    public static void main(String args[]) 
    { 
        Scanner sc = new Scanner(System.in);
                
                System.out.print("Enter a Phrase : ");
                String response = sc.nextLine();
                
                vowelCheck(response);
                
                
    } 
} 

Output(Part B):

I hope this helps you. As there is no mention of which language to use, i used java. but the purpose for the assignment of reinforcing concept of string processing is fully kept in mind while solving. Code is properly indented and comments are also present between the code for better understanding. Thank you


Related Solutions

The primary objective of this assignment is to reinforce the concepts of string processing. Part A:...
The primary objective of this assignment is to reinforce the concepts of string processing. Part A: Even or Odd [10 marks] For this first part of the assignment, write a function that will generate a random number within some range (say, 1 to 50), then prompt the user to answer if the number is even or odd. The user will then input a response and a message will be printed indicated whether it was correct or not. This process should...
Using Python 3 The primary objective of this assignment is to reinforce the concepts of string...
Using Python 3 The primary objective of this assignment is to reinforce the concepts of string processing. Part A: Even or Odd For this first part, write a function that will generate a random number within some range (say, 1 to 50), then prompt the user to answer if the number is even or odd. The user will then input a response and a message will be printed indicated whether it was correct or not. This process should be repeated...
The purpose of this assignment is to reinforce Ada concepts. Define a Complex-numbers package includes the...
The purpose of this assignment is to reinforce Ada concepts. Define a Complex-numbers package includes the following operations: Addition Subtraction Multiplication Division A “main program” needs to create one or more complex numbers, perform the various arithmetic operations, and then display results. Develop the program in ADA code with several packages. Write a short report that documents all work done, including the overall design, explanation of the implementation, the input data used to run the program, the output of the...
The objective of this homework assignment is to demonstrate proficiency with reading files, and using string...
The objective of this homework assignment is to demonstrate proficiency with reading files, and using string methods to slice strings, working with dictionaries and using-step wise development to complete your program. Python is an excellent tool for reading text files and parsing (i.e. filtering) the data. Your assignment is to write a Python program in four steps that reads a Linux authentication log file, identifies the user names used in failed password attempts and counts the times each user name...
Objective This assignment aims to investigate the concepts in solving problems on the special theory of...
Objective This assignment aims to investigate the concepts in solving problems on the special theory of relativity, analyze the effects of relativity and evaluate the validity of results on the application of relativity. Problem Solving, Analysis and Evaluating of the Validity of Results Direction: Solve the given problem using systematic/logical solution. Make an analysis and evaluation of the results. Rubrics will be used in marking. (1) Determine Ƴ if v = 0.01c; 0.1c; 0.5c; 0.6c; 0.8c; 0.9c; 0.99c; 1.00c; and...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple HR application using the C++ programming language and Oracle server. This assignment helps students learn a basic understanding of application development using C++ programming and an Oracle database Submission: This Milestone is a new project that simply uses what was learned in the SETUP. Your submission will be a single text-based .cpp file including your C++ program for the Database Application project/assignment. The file...
The purpose of this problem set is to reinforce your knowledge of some basic chemical concepts...
The purpose of this problem set is to reinforce your knowledge of some basic chemical concepts that are important for the origin of the elements. 1. Use the abundances of the stable isotopes of strontium and the masses of these nuclides (found at http://atom.kaeri.re.kr/nuchart/) to calculate the atomic weight of strontium. Compare the value that you get with the value shown in the periodic table found at http://www.rsc.org/periodic-table. Show your work. 2. Consult the chart of the nuclides (http://atom.kaeri.re.kr/nuchart/ )...
The primary objective in setting transfer prices is to _______
The primary objective in setting transfer prices is to _______  A) establish a system that determines the best transfer prices for the company as a whole B) evaluate the managers of the responsibility centers involved C) achieve goal congruence by selecting a price that will maximize overall company profits D) make it easy for managers to select prices that maximize division profits
Objective: The objective of this assignment is to allow students to understand the basic principles of...
Objective: The objective of this assignment is to allow students to understand the basic principles of arrays Program Requirements: The assignment will require the students use arrays to perform mathematical calculations. Program Setup: Functions to include in the program The program will have three functions Function 1: Responsible for setting values in the arrays Function 2: Responsible for printing values in arrays Function 3: Responsible for doing calculations to arrays Function 1 Details: Setting array values Parameters: 3 Array of...
Composite engineering In a design assignment, you are asked to use continuous carbon fibres to reinforce...
Composite engineering In a design assignment, you are asked to use continuous carbon fibres to reinforce an epoxy matrix to achieve Young's modulus of 250 GPa along the longitudinal direction of the composite. The epoxy matrix to use has a Young's modulus of 2 GPa and the continuous carbon fibres 400 GPa. If the allowed maximum loading of the C fibres is 60 vol%, is the design goal achievable? You are required to derive any equation needed in the calculation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT