Assignment 26, 27, 28, & 29 - Math and String Classes
Assignment 26, 27, 28, & 29 - Math and String Classes
Use the Java API and other online resources to investigate the Java Math class.
2. Name the methods that find the absolute value? How many of such methods are there?
5. How do you call these methods in Math class? (Go online to look for examples)
6. RandomNum.java Write a program that will print out a random number between 0 and 100.
7. Exponent.java Write a program that asks the user for a base b, and an exponent e, then prints out
the power be.
8. AbsValue.java Write a program that asks the user for a number, then outputs the absolute value of
that number.
1. Taxi.java: Write a program that calculates the number of taxis that is required for the given number of
passengers. Assume each taxi can take 4 people.
2. Bigger.java: Write a program that asks the user for two numbers, and then prints out the bigger of the
two numbers.
3. Addition.java: Write a program that displays an addition question with two random numbers. It will
continuously ask the user for the answer until it is correct.
Assignment 28 – Strings
1. StringLen.java: print out the length of a string entered by the user. (method: length)
2. FirstLast.java: print out the 1st and last character of a string entered by the user. (method charAt)
3. Alphabetical.java: Ask the user for two strings and tell them whether the first is alphabetically first,
last or equal. (method: compareTo)
4. Half.java: Ask the user for a string and break it in half printing the first half on one line and the last half
on another. (method: substring)
2. HiddenPassword.java: Write a program that accepts a string of characters and prints out a string with
the same number of characters with all characters except the space, replaced by the given character.
Confused? See the example.
Example:
Input: Enter a string: Happy Days
Enter a character: !
Output: Hidden Password: !!!!! !!!!
3. AddUnicodes.java: Write a program which prompts the user for a string and then prints out the sum of
the Unicodes of each of the characters.
Example:
Input: Enter a string: Deuce
Output: Sum of the Unicodes = 486
4. MakeUppercase.java: Code a program which changes a string to its upper case equivalent. Note: only
lower case letters should be changed.
Example:
Input: Enter string: Abracadabra!
5. FormatName.java: Code a program, which accepts a first and last name and creates a new string in
the format lastname, FirstInitial. Then print out the new string. (make sure you concatenate the strings
first... and same them in a new string) (see example below).
Example:
Input: Enter firstname: Bart
Enter lastname: Simpson
Output: The new name is: Simpson, B.
6. Encrypt.java: Code a program which encrypts a line of code. The encryption specifications are as
follows:
● the first and last characters of each string are exchanged.
● Middle characters of each string are shifted to the character two after it in the ASCII table (works for
non-letters as well.)
● spaces are left alone
● careful with strings less than 3 characters
Example:
Input: Enter a line to be encrypted: Happy Days!
Output: The encryption is: ycrrH !c{uD
7. ChangeHalf.java: Write a program that interchanges the first and the second half of the string entered
by the user. If the string has even length, the middle character does not move.
Example:
Input: Implementation
Output: ntationImpleme
Input: character
Output: cterachar
Assignment 30: Strings and Integers (Optional Bonus)
1. AddDigits.java: Write a program that outputs the sum of each digit in a number entered by the user.
Example:
Input: 95684
Output: 32
(9+5+6+8+4)
NOTE: You should read your number in as a String and use the substring() methods to isolate your digits,
and then the parseInt() method to convert your digits into integer values.
2. AddPairs.java: Write a program that divides a number entered by the users into a set of two digit
numbers (if the number has an odd number of digits, the last number is only one digit), then output the
sum of the set of numbers.
Example:
Input: 239403854
Output: 209
(23+94+3+85+4)
NOTE: Similar to AddDigits.java, you should read your number in as a String and use the substring()
methods to isolate your digits, and then the parseInt() method to convert your digits into integer values.
3. Divide17.java: Write a program that “builds” a new number by joining two numbers entered by the
user, then output the result of the new number divided by 17 (in two decimals places)
Again, you should solve this problem by treating your numbers as Strings, using the + operator to combine
them, and then using the parseInt() method to convert your String into an integer value.