Labs

For these labs do not use any existing java.lang.Math static methods.

  1. In mathematics, the floor function returns the largest integer less than or equal to a given number. For example:

floor(25.95) yields 25

floor(-25.95) yields -26

Create a program that prompts the user for a floating point number and reads it into a variable of type double. Use the cast operator to convert the number to the largest integer less than or equal to the number. Print out the results. (Hint: negative numbers will require more than just casting. Be sure to check your solution for decimals and whole numbers.)

(Solution: Floor.java)

  1. In mathematics, the ceiling function returns the smallest integer greater than or equal to a number. For example:

ceiling(25.95) yields 26

ceiling(-25.95) yields -25

Write a program similar to (1) to print out the ceiling for any floating point number given by the user.

(Solution: Ceiling.java)

  1. Write a program that reads a floating point number from the user and prints out the number rounded to the nearest int.

(Solution: Round.java)


Prev -- Up