Java Input and Output
Use Eclipse to write a Java program that:
- Defines three variables of type double (say x,y, and z).
- Prints a prompt message, telling the user to input 3 numbers.
- Prints out the average of the three numbers.
- Prints out the maximum of the three numbers.
To compute the maximum, use the Math.max method which computes
the maximum of two numbers.
The output of the program needs to look like:
Enter three numbers:
8.0 9.0 10.0
The three numbers are: 8.0 9.0 10.0
The maximum is: 10.0
The average is: 9.0
In order to print both a string and numbers on the same line you can
use System.out.print in addition to using System.out.println.
System.out.print is used just like System.out.println,
the only difference being that it does not start a new line after displaying
the string it receives as a parameter.
For example, the commands
System.out.print("the number is: ");
System.out.println(10);
display the message:
the number is: 10
Submit the printout of your program
to your GTA to get credit for this lab.
For some Fame and Glory: You will notice that when you print the
average, and the average has some non-zero decimal part (eg.
(3.0+5.0+2.0)/3.0 = 10.0/3.0 = 3.3333333...), the Java print
statements will print many digits to the right of the decimal point.
To make your output appear more reasonable, you may want to limit
your prints to 2 decimal digits to the right of the decimal point
using DecimalFormat.
© 2009 CSU