/** * Arithmetic.java * * @author Jeff Ondich * * This program illustrates numerical arithmetic and a String * operation or two in Java. * * 1. Compile the class and run main. * * 2. Do +, -, *, and / do what you expect them to do? * * 3. What does % do? * * 4. What happens if you try to enter a non-integer when * asked for a number? * * 5. Change the declaration of i, j, and k from "int" to "double". * For stupid historical reasons which I will describe some time in the * remote future, the term "double" is used as the type for variables * that store real numbers. So if you want to store "2.718", you'll * need a double rather than an int. * * Also, change "getInteger" to "getDouble" both times. Now run the * program again and see what the five operations do. How does / * behave differently for ints and for doubles? */ import javabook.*; class Arithmetic { public static void main(String[] args) { // Get ready to talk to the us. MainWindow mw = new MainWindow(); mw.setVisible( true ); mw.toFront(); OutputBox out = new OutputBox( mw ); out.setVisible( true ); out.toFront(); InputBox in = new InputBox( mw ); // Mess around with some strings. String a = in.getString( "Name the moose, please:" ); String b = new String( " the moose frolicked merrily in the woods." ); String c = a + b; out.printLine( c ); // Get two integers and do some arithmetic. int i, j, k; i = in.getInteger( "Number, please:" ); j = in.getInteger( "Another number, please:" ); k = i + j; out.printLine( i + " + " + j + " = " + k ); k = i - j; out.printLine( i + " - " + j + " = " + k ); k = i * j; out.printLine( i + " * " + j + " = " + k ); k = i / j; out.printLine( i + " / " + j + " = " + k ); k = i % j; out.printLine( i + " % " + j + " = " + k ); } }