CPS 180-Programming Assignment #5 Assigned: 05/31/17 Due: 06/07/17 Points: 40 Purpose: Use math functions, string manipulation and input/output to create an alternative to Twitter called Chipper. Requirements: 1. Ask the user for 3 messages to send using the following characteristics: a. Generate a random number to ask the user for a of chars for the message. The random number should be between l and 50. Use Math random to generate 3 numbers: randi, rand2, rand3 b. Have the user type the strings into 3 separate variables c. Find the length of the string and report if the string is too long, then truncate it and reassign it to the string 2. String variables: Keep track of all three messages in msgl, msg2, msg3 strings. Also combine (concatenate) all 3 messages into 1 string called msgAll 3. int variables: Keep track of the lengths of all three messages (after truncating) in msglLength, msg2Length, msg3Length 4. Counters a. Count and report the total of characters in msgAll. b. Count and report the number of vowels in msgAll (this will require a loop). c. Count and report the number of spaces in msgAll (this will require a loop). 5. Display msgAll backwards (this will require a loop). 6. Display msgAll Vertically (one char per line). Sample output (user input in bold): Note: The output is incomplete, just use as a reference. Message #1: Enter a message with 18 chars or less:
Expert Answer
public class Chipper {
public static void main(String[] args) {
int range = (50 – 1) + 1; // 50 is max value 1 is min value
int rand1 = (int) (Math.random() * range) + 1;// 1.a)gives a random
// number between 1 and
// 50
int rand2 = (int) (Math.random() * range) + 1;
int rand3 = (int) (Math.random() * range) + 1;
String msg1 = null;// 1.b) all strings in three input variables
String msg2 = null;
String msg3 = null;
Scanner in = new Scanner(System.in);
System.out.println(“Enter a message with ” + rand1 + ” character or less”);
msg1 = in.nextLine();
if (msg1.length() > rand1) {
System.out.println(“Oops! youe message is more than ” + rand1 + ” characters! it has been truncated to:”);
msg1 = msg1.substring(0, rand1);// 1.c) truncate string if
// length is more than random
// variable
System.out.println(msg1);
}
System.out.println(“Enter a message with ” + rand2 + ” character or less”);
msg2 = in.nextLine();
if (msg2.length() > rand2) {
System.out.println(“Oops! youe message is more than ” + rand2 + ” characters! it has been truncated to:”);
msg2 = msg2.substring(0, rand2);
System.out.println(msg2);
}
System.out.println(“Enter a message with ” + rand3 + ” character or less”);
msg3 = in.nextLine();
if (msg3.length() > rand3) {
System.out.println(“Oops! youe message is more than ” + rand3 + ” characters! it has been truncated to:”);
msg3 = msg3.substring(0, rand3);
System.out.println(msg3);
}
System.out.println(“Here is message1: ” + msg1);
System.out.println(“Here is message2: ” + msg2);
System.out.println(“Here is message3: ” + msg3);
String msgAll = msg1.concat(msg2).concat(msg3);// 2.Concatenate all
// string in one
System.out.println(“Here are your three messages combined: “);
System.out.println(msgAll);
System.out.println(“Here are your messages in all uppercase: “);
System.out.println(msgAll.toUpperCase());
System.out.println(“Here are your messages in all lowercase: “);
System.out.println(msgAll.toLowerCase());
int msg1Length = msg1.length();// 3.keeps the track of lengths of all
// three messages
int msg2Length = msg2.length();
int msg3Length = msg3.length();
// 4. a) count the total no of characters in string b) vowel count
// c)space count
int charCount = 0;
int spaceCount = 0;
int vowelCount = 0;
char temp;
for (int i = 0; i < msgAll.length(); i++) {
temp = msgAll.charAt(i);
if (temp != ‘ ‘)
charCount++;// counts character in string
if (temp == ‘ ‘)
spaceCount++;// count space in string
if ((temp == ‘a’) || (temp == ‘e’) || (temp == ‘i’) || (temp == ‘o’) || (temp == ‘u’))
vowelCount++;// counts vowels in string
}
System.out.println(“The count of character in all your messages: ” + charCount);
System.out.println(“The numbers of vowels in all your messages: ” + vowelCount);
System.out.println(“The number of spaces in all your messages: ” + spaceCount);
String backwardString = “”;
for (int i = msgAll.length() – 1; i >= 0; i–) {
backwardString = backwardString + msgAll.charAt(i);// 5. backward
// the string
// msgAll
}
System.out.println(“Here is your message backwards: ” + backwardString);
System.out.println(“Here is your message vertical: “);
for (int i = 0; i < msgAll.length(); i++) {
System.out.println(msgAll.toUpperCase().charAt(i));
}
}
}
output: