1 / 26

Introduction to Programming

Introduction to Programming. Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2014 Week 7: loops. Overview. Java Lab 6, Exercises 2 and 3 while statement for statement See Java for Everyone, Ch. 4. JavaLab 6, Qu. 2.

akira
Download Presentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2014 Week 7: loops Birkbeck College, U. London

  2. Overview • Java Lab 6, Exercises 2 and 3 • while statement • for statement • See Java for Everyone, Ch. 4 Birkbeck College, U. London

  3. JavaLab 6, Qu. 2 Obtain a score of type int from the keyboard and print out the corresponding letter grade. 21 February 2014 Birkbeck College, U. London 3

  4. Solution to Qu. 2: first part import java.util.Scanner; public class QuizGrading { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please type in the score: "); int score = in.nextInt(); … // more code here System.out.print("The grade is: "+grade); } } 21 February 2014 Birkbeck College, U. London 4

  5. Solution to Qu. 2: second part char grade = ''; if (score >= 90) {grade = 'A';} else if (score >= 80) {grade = 'B';} else if (score >= 70) {grade = 'C';} else if (score >= 60) {grade = 'D';} else {grade = 'E';} 21 February 2014 Birkbeck College, U. London 5

  6. Diagram for Question 2 E >= D >= C >= B >=A 50 70 80 90 100 60 21 February 2014 Birkbeck College, U. London 6

  7. JavaLab 6, Qu. 3 Obtain a year of type int from the keyboard. Print true if it is a leap year, otherwise print false. Usually years that are divisible by 4 are leap years. However, years that are divisible by 100 are not leap years, unless the year is also divisible by 400. 21 February 2014 Birkbeck College, U. London 7

  8. Decision Tree no yes divisible by 4? not a leap year divisible by 100 ? yes no leap year divisible by 400 ? yes no leap year not a leap year 21 February 2014 Birkbeck College, U. London 8

  9. Two Paths through the Decision Tree a: (year%4 == 0); b: (year%100 !=0); c: (year%100 == 0) && (year%400 == 0); First path: a && b; Second path: a && c; 21 February 2014 Birkbeck College, U. London 9

  10. Boolean Test for a Leap Year Solution: (a && b)||(a && c) Equivalent solution: a && (b||c) Proof of equivalence: check a = false (both are false) check a = true (both reduce to b||c) 21 February 2014 Birkbeck College, U. London 10

  11. Solution to Qu. 3: first part import java.util.Scanner; public class LeapYear { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please type in the year: "); int year = in.nextInt(); … // more code here } } 21 February 2014 Birkbeck College, U. London 11

  12. Solution to Qu. 3: second part boolean a = year%4 == 0; boolean b = year%100 != 0; boolean c = (year%100==0) && (year%400==0); if (a && (b||c)) { System.out.println("Leap year"); } else { System.out.println("Not a leap year"); } 21 February 2014 Birkbeck College, U. London 12

  13. Syntax for the while Loop while (boolean condition) { statements } /* The statements are carried out while the condition tests true. */ 21 February 2014 Birkbeck College, U. London 13

  14. Flowchart for the while Loop test condition false true execute statements 21 February 2014 Birkbeck College, U. London 14

  15. Example: compound interest double balance = 10000; int year = 0; while (balance < TARGET) { year++; double interest = balance*RATE/100; balance = balance+interest; } 21 February 2014 Birkbeck College, U. London 15

  16. Access to Variables double balance = 10000; … while (balance < TARGET) { double interest = balance*RATE/100; … } /* The variable balance is declared outside the loop and is available inside and outside the loop. The variable interest is declared inside the loop and is only available inside the loop. A new version of the variable interest is created on each iteration. */ 21 February 2014 Birkbeck College, U. London 16

  17. Compute Time to Double an Investment public class DoubleInvestment { public static void main(String[] args) { final double RATE = 5; final double INITIAL_BALANCE = 10000; final double TARGET = 2*INITIAL_BALANCE; double balance = INITIAL_BALANCE; int year = 0; /* while loop here */ System.out.println("Investment doubled after "+year+" years"); } } 21 February 2014 Birkbeck College, U. London 17

  18. Test Cases • Use very simple test data to check that the while loop is correct. • Eg. if RATE = 100.1%, TARGET = 2*INITIAL_BALANCE then the balance is slightly more than doubled at the end of the first year. • Check the value of year on exiting the loop. 21 February 2014 Birkbeck College, U. London 18

  19. while Loop Example 1 int i = 0, sum = 0; while(sum < 10) { i++; sum = sum+i; System.out.print(i+""+sum+", "); } /* output: 1 1, 2 3, 3 6, 4 10, */ 21 February 2014 Birkbeck College, U. London 19

  20. while Loop Example 2 int i = 0, sum = 0; while(sum < 10) { i++; sum = sum-i; System.out.print(i+""+sum+", "); } /* output: 1 -1, 2 -3, 3 -6, 4 -10, … */ 21 February 2014 Birkbeck College, U. London 20

  21. The for Loop for(int i = 1; i <=10; i++) { System.out.println(i); } /* Use a for loop when the number of iterations is known in advance. The for loop is count controlled. The while loop is event controlled. */ 21 February 2014 Birkbeck College, U. London 21

  22. Parts of the for Statement for (int i = 0; i <= 10; i++) { … } /* i = 0: initialisation, executed once on entering the loop. i <= 10: condition to be checked before each iteration. i++: update executed after each iteration. */ 21 February 2014 Birkbeck College, U. London 22

  23. Examples of for Loops 21 February 2014 Birkbeck College, U. London 23

  24. Avoid Confusing for Loops double x, sum = 0; for(System.out.print(“Input: ”); in.hasNextDouble(); sum += x) { x = in.nextDouble(); } /* The compiler does not check whether the initialisation, condition and update are related. in.hasNextDouble() tests to see if the next input is a number. */ 21 February 2014 Birkbeck College, U. London 24

  25. Sum and Average Value double total = 0; int count = 0; while (in.hasNextDouble()) { double input = in.nextDouble(); total = total+input; count++; } double average = 0; if (count > 0){average = total/count;} 21 February 2014 Birkbeck College, U. London 25

  26. Counting Matches int upperCaseLetters = 0; // initialise a counter for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); //obtain the ith character in str if (Character.isUpperCase(ch)) //test for upper case { upperCaseLetters++; } } /* Character.isUpperCase is a method in the class Character which is in the package java.lang. */ 21 February 2014 Birkbeck College, U. London 26

More Related