Here's a simple program to average three ints, written using three simple input styles: JOptionPane, Scanner, and SavitchIn.
Documentation: Savitch, , Appendix 10, p 918. Some of the sample programs in the third edition of the textbook are rewritten in the 4th edition using JOptionPane. See Scanner below for how to download.
Documentation: Savitch has switched to using both JOptionPane and Scanner in the 4th edition of his textbook. You can download pdf files of the first three chapters of the new edition for free at . Follow the link to the the fourth edition ("4e"), then click on "Sample Chapters". Chapter two explains both Scanner and JOptionPane.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
// File: average3/Average3JOptionPane.java // Description: Average three ints. Use JOptionPane. // Author: Fred Swartz // Date: 2002-02-13 import javax.swing.JOptionPane; public class Average3JOptionPane { public static void main(String[] args) { //... Declare local variables; int a, b, c; // No meaningful names are possible. int average; String temp; // Temporary storage for JOptionPane input. //... Read three numbers from dialog boxes. temp = JOptionPane.showInputDialog(null, "First number"); a = Integer.parseInt(temp); // Convert String to int. temp = JOptionPane.showInputDialog(null, "First number"); b = Integer.parseInt(temp); temp = JOptionPane.showInputDialog(null, "First number"); c = Integer.parseInt(temp); //... Compute their average. average = (a + b + c) / 3; //... Display their average in a dialog box. JOptionPane.showMessageDialog(null, "Average is " + average); System.exit(0); // Some systems may require this to stop. } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// File: average3/Average3Scanner.java // Description: Average three ints. Use Scanner. // Author: Fred Swartz // Date: 2002-02-13 // Note: Scanner was added to Java 5 (JDK 1.5). import java.util.Scanner; public class Average3Scanner { public static void main(String[] args) { //... Declare local variables; int a, b, c; // No meaningful names are possible. int average; //... Initialize Scanner to read from console. Scanner input = new Scanner(System.in); //... Read three numbers from the console. System.out.print("Enter first number : "); a = input.nextInt(); System.out.print("Enter second number: "); b = input.nextInt(); System.out.print("Enter last number : "); c = input.nextInt(); //... Compute their average. average = (a + b + c) / 3; //... Display their average on the console. System.out.println("Average is " + average); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// File: average3/Average3SavitchIn.java // Description: Average three ints. Use SavitchIn. // Author: Fred Swartz // Date: 2002-02-13 // Note: SavitchIn is NOT part of standard Java. // To use it you must get it from the CD that // comes with your book and copy SavitchIn.java // into the same directory as this program. // Compile it (once) before compiling this program. public class Average3SavitchIn { public static void main(String[] args) { //... Declare local variables; int a, b, c; // No meaningful names are possible. int average; //... Read three numbers from the console. System.out.print("Enter first number : "); a = SavitchIn.readLineInt(); System.out.print("Enter second number: "); b = SavitchIn.readLineInt(); System.out.print("Enter last number : "); c = SavitchIn.readLineInt(); //... Compute their average. average = (a + b + c) / 3; //... Display their average on the console. System.out.println("Average is " + average); } } |