Presentation is loading. Please wait.

Presentation is loading. Please wait.

07 Further testing1June 15 07 Further selection CE00858-1: Fundamental Programming Techniques.

Similar presentations


Presentation on theme: "07 Further testing1June 15 07 Further selection CE00858-1: Fundamental Programming Techniques."— Presentation transcript:

1 07 Further testing1June 15 07 Further selection CE00858-1: Fundamental Programming Techniques

2 07 Further testing2June 15 Objectives In this session, we will: look at the ternary operator and when it should be used see how if statements are used in validation use the switch statement to handle mutually exclusive choices

3 07 Further testing3June 15 Ternary operator example: Morning alternative notation for if else construction if exp1 is true, exp2 is executed, otherwise exp3 is executed only use where it doesn't reduce readability problem: output greeting based on time: prompt user for hour in 24-hour time format either output "Good morning" or "Good afternoon" exp1 ? exp2 : exp3

4 Morning analysis what data is used? hour: positive integer in range 0 – 23 input by user need selection as times in morning handled differently from those in afternoon what operations are done before the selection? create Scanner prompt user for hour input hour what operations are done for times in morning? output "Good morning" what operations are done for times not in the morning? output "Good afternoon" what operations are done after the selection? none 07 Further testing4June 15

5 07 Further testing5June 15 Morning code //output appropriate greeting Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter hour: "); int hour = myKeyboard.nextInt(); //output greeting System.out.println("Good " + (hour < 12 ? "morning" : "afternoon")); Morning.java output if condition true output if condition false

6 07 Further testing6June 15 Data validation in many applications it is necessary to validate data only perform any calculations if the data is valid validation involves selection ensuring data is correct type (not covered here) ensuring data is in range ensuring data is specific value

7 07 Further testing7June 15 Data in range example – Percentage either check for valid data being within range: or check for invalid data being outside range: if (percentage 100) //report invalid data else //process valid data if (percentage >= 0 && percentage <= 100) //process valid data else //report invalid data Percentage.java

8 07 Further testing8June 15 Data specific type example – Gender can only check for valid data being specific type: can split check and use else for invalid data: if (gender.equals("Male") || gender.equals("Female")) //process valid data else //report invalid data if (gender.equals("Male")) // process male else if (gender.equals("Female")) //process female else //report invalid data Gender.java

9 07 Further testing9June 15 Switch statement in Java alternative to if for mutually exclusive options comprises a single expression and any number of alternative cases statements expression: evaluated then matched against case statements must be byte, short, int or char case statements: each case statement must be a unique literal first case statement that matches is executed can optionally have a default case that is executed if no others match break: break statement is used to end the case statement

10 07 Further testing10June 15 Switch advantages and disadvantages although any switch statement could be implemented as an if statement, using switch has benefits: reduction in the amount of coding required improved readability of the code simplified logic but... choice must be of type byte, short, int or char easy to miss out one of the breaks

11 07 Further testing11June 15 Switch syntax switch ( choice ) { case 0: statement0; break; case 1: statement1; break; case 2: statement2; break; default: statement3; } statement0 executed if choice = 0 executed if no cases match used to terminate statements for case 0

12 07 Further testing12June 15 Switch example – DayNumber problem: output the day name based on its number 1 is Saturday 2 is Sunday etc. output message if invalid day input

13 07 Further testing13June 15 DayNumber analysis what data is used? day: integer in the range 1 – 7 input by user need selection as different message output for each day what operations are done before the selection? create Scanner prompt user for day input day what operations are done if day is 1? output "Saturday" what operations are done if day is 2? output "Sunday" similar for remaining days what operations are done if day is none of the above? output "Invalid day number" what operations are done after the selection? none Note the design doesn’t specify that switch is to be used

14 07 Further testing14June 15 //output day based on number Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter day, where 1 is Saturday: "); int day= myKeyboard.nextInt(); switch (day) { case 1: System.out.println("Saturday"); break; case 2: System.out.println("Sunday"); break; //similar code for other cases case 7: System.out.println("Friday"); break; default: System.out.println("Invalid data"); } executed if all conditions false DayNumber.java executed if day is 1 used to break out of switch statement

15 07 Further testing15June 15 Switch with fall through case can be followed by as many statements as required before break break marks the end of the case if break is omitted, execution falls through into the next case

16 07 Further testing16June 15 Switch fall through example – GradePoint problem: output a description of a Grade Point: GP 0 - 1:Uncompensatable fail GP 2 - 3:Compensatable fail GP 4 - 6:Third class GP 7 - 9:Lower second class GP 10 - 12:Upper second class GP 13 - 15:First class

17 07 Further testing17June 15 GradePoint analysis what data is used? grade point: integer in the range 0 – 15 input by user need selection as grade points fall into different categories what operations are done before the selection? create Scanner prompt user for grade point input grade point what operations are done if the grade point is 0 or 1? output "Non-compensatable fail" what operations are done if the grade point is 2 or 3? output "Compensatable fail" similar for other grade points what operations are done if the grade point is none of the above? output "Invalid grade point" what operations are done after the selection? none

18 07 Further testing18June 15 //output explanation of grade point Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter grade point: "); int gp = myKeyboard.nextInt(); switch (gp) { case 0: case 1: System.out.println("Uncompensatable fail"); break; case 2: case 3: System.out.println("Compensatable fail"); break; //similar code for other cases case 13: case 14: case 15: System.out.println("First class"); break; default: System.out.println("Invalid data"); } GradePoint.java executed if gp is 0 or 1 used to break out of switch statement

19 07 Further testing19June 15 Switch using characters - Capital the selector in a switch statement can also be a variable of type char the character may need to be extracted from a string before being used problem: input the name of a UK country (England, Wales, Scotland or Ireland) output the capital of that country assume all data is valid:

20 07 Further testing20June 15 Capital analysis what data is used? countryString: String country input by user countryLetter: char first letter extracted from countryString need selection as each country has a different capital what operations are done before the selection? create Scanner prompt user for country input countryString extract first character what operations are done if the first character is 'E'? output "London" what operations are done if the first character is 'W'? output "Cardiff" what operations are done if the first character is 'S'? output "Edinburgh" what operations are done if the first character is 'I'? output "Dublin" what operations are done after the selection? none

21 07 Further testing21June 15 //output capital of UK country Scanner myKeyboard = new Scanner(System.in); //input country and get first letter System.out.print("Enter country: "); String countryString = myKeyboard.next(); char countryLetter = countryString.charAt(0); switch (countryLetter) { case 'E': System.out.println("London"); break; case 'W': System.out.println("Cardiff"); break; case 'S': System.out.println("Edingburgh"); break; case 'I': System.out.println("Dublin"); } Capital.java

22 07 Further testing22June 15 Testing switch switch is used to implement multiple selection must check each path through switch if fall through is used, need to check all possible cases

23 07 Further testing23June 15 Summary In this session we have: used the ternary operator validated input using if statements implemented switch statements and seen how break is used to control execution In the next session we will: introduce iteration


Download ppt "07 Further testing1June 15 07 Further selection CE00858-1: Fundamental Programming Techniques."

Similar presentations


Ads by Google