CPS 180 – Programming Assignment #5 Assigned: 05/31/17 Due: 06/07/17 Points: 40
ID: 3847121 • 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
Chipper Class
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Chipper {
private String chat;
private String[] mgs; //Creating an array of String type to store all the msgs
Chipper(){
chat=null;
mgs=new String[3];
input();
}
public void input(){
Scanner scan=new Scanner(System.in); //To take in the input
Random ran=new Random(); //Using Random for creating a Random input size
for(int i=0;i<3;i++){
int length=ran.nextInt(51)+1; //Definining the max size of Randomness
System.out.println("Message #"+(i+1)+": Enter a message with "+length+" chars or less:");
chat=scan.nextLine(); //taking the entire line as string input.
if(chat.length()>length){ //Checking the size of input
chat=chat.substring(0, length);
System.out.println("Oops! Your message has more than "+length+" characters! It has been truncated to:");
System.out.println(chat);
mgs[i]=chat;
}
else{
System.out.println(chat);
mgs[i]=chat;
}
}
}
public int countSpaces(){ //Counting the spaces in all of the 3 messages.
int count=0;
for(String str:mgs){
for(int i=0;i<str.length();i++){
if(Character.isWhitespace(str.charAt(i))){
count++;
}
}
}
return count;
}
public int countVowels(){ //Counting vowels in all of the messages
int count=0;
String regex="[aeiou]";//Defining the regular expression with one having [aeiou] characters
Pattern pat=Pattern.compile(regex); //using the pattern class to use the above pattern
for(String str:mgs){
Matcher mat=pat.matcher(str);
while(mat.find()){ //Counting the number of time vowels was encountered in the msg
count++;
}
}
return count;
}
public int countCharacter(){ //Counting Character in all of the msgs
int count=0;
for(String str:mgs){
for(int i=0;i<str.length();i++){
count++;
}
/*This calls in the spaces in the end to remove space count from the final
* character count */
}
return (count-this.countSpaces());
}
public void diplay(){
for(String str:mgs){
System.out.println(str);
}
}
public void toLowerCase(){
for(String str:mgs){
for(int i=0;i<str.length();i++){
System.out.print(Character.toLowerCase(str.charAt(i)));
}
System.out.println();
}
}
public void count(){
System.out.println("The count of characters in all of your messages: "+this.countCharacter());
System.out.println("The number of vowels in all of your messages: "+this.countVowels());
System.out.println("The number of spaces in all of your messages: "+this.countSpaces());
}
public void displayBackwards(){
for(String str:mgs){
for(int i=str.length()-1;i>=0;i--){
System.out.print(str.charAt(i));
}
System.out.println();
}
}
public String combinedMsg(){
StringBuilder strx=new StringBuilder();
for(String str:mgs){
strx.append(str);
strx.append(" ");
}
return strx.toString();
}
public void verticalMsg(){
String stx=this.combinedMsg();
for(int i=0;i<stx.length();i++){
System.out.println(stx.charAt(i));
}
}
}
Main Class
public class ChipperMain {
public static void main(String[] args){
Chipper cp=new Chipper();
System.out.println("-------------Result--------------");
cp.toLowerCase();
cp.count();
cp.displayBackwards();
System.out.println("Combined Msgs:"+cp.combinedMsg());
cp.verticalMsg();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.