Solution to question 0 import java.util.Scanner; /* YOUR NAME Loop Hwk #1 Comp 110 * Assigned: Mon 2/21 * Due: Fri 2/5 at start of lab * You may work together on these problems, but each student * must turn in their solution on Sakai. * This homework is just practice writing loops in Java. * You do not need an Implementation Plan or a Test Plan. * Just complete the Java code for each of the methods. */ public class LoopFeb18SOLUTION { public static void main( String[] args) { // Change the name of this method (e.g. question1) to // run your code for a different question on this assignment. question0v2(); } /* * Ask the user to "Enter a positive integer" * Print out the sum of the first n odd integers * where n is the number entered by the user. * For example, if the user enters 5, print out * 25 = 1 + 3 + 5 + 7 + 9 */ public static void question0() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a positive integer"); int n = keyboard.nextInt(); // Put your code here } /* * Step 1 - understand the problem * Step 2 - Make it concrete. Pick a typical input. * Draw your data (not applicable on question0). * * We use as the typical input the example given in * the problem. * n = 5 * 1 + 3 + 5 + 7 + 9 = 25 * * Step 3 - write a loop that executes the correct number * of times (but does nothing useful). */ public static void question0v0() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a positive integer"); int n = keyboard.nextInt(); int count = 0; while( count <= n ) { System.out.println("count = " + count); count++; } System.out.println("count = " + count); } /* Whoops - an off by one error. This is a very common * mistake to make. We fix the loop and sum up n * values. Not yet sure how to get only the odd numbers. * First, we will just sum up ones. */ public static void question0v1() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a positive integer"); int n = keyboard.nextInt(); // To fix the loop, either initialize to 1 (not 0) // or change the condition to less than (not <=). // Its doesn't matter which you do. int count = 0; int sum = 0; // new code while( count < n ) { System.out.println("count = " + count); sum = sum + 1; // new code count++; } System.out.println("count = " + count); System.out.println("sum = " + sum); } /* * Last step - compute the odd integers. It helps to line up * the value of count with the integer we want to add. * * 0 1 2 3 4 * 1 3 5 7 9 * * What is the pattern? How do we take the number in the first * row and compute the corresponding number in the second row? */ public static void question0v2() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a positive integer"); int n = keyboard.nextInt(); // To fix the loop, either initialize to 1 (not 0) // or change the condition to less than (not <=). // Its doesn't matter which you do. int count = 0; int sum = 0; while( count < n ) { System.out.println("count = " + count); sum = sum + 2*count + 1; // new code count++; } System.out.println("count = " + count); System.out.println("sum = " + sum); } }