SE1011
Class Exercises

Exercise 1: Math

Using documentation for the java.lang.Math class:

  1. determine how to obtain the larger of two numbers, a and b.
  2. write the code for when the numbers are doubles
  3. write the code for when the numbers are longs
  4. what will be returned if a is a long and b is an int?
  5. what will be returned if a is a double and b is an int?

Exercise 2: Primitive to String Conversion

Using documentation for the java.lang.Double class:

  1. Explain why the class is referred to as a wrapper class
  2. Determine which of the following is the correct way to convert a double to a String and explain why
    double pi = Math.PI;
    String number = Double.toString(pi);
    
    or
    Double pi = new Double(Math.PI);
    String number = pi.toString();
    
  3. Determine any differences required for converting an int to a String

Exercise 3: Characters

Using documentation for the java.lang.Character class:

  1. 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
  2. What characters are considered whitespace?

Exercise 4: Strings

Using documentation for the java.lang.String class:

  1. 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
  2. Write a program that asks the user to enter a phrase and then prints the phrase with each word displayed on a separate line.
  3. 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).
  4. Write code that will determine if a string, phrase, contains "sap" in it.

Last modified: Tuesday, 16-Feb-2016 09:54:11 CST