JAVATM- The Beginnings

User Interaction

Suppose you wrote a program to compute the square of a number.

Listing 1 - square.java (version 1)

public class square

{

   public static void main(String[] args)

   {

      int number = 5;

      System.out.println(number * number);         // Prints 25

   }

}

This program only computes the square of the number five. If we wanted to compute the square of the number 7, we would have to edit, recompile, and rerun the program.

Listing 2 - square.java (version 2)

public class square

{

   public static void main(String[] args)

   {

      int number = 7;

      System.out.println(number * number);         // Prints 49

   }

}

If we could make it the responsibility of the user of the program to select a number to square, then read the number, we wouldn’t have to edit and recompile the program.

User interaction refers to the process of reading information from the user of a program in the form of values provided for calculations or answers to questions which impact the logic implemented inside the program.

Section 1 - System.in

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.



Back to ralphlecessi.com