// Calculate.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2004-10-09 File created. // 2005-11-19 Last modification. // As this is a program that can receive parameters from // the command line, this should be executed in a command // prompt window where the command line parameters can // be given. It may be difficult, and even impossible, to // supply the command line parameters if this program is // executed with a modern development tool such as JCreator // or Eclipse. // This program will, though, ask for data from the user // if no command line parameters are given. import java.util.* ; class Calculate { static void print_calculations ( int first_integer, int second_integer ) { System.out.print( "\n " + first_integer + " + " + second_integer + " = " + ( first_integer + second_integer ) + "\n " + first_integer + " - " + second_integer + " = " + ( first_integer - second_integer ) + "\n " + first_integer + " * " + second_integer + " = " + first_integer * second_integer ) ; if ( second_integer != 0 ) { System.out.print( "\n " + first_integer + " / " + second_integer + " = " + first_integer / second_integer + "\n " + first_integer + " % " + second_integer + " = " + first_integer % second_integer + "\n" ) ; } else { System.out.print( "\n Cannot divide with zero. \n" ) ; } } public static void main( String[] command_line_parameters ) { Scanner keyboard = new Scanner( System.in ) ; int first_operand, second_operand ; if ( command_line_parameters.length == 2 ) { first_operand = Integer.parseInt( command_line_parameters[ 0 ] ) ; second_operand = Integer.parseInt( command_line_parameters[ 1 ] ) ; print_calculations( first_operand, second_operand ) ; } else { System.out.print( "\n This program calculates with integers." + "\n Give two integers separated by a space: " ) ; first_operand = keyboard.nextInt() ; second_operand = keyboard.nextInt() ; print_calculations( first_operand, second_operand ) ; } } }