Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSC111 Quick Revision Problem We want to develop a Java program to calculate the average of numbers entered by the user. The average is calculated as follows Average= sum \ count of numbers Your program should ask the user to enter numbers and stops if: 1) The user enters -999. 2) Or the user enters 10 numbers. Analysis Input Number processing Take numbers from user Calculate the sum Calculate the average Output Average Design 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. Start the program Read number from user and save it in number. Declare variable count and initialize it to zero. Declare variable sum and initialize it to zero. While number is not equal to -999 and count is less than 30 numbers Calculate sum using -> (sum + number) Increment count by 1. Now repeat Calculate Average using -> (sum \ count). print the average. End the Program Coding Start the program 1. Start by writing the program structure /* We want to develop a Java program to calculate the average of numbers entered by the user */ //Aseel Alkhelaiwi // import Section – import used java libraries public class Average{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables // Input section – Enter required data // Processing section – Processing Statements // Output section – Display expected results } // end main } // end class Variable Declaration What variable do I need ?? input number Processing sum count Variable Declaration what about data type input number int Processing sum count int int 2. Declare variable Note: • if some variable has initial value // import Section – import usedinitialize javathem libraries public class Average{ • Use appropriate variable name // main method public static void main( String args[] ){ // Declaration section – Declare needed variables int number; int sum=0 ; int count =0; // Input section – Enter required data // Processing section – Processing Statements // Output section – Display expected results } // end main } // end class 3.Input Reading input from the user • 4 basic steps Step 1: import the Scanner class: import java.util.Scanner; Step 2 : declaring a reference variable of a Scanner Scanner input ; Step 3: creating an instance of the Scanner input = new Scanner (System.in); Step 4: use specific methods to enter data int number = input.nextInt(); /// import Section – import used java libraries import java.util.Scanner; public class Average{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables // to store numbers int number; // to store the sum int sum=0 ; //to store the count of numbers int count =0; input=new Scanner (System.in); //read number System.out.println("Enter number"); number=input.nextInt(); } //end main } //end class Scanner input; • Print message before each read 4.Processing There are two conditions that need to be met to enter the while loop: 1)the number entered not equal to -999 Written as : while (number != -999) 2) the numbers entered are less than or equal to 10. HOW!! by counting the numbers entered by the user. Once the amount exceeded 10 we will exit the loop Written as : while ( count <10) 4.Processing When 1 and 2 combined the while loop is written as: while ( number != -999 && count < 10) //import Section – import used java libraries import java.util.Scanner; public class Average{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables int number; // to store numbers int sum=0 ; // to store the sum int count =0; // to store the count of numbers Scanner input; input=new Scanner (System.in); //read number System.out.println("Enter number"); number=input.nextInt(); while (number != -999 && count < 10){ sum = sum +number; count ++; System.out.println("Enter another number"); number=input.nextInt(); } // end while // Output section – Display expected results if (count != 0) System.out.printf("The average = %d%n",(sum / counter)); else System.out.println("No input."); } // end main // Processing section – Processing Statements } // end class Sample Run Enter number 10 Enter another number 15 Enter another number 5 Enter another number -999 The average = 10 Same Question in Flag-controlled While Loop boolean done = false; while (number != -999 && count < 10){ while ( !done){ sum = sum +number; system.out.println("Enter number"); count ++; number=input.nextInt(); System.out.println("Enter another number"); if (number == -999 || count >=10) number=input.nextInt(); } // end while done = true; else { sum = sum + number; count++; } // end else } // end while Same Question but Flag-controlled While Loop //import Section – import used java libraries import java.util.Scanner; public class Average{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables int number; // to store numbers int sum=0 ; // to store the sum int count =0; // to store the count of numbers boolean done= false; Added Scanner input; input=new Scanner (System.in); //read number // Processing section – Processing Statements while ( !done){ system.out.println("Enter number"); number=input.nextInt(); if (number == -999 || count >=10) done = true; else { sum = sum + number; count++; } // end else }// end while // Output section – Display expected results if (count != 0) System.out.printf("The average = %d%n",(sum / counter)); else System.out.println("No input."); } // end main } // end class In the question above we saw that: The same question could be written in different forms of while loops.