In: Computer Science
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
>>> 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
>>> 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!
>>>
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