go to previous page   go to home page   go to next page hear noise

Answer:

No — bugs are not allowed in restaurants, nor in programs.


Java as a Calculator

All of the familiar mathematical functions found on an electronic calculator such as sine, log, and square root are available in the Java Math class. Typically these functions expect data type double as a parameter and return a double value.

Here is a program that reads a floating point number from the keyboard and prints out its square root:

import java.util.Scanner;

class SquareRoot
{
  public static void main (String[] args)
  {
    Scanner scan = new Scanner( System.in );
    double value;

    // read in a double
    System.out.print  ("Enter a double:");
    value = scan.nextDouble();
    
    // calculate its square root
    double result = Math.sqrt( value );
    
    // write out the result
    System.out.println("square root: " + result );
  }
}

The assignment statement (in blue) uses the sqrt() method of the class Math. This is a static method, which means that you ask for it using the name of a class and a dot operator, like this:

Class . method ( parameters )

This looks the same as calling a method of an object, but here a class name is used, not an object name.


QUESTION 12:

The class Math also has a log method that returns the natural logarithm of its argument. Mentally modify the program so that it returns the log of the input value.