/* CIS 790 JAVA PROGRAMMING (DOS) PROF. AUCIELLO TOPICS: programs vs applets for loops: start value, end value, increment System.out.println as output statement to screen Structure */ ASSIGNMENT: WRITE A PROGRAM THAT WILL GIVE THE OUTPUT BELOW: i = 0 rolling sum = 0 i = 1 rolling sum = 1 i = 2 rolling sum = 3 i = 3 rolling sum = 6 i = 4 rolling sum = 10 i = 5 rolling sum = 15 i = 6 rolling sum = 21 i = 7 rolling sum = 28 i = 8 rolling sum = 36 i = 9 rolling sum = 45 i = 10 rolling sum = 55 i = 11 rolling sum = 66 i = 12 rolling sum = 78 i = 13 rolling sum = 91 i = 14 rolling sum = 105 i = 15 rolling sum = 120 i = 16 rolling sum = 136 i = 17 rolling sum = 153 i = 18 rolling sum = 171 i = 19 rolling sum = 190 i = 20 rolling sum = 210 The sum of 1 to 20 is 210 THE INTERNAL NAME OF THIS PROGRAM IS addnum. ITS EXTERNAL NAME IS addnum.java to compile: type javac addnum.java to execute in DOS: type java addnum (it displays on screen) 1. program starts by naming the class. same name as external file name. 2. define the class as main, allow for passing String arguments. 3. bracket that includes the entire class. 4. define loop counter as an integer and set it to 0. 5. define an accumulator for the total and initialize to 0. 6. start a loop that begins with 0, exits at 101, and increments by 1 7. accumulate a total. 8. print the index and the total 9. when out of the for-loop, print the total. (watch the brackets) SELECT FROM THESE JAVA STATEMENTS: public static void main(String args[]) class addnum // names program "addnum" { for ( i=0; i<21; i++) { sum = i + sum; System.out.println("i = " + i + " rolling sum = " + sum); } { int i = 0; // defines i as int = 0 int sum = 0; } System.out.println("The sum of 1 to 20 is " + sum); }