Prev | Current Page 498 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"

Here is how you use the Math class constant:
double pi = Math.PI;
How much does your result change?
public class CircleArea {
//A Java program to compute the area of a circle
// whose radius is 5
public static void main( String[] args ) {
double r = 5.;
System.out.print ( "Using pi = 3.14: " );
System.out.println( "Area when r = 5: " + 3.14*r*r );
System.out.print ( "Using Math.PI: " );
System.out.println( "Area when r = 5: " + Math.PI*r*r );
}
}
Using pi = 3.14: Area when r = 5: 78.5
Using Math.PI: Area when r = 5: 78.53981633974483
5.3 Write a Java program that prompts the user for a number, and then tells the user whether the number is an
even multiple of 5. Use Scanner to read the number from the user, and use the modulo operator (%)
to decide whether the number is a multiple of 5.
import java.util.Scanner;
public class Mod5 {
//A Java program to detect a multiple of 5
public static void main( String[] args ) {
Scanner sc = new Scanner(System.in);
System.out.print( "Enter an integer: " );
int number = sc.nextInt();
ANSWERS TO REVIEW QUESTIONS 191
if( number % 5 == 0 ) {
System.out.println( number + " is a multiple of 5." );
}
else {
System.out.println( number + " is not a multiple of 5.


Pages:
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
Anteny satelitarne apartamenty przy plaży online loan for bad credit rekuperatory skarpetki narciarskie męskie