SE1011
Class Exercises
Exercise 1: Math
Using documentation for the java.lang.Math class:
- determine how to obtain the larger of two numbers,
a
andb
. - write the code for when the numbers are
double
s - write the code for when the numbers are
long
s - what will be returned if
a
is along
andb
is anint
? - what will be returned if
a
is adouble
andb
is anint
?
Exercise 2: Primitive to String Conversion
Using documentation for the java.lang.Double class:
- Explain why the class is referred to as a wrapper class
- Determine which of the following is the correct way to convert a
double
to aString
and explain whydouble pi = Math.PI; String number = Double.toString(pi);
orDouble pi = new Double(Math.PI); String number = pi.toString();
- Determine any differences required for converting an
int
to aString
Exercise 3: Characters
Using documentation for the java.lang.Character class:
- Identify methods that would be useful for:
- Determining if a character is a digit
- Determining if a character is a letter
- Determining if a character is a vowel
- Determining if a character is capitalized
- What characters are considered whitespace?
Exercise 4: Strings
Using documentation for the java.lang.String class:
- Write a program that asks the user to enter a phrase and then displays:
- The number of whitespace characters
- The number of letters
- If any digits were present
- Write a program that asks the user to enter a phrase and then prints the phrase with each word displayed on a separate line.
- Write code that takes a string,
phrase
, and ensures that there are no capital letters in it (if there are, they should be converted to lowercase characters). - Write code that will determine if a string,
phrase
, contains "sap" in it.
Last modified: Tuesday, 16-Feb-2016 10:54:11 EST