Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC111 Quick Revision.

Similar presentations


Presentation on theme: "CSC111 Quick Revision."— Presentation transcript:

1 CSC111 Quick Revision

2 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: The user enters -999. Or the user enters 10 numbers.

3 Analysis Input processing Output Number Take numbers from user
Calculate the sum Calculate the average Output Average

4 Design 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

5 Coding

6 Start by writing the program structure
Start the program 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

7 Variable Declaration What variable do I need ?? input Processing
number Processing sum count

8 Variable Declaration what about data type input Processing number int
sum int count int

9 2. Declare variable Note:
if some variable has initial value initialize them Use appropriate variable name // import Section – import used java libraries public class Average{ // 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

10 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();

11 Print message before each read
/// 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; Scanner input; input=new Scanner (System.in); //read number System.out.println("Enter number"); number=input.nextInt(); } //end main } //end class Print message before each read

12 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)

13 4.Processing When 1 and 2 combined the while loop is written as: while ( number != -999 && count < 10)

14 input=new Scanner (System.in);
//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(); // Processing section – Processing Statements 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 } // end class

15 Sample Run Enter number 10 Enter another number 15 5 -999
The average = 10

16 Same Question in Flag-controlled While Loop
while (number != -999 && count < 10){ sum = sum +number; count ++; System.out.println("Enter another number"); number=input.nextInt(); } // end while boolean done = false; 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

17 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; 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 Added

18 In the question above we saw that: The same question could be written in different forms of while loops.


Download ppt "CSC111 Quick Revision."

Similar presentations


Ads by Google