CSC111 Quick Revision.

Slides:



Advertisements
Similar presentations
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Java Programming: From the Ground Up
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 4.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Java Review if Online Time For loop Quiz on Thursday.
Computer Programming1 Computer Science 1 Computer Programming.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
Chapter 5: Control Structures II
Loop Structures.
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Repetition.
Chapter 5: Control Structures II
TK1114 Computer Programming
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
INPUT STATEMENTS GC 201.
Control Statement Examples
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Conditional Loops.
Java Fix a program that has if Online time for Monday’s Program
Lecture Notes – Week 3 Lecture-2
SELECTION STATEMENTS (2)
Java Programming Loops
AP Java Review If else.
Control Statements Loops.
Java Fix a program that has if Online time for Monday’s Program
Method Revision.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Truth tables: Ways to organize results of Boolean expressions.
AP Java Review If else.
Java Programming Loops
Control Statements Loops.
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
Random Numbers while loop
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Computer Science Club 1st November 2019.
Presentation transcript:

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

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

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

Coding

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

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

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

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

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

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

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)

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

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

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

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

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