I have done 1-6 on the same button, please help me through 7-15 i would really a
ID: 674061 • Letter: I
Question
I have done 1-6 on the same button, please help me through 7-15 i would really appriciate it.
Thank You.
You will create a bunch of functions to practice making them. Your page should have ONE button that runs a main that I will provide to you. Try to write and test one function at a time before continuing on. Most syntax errors I see crop up when everyone tries to put in all of their code at once. Build small, test often, don't add code until you know what you have is good. Don't forget about the debugger! 1. Write a function phone() that alerts “ring ring” when called. 2. Write a function helloWorld() that alerts “Hello World” when called. 3. Write a function even(num) that returns true if a number is even. It should return false if the number is odd. 4. Write a function rollDie(sides) that returns a random number between 1 and the value of sides. 5. Write a function bigger(num1, num2) that returns the bigger number of the two parameters. 6. Write a function smaller(num1, num2) that returns the smaller number of the two parameters. 7. Write a function vowelCounter(string) that takes a String parameter and returns the number of vowels in a String. Vowels are a, e, i, o, u. Hint: the .toLowerCase() could make your life easier here. 8. Write a function consonantCounter(string) that takes a String parameter and returns the number of non-vowel letters in a String. Hint: Can you use your vowelCounter function to help you with this? 9. Write a function upperCounter(string) that takes a String parameter and returns the number of uppercase characters in a String. Hint: Can the ASCII table and .charCodeAt() help you here? 10. Write a function lowerCounter(string) that takes a String parameter and returns the number of lowercase characters in a String. Hint: See the previous function's hint. 11. Write a function isAlphabetic(string) that takes a String parameter and returns true if there are only letters (no numbers, no punctuation and no spaces) in the string. Return false if there are non letter characters in the string. Hint: .toLowerCase() could help you here. Problem 7 and 8 or 9 and 10 could help you too if you wanted. 12. Write a function isInteger(string) that takes a String parameter and returns true if there are only numbers in the string. Return false if it has any letters. 13. Write a function reverseString(string) that takes a String as a parameter and returns a reversed version of a String. Example: “Go” should become “oG” Hint: A loop that counts backward and concatenation would be helpful here. 14. Write a function isPalindrome(string) that takes a String as a parameter and returns true if the String is a palindrome. A palindrome is a word that looks the same written both forward and backwards. Example: “radar” is “radar” if you spell it backward. Hint: the reverseString could help you here. 15. Write a function phoneWords(string) that takes a string containing a phone number that could contain letters like “1-800-Flowers” and returns what you would actually dial on the phone. “1-800- Flowers” would result in 18003569377.
Explanation / Answer
I just wrote the functions, as you already have the code for buttons and the first 6 functions.
You just append these functions to your existing logic. That will be enough. Remove the class. I just added that to perform some error checking.
class SimpleFunctions
{
/*7. Write a function vowelCounter(string) that takes a String parameter and returns the number of vowels in a String. */
int vowelCount(String str)
{ char[] letters = str.toCharArray(); //Read the string into character array.
int count = 0; //Initialize count to 0.
for (char c : letters) //For each character
{
switch (Character.toLowerCase(c)) //If it is an vowel, increment the count.
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
default:
}
}
return count;
}
/*8. Write a function consonantCounter(string) that takes a String parameter and returns the number of non-vowel letters in a String.*/
int consonantCount(String str)
{
char[] letters = str.toCharArray();
int count = 0;
for (char c : letters)
{
switch (Character.toLowerCase(c)) //If it is a consonant, increment the count.
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
break;
default:count++;
}
}
return count;
}
/* 9. Write a function upperCounter(string) that takes a String parameter and returns the number of uppercase characters in a String. */
int upperCounter(String str)
{
int length=str.length(); //Initialize the length of the str to length.
int upperCount=0; //Initialize upperCount to 0.
for(int i=0; i<length;i++) //For each character.
{
if(Character.isUpperCase(str.charAt(i))) //If it is in Upper case.
upperCount++; //Increment the counter.
}
return upperCount;
}
/*10. Write a function lowerCounter(string) that takes a String parameter and returns the number of lowercase characters in a String.*/
int lowerCounter(String str)
{
int length=str.length();
int lowerCount=0;
for(int i=0; i<length;i++)
{
if(Character.isLowerCase(str.charAt(i))) //If it is in Lower case.
lowerCount++;
}
return lowerCount;
}
/*11. Write a function isAlphabetic(string) that takes a String parameter and returns true if there are only letters (no numbers, no punctuation and no spaces) in the string. Return false if there are non letter characters in the string.*/
boolean isAlphabetic(String str)
{
char[] chars =str.toCharArray();
for (char c : chars) //For each letter.
{
if(!(Character.isLetter(c))) //If it is not a letter.
{
return false; //return false.
}
}
return true; //If all characters are letters, return true.
}
/*12. Write a function isInteger(string) that takes a String parameter and returns true if there are only numbers in the string. Return false if it has any letters.*/
boolean isInteger(String str)
{
char[] chars=str.toCharArray();
for(char c : chars) //For each character in the string.
{
if(!(Character.isDigit(c))) //If it is not a digit.
return false; //Return false.
}
return true; //If all the characters are digits, return true.
}
/*13. Write a function reverseString(string) that takes a String as a parameter and returns a reversed version of a String. */
String reverseString(String string)
{
String revString = "";
for(int i = 0; i < string.length(); i++)
revString += string.charAt(i);
revString += '';
return revString;
}
/*14. Write a function isPalindrome(string) that takes a String as a parameter and returns true if the String is a palindrome.*/
boolean isPalindrome(String string)
{
String revString = reverseString(string);
if(string == revString)
return true;
return false;
}
/*Write a function phoneWords(string) that takes a string containing a phone number that could contain letters like “1-800-Flowers” and returns what you would actually dial on the phone.*/
String phoneWords(String string)
{
char myChar;
int myASCII;
String final_Number = ""; //Initializing the final_Number to an empty string.
String number = "0123456789abcdefghijklmnopqrstuvwxyz"; //Assign the small letters and digits string to number.
for(int i = 0; i < 13; i++) //For each of the 12 characters in an input string.
{
myChar = string.charAt(i); //Read the character from string into myChar.
myASCII = (int)myChar; //Convert that character into its equivalent ASCII value.
if((myASCII >= 65 && myASCII < 91)) //If that ASCII value is between 65 and 90 (if its a capital letter).
myASCII += 32; //Add 32 to that variable myASCII (make it a small letter).
switch(myASCII) //If an alphabet is encountered, convert it into equivalent digit of the keypad.
{
case 97:
case 98:
case 99:
myChar = '2';
break;
case 100:
case 101:
case 102:
myChar = '3';
break;
case 103:
case 104:
case 105:
myChar = '4';
break;
case 106:
case 107:
case 108:
myChar = '5';
break;
case 109:
case 110:
case 111:
myChar = '6';
break;
case 112:
case 113:
case 114:
case 115:
myChar = '7';
break;
case 116:
case 117:
case 118:
myChar = '8';
break;
case 119:
case 120:
case 121:
case 122:
myChar = '9';
break;
}
if(number.indexOf(myChar) != -1) //If the character read is valid.
final_Number += myChar; //Append the character to the final String.
}
return final_Number;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.