Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, I would create a class <code>InputData</code> that would store the inputs from the user:</p> <pre><code>class InputData { public int number1; public int number2; public int number3; } </code></pre> <p>This in itself is a useful general technique: collect several related values into a single data structure. You can then either rewrite your <code>main</code> method to use this class instead of three separate <code>int</code> variables, or you can go one step further and add some behavior to the <code>InputData</code> class. The obvious first behavior to add would be to compute the average:</p> <pre><code>class InputData { public int number1; public int number2; public int number3; public int average() { return (number1 + number2 + number3) / 3; } } </code></pre> <p>With this, you could rewrite your <code>main</code> as follows:</p> <pre><code>public class Average3 { static class InputData { public int number1; public int number2; public int number3; public int average() { return (number1 + number2 + number3) / 3; } } /** * @param args */ public static void main(String[] args) { InputData input = new InputData(); System.out.println("Enter the first integer."); Scanner keyboard = new Scanner(System.in); input.number1 = keyboard.nextInt(); System.out.println("Enter the second integer."); input.number2 = keyboard.nextInt(); System.out.println("Enter the third integer."); input.number3 = keyboard.nextInt(); System.out.println("The average of NUMBER1, NUMBER2, and NUMBER3 = " + input.average()); } } </code></pre> <p>Notice that I've made <code>InputData</code> a <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html" rel="nofollow"><em>static inner class</em></a> of <code>Average3</code>. It could also have been a separate, top-level class in the same file (as long as it wasn't <code>public</code>) or a class in a separate file.</p> <p>An improvement on this would be to use an array instead of separate <code>int</code> fields in the <code>InputData</code> class. Your program might then look like this:</p> <pre><code>public class Average3 { static class InputData { public int[] numbers; InputData(int size) { numbers = new int[size]; } public int average() { int sum = 0; for (int number : numbers) { sum += number; } return sum / numbers.length; } } /** * @param args */ public static void main(String[] args) { String[] prompts = { "first", "second", "third" }; InputData input = new InputData(3); Scanner keyboard = new Scanner(System.in); for (int i = 0; i &lt; prompts.length; ++i) { System.out.println("Enter the " + prompts[i] + " integer."); input.numbers[i] = keyboard.nextInt(); } System.out.println("The average of NUMBER1, NUMBER2, and NUMBER3 = " + input.average()); } } </code></pre> <p>Here I've added a constructor to <code>InputData</code> to initialize the array and given it an argument to set the size of the array.</p> <p>You could then introduce additional improvements like making the number of input values dynamic (using an <code>ArrayList&lt;Integer&gt;</code> instead of an <code>int</code> array). But this goes beyond the specific requirements of the assignment. Some people (many, actually) tend to do this kind of generalization automatically. However, advocates of <a href="http://en.wikipedia.org/wiki/Extreme_programming" rel="nofollow">Extreme Programming</a> will point out that it is generally better to keep things simple: design and code for the needs of today instead of those of tomorrow, next week, or next month.</p> <p>In other words, you don't know what the next assignment will bring, so however you generalize beyond the immediate assignment might be wasted work.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload