Skip to content

Instantly share code, notes, and snippets.

@Rdilorenzo73
Last active November 4, 2018 19:15
Show Gist options
  • Save Rdilorenzo73/ed2a70884352138ac08352c23992c079 to your computer and use it in GitHub Desktop.
Save Rdilorenzo73/ed2a70884352138ac08352c23992c079 to your computer and use it in GitHub Desktop.
Java String handling

A Java exercise taken from Exercism.io

I did not create this assignment. The coded solution is mine. This was a Java practice project which helped me with String manipulation methods.


Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Whoa, chill out!' if you yell at him.

He answers 'Calm down, I know what I'm doing!' if you yell a question at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

bob.hey("         hmmmmmmm...") -> "Whatever."
bob.hey("Tom-ay-to, tom-aaaah-to.") -> "Whatever."
bob.hey("\t\t\t\t\t\t\t\t\t\t") -> "Fine. Be that way!"
bob.hey("") -> "Fine. Be that way!"
bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!") -> "Whoa, chill out!"
bob.hey("FCECDFCAAB") -> "Whoa, chill out!"
bob.hey("fffbbcbeab?") -> "Sure."
bob.hey("You are, what, like 15?") -> "Sure."
bob.hey("WHAT WERE YOU THINKING?") -> "Calm down, I know what I'm doing!"
class Bob {
public String hey(String address) {
String response = null;
if(this.hasLetters(address) && !this.hasLowerCase(address) && this.isQuestion(address))
{
response = "Calm down, I know what I'm doing!";
}
//shouting = has letters which are all upper case
else if(this.hasLetters(address) && !this.hasLowerCase(address))
{
response = "Whoa, chill out!";
}
//question - ends with the question mark
else if(this.isQuestion(address))
{
response = "Sure.";
}
//nothing but whitespace characters
else if(this.isSilence(address))
{
response = "Fine. Be that way!";
}
//all other possibilities
else
{
response = "Whatever.";
}
return response;
}
private boolean isSilence(String address)
{
boolean isSilence = true;
if (address.length() > 0)
{
for(int i = 0;i<address.length();i++)
{
//get the character
char a = address.charAt(i);
//if not a space break the loop
if(!Character.isWhitespace(a))
{
isSilence = false;
break;
}
}
}
return isSilence;
}
private boolean isQuestion(String address)
{
boolean isQuestion = false;
if(address.length() > 0)
{
int theLength = address.length();
//get the last character in the address
String theEnd = address.substring(theLength-1);
if(theEnd.equals("?"))
{
isQuestion = true;
}
}
return isQuestion;
}
private boolean hasLetters(String address)
{
boolean hasLetters = false;
for(int i = 0;i<address.length();i++)
{
//get the character
char a = address.charAt(i);
//if it has letters break the loop
if(Character.isLetter(a))
{
hasLetters = true;
break;
}
}
return hasLetters;
}
private boolean hasLowerCase(String address)
{
boolean hasLower = false;
for(int i = 0;i<address.length();i++)
{
//get the character
char a = address.charAt(i);
//if it has lower case letters break the loop
if(Character.isLowerCase(a))
{
hasLower = true;
break;
}
}
return hasLower;
}
public static void main(String[] args) {
Bob bob = new Bob();
System.out.println(bob.hey(" hmmmmmmm..."));
System.out.println(bob.hey("hi"));
System.out.println(bob.hey(""));
System.out.println(bob.hey("\t\t\t\t\t\t\t\t\t\t"));
System.out.println(bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"));
System.out.println(bob.hey("WHAT WERE YOU THINKING?"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment