CPS 180 – Programming Assignment #5 Assigned: 05/31/17 Due: 06/07/17 Points: 40
ID: 3847125 • Letter: C
Question
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:
Ask the user for 3 messages to send using the following characteristics:
Generate a random number to ask the user for a # of chars for the message. The random number should be between 1 and 50. Use Math.random to generate 3 numbers: rand1, rand2, rand3
Have the user type the strings into 3 separate variables
Find the length of the string and report if the string is too long, then truncate it and reassign it to the string
String variables: Keep track of all three messages in msg1, msg2, msg3 strings. Also combine (concatenate) all 3 messages into 1 string called msgAll
int variables: Keep track of the lengths of all three messages (after truncating) in msg1Length, msg2Length, msg3Length
Counters:
Count and report the total # of characters in msgAll.
Count and report the number of vowels in msgAll (this will require a loop).
Count and report the number of spaces in msgAll (this will require a loop).
Display msgAll backwards (this will require a loop).
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:
This is a message to you!
Oops! Your message has more than 18 characters! It has been truncated to:
This is a message
Message #2: Enter a message with 40 chars or less:
…. Message ….
Message #3: Enter a message with 28 chars or less:
…. Message ….
Here is message 1: … Message …
Here is message 2: … Message …
Here is message 3: … Message …
Here are your three messages combined:
This is a message … Message … … Message …
Here are your messages in all uppercase:
THIS IS A MESSAGE … MESSAGE … … MESSAGE …
Here are your messages in all lowercase:
this is a message … message … … message …
The count of characters in all of your messages: 99
The number of vowels in all of your messages: 10
The number of spaces in all of your messages: 8
Here is your message backwards: xxxxxya aaks ss kdkl3
Here is your message vertical:
T
H
I
S
I
S
…
Explanation / Answer
import java.util.Scanner;
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));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.