Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);

Similar presentations


Presentation on theme: "Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);"— Presentation transcript:

1 Programming Methodology (1)

2 import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in); double price, tax; System.out.println("*** Product Price Check ***"); System.out.print("Enter initial price: "); price = sc.nextDouble(); System.out.print("Enter tax rate: "); tax = sc.nextDouble(); price = price * (1 + tax/100); System.out.println("Cost after tax = " + price); }

3 RUN *** Product Price Check *** Enter initial price: _ 1000 Enter tax rate: _12.5 Cost after tax = 1125.0

4 import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in); double price, tax; System.out.println("*** Product Price Check ***"); System.out.print("Enter initial price: "); price = sc.nextDouble(); System.out.print("Enter tax rate: "); tax = sc.nextDouble(); price = price * (1 + tax/100); System.out.println("Cost after tax = " + price); }

5 Selection

6

7 Learning objectives explain the difference between sequence and selection; use an if statements to make a single choice in a program; use an if...else statement to make a choice between two options in a program; use nested if…else statements to make multiple choices in a program; use a switch statement to make multiple choices in a program.

8 Making choices

9 display the price of the seats requested ? display a list of alternative flights ? display a message saying that no flights are available to that destination ?

10 Selection in Java if statement if…else statement switch statement

11 The ‘'if’' statement if ( ) { } // some code here // some code here // some code here /* a test goes here */  question

12 The ‘'if’' statement: an example if ( ) { } temperature = sc.nextDouble(); System.out.println(“Below freezing”); System.out.print(“Enter another temperature”); temperature < 0 -5 Below freezing Enter another temperature Enter another temperature RUN 12

13 Remember: you can have multiple instructions inside an if statement!

14 import java.util.*; public class FindCostWithDiscount { public static void main(String[] args ) { double price, tax; Scanner sc = new Scanner(System.in); System.out.println("*** Product Price Check ***"); System.out.print("Enter initial price: "); price = sc.nextDouble(); System.out.print("Enter tax rate: "); tax = sc.nextDouble(); price = price * (1 + tax/100); System.out.println("Cost after tax = " + price); } } // code to reduce tax here if price is greater than 100 if ( ) { } price > 100 System.out.println (“Special promotion: Your tax will be halved!“); tax = tax * 0.5;

15 *** Product Price Check *** Enter initial price: 50 Enter tax rate: 10 Cost after tax = 55.0

16 *** Product Price Check *** Enter initial price: 1000 Enter tax rate: 10 Special Promotion: Your tax will be halved! Cost after tax = 1050.0

17 The comparison operators of Java OperatorMeaning ==equal to !=not equal to <less than >greater than >=greater than or equal to <=less than or equal to if ( temperature >= 18 ) { System.out.println("Today is a hot day!"); }

18 The comparison operators of Java OperatorMeaning ==equal to !=not equal to <less than >greater than >=greater than or equal to <=less than or equal to if (angle == 90) { System.out.println("This is a right angle!"); }

19 The comparison operators of Java OperatorMeaning ==equal to !=not equal to <less than >greater than >=greater than or equal to <=less than or equal to if (angle != 90) { System.out.println("This is NOT a right angle!"); }

20 The ‘'if…else’' statement if (/* a test goes here */ ) { } // some code here // some code here // some code here  // some code here else { }

21 if ( ) { } mark = sc.nextInt(); System.out.println("“You have passed!"); System.out.println("“You have failed!");  System.out.println("“Good luck with other exams"); else { } mark >= 50

22 import java.util.*; public class DisplayResult { public static void main(String[ ] args) { int mark; Scanner sc = new Scanner(System.in); System.out.println("What exam mark did you get? "); mark = sc.nextInt(); if (mark >= 50) { System.out.println("Congratulations, you passed"); } else { System.out.println("I'm sorry, but you failed"); } System.out.println("Good luck with your other exams"); } }

23 What exam mark did you get? _

24 What exam mark did you get? 52 Congratulations, you passed Good luck with your other exams

25 What exam mark did you get? _

26 35 Im sorry, but you failed Good luck with your other exams

27 Combining tests

28 5 Celsius TEMPERATURE 12 Celsius

29 if ( )temperature >= 5temperature <= 12&&

30 Logical operatorJava counterpart AND OR NOT && || !

31 if ( )temperature >= 18! ( )

32 Nested ‘if…else’ statements

33 grouptime A10.00 a.m. B1.00 p.m. C11.00 a.m

34 if (group == 'A') { } else { } if (group == 'B') { } else { } if (group == 'C') { } else { } System.out.print("No such group"); System.out.print("1.00 p.m"); System.out.print("11.00 a.m"); System.out.print("10.00 a.m");

35 if (group == 'A') { } else { } if (group == 'B') { } else { } if (group == 'C') { } else { } System.out.print("No such group"); System.out.print("1.00 p.m"); System.out.print("11.00 a.m"); System.out.print("10.00 a.m"); if (group == 'A') { System.out.print("10.00 a.m"); } else if (group == 'B') { System.out.print("1.00 p.m"); } else if (group == 'C') { System.out.print("11.00 a.m"); } else { System.out.print("No such group"); }

36 The 'switch' statement

37 switch( ) { } someVariable casevalue1:// instructions(s) to be executed break; case value2:// instructions(s) to be executed break; // more values to be tested can be added default:// instruction(s) for default case

38 The 'switch' statement: An example….

39 grouptime A10.00 a.m. B1.00 p.m. C11.00 a.m

40 switch( ) { } someVariable casevalue1:// instructions(s) to be executed break; casevalue1:// instructions(s) to be executed break; // more values to be tested can be added default:// instruction(s) for default case group ‘A’:System.out.print("10.00 a.m"); ‘B’:System.out.print("1.00 p.m"); case ‘C’:System.out.print("11.00 a.m"); break; System.out.print(“No such group.");

41 switch( ) { } group case‘A’:System.out.print("10.00 a.m"); break; case‘B’:System.out.print("1.00 p.m"); break; case ‘C’: default:System.out.print(“No such group."); System.out.print("11.00 a.m");break; 10.00 a.m1.00 p.m11.00 a.mNo such group No such group11.00 a.m

42 Combining options grouptime A10.00 a.m. B1.00 p.m. C11.00 a.m

43 Combining options grouptime A10.00 a.m. B1.00 p.m. C10.00 a.m

44 switch(group) { case 'A': System.out.print("10.00 a.m "); break; case 'B': System.out.print("1.00 p.m "); break; case ‘C’: System.out.print("10.00 a.m "); break; default: System.out.print("No such group"); }

45 switch(group) { case 'A': System.out.print("10.00 a.m "); break; case ‘C’: System.out.print("10.00 a.m "); break; case 'B': System.out.print("1.00 p.m "); break; default: System.out.print("No such group"); } switch(group) { case 'A': case ‘C’: System.out.print("10.00 a.m "); break; case 'B': System.out.print("1.00 p.m "); break; default: System.out.print("No such group"); }

46

47 import java.util.*; public class SelectionQ4 { public static void main(String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _ Red 10

48 import java.util.*; public class SelectionQ4 { public static void main(String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _ Green 20 Blue Red

49 import java.util.*; public class SelectionQ4 { public static void main(String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _10 Blue Red

50 import java.util.*; public class SelectionQ5 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); } else { System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _10 Blue Red

51 import java.util.*; public class SelectionQ5 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); if (x > 10) { System.out.println("Green"); } else { System.out.println("Blue"); } System.out.println(“Red"); } Enter a number: _20 Green Red

52 import java.util.*; public class SelectionQ6 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); switch (x) { case 1: case 2: System.out.println("Green"); break; case 3: case 4: case 5: System.out.println("Blue"); break; default: System.out.println("numbers 1-5 only"); } System.out.println(“Red"); } Enter a number: _1 Green Red

53 import java.util.*; public class SelectionQ6 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); switch (x) { case 1: case 2: System.out.println("Green"); break; case 3: case 4: case 5: System.out.println("Blue"); break; default: System.out.println("numbers 1-5 only"); } System.out.println(“Red"); } Enter a number: _2 Green Red

54 import java.util.*; public class SelectionQ6 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); switch (x) { case 1: case 2: System.out.println("Green"); break; case 3: case 4: case 5: System.out.println("Blue"); break; default: System.out.println("numbers 1-5 only"); } System.out.println(“Red"); } Enter a number: _3 Blue Red

55 import java.util.*; public class SelectionQ6 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); switch (x) { case 1: case 2: System.out.println("Green"); break; case 3: case 4: case 5: System.out.println("Blue"); break; default: System.out.println("numbers 1-5 only"); } System.out.println(“Red"); } Enter a number: _10 numbers 1-5 only Red

56 import java.util.*; public class SelectionQ6 { public static void main (String[ ] args) { int x; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); x = sc.nextInt(); switch (x) { case 1: case 2: System.out.println("Green"); break; case 3: case 4: case 5: System.out.println("Blue"); break; default: System.out.println("numbers 1-5 only"); } System.out.println(“Red"); } Enter a number: _10 Red

57 Design and implement a program that asks the user to enter two numbers and then guess at the sum of those two numbers. If the user guesses correctly a congratulatory message is displayed, otherwise a commiseration message is displayed along with the correct answer.

58 Enter first number _

59 Enter first number 12 Enter second number _

60 12 Enter second number 6 Guess the sum _

61 Enter first number 12 Enter second number 6 Guess the sum 18 Correct answer!

62 Enter first number _

63 Enter first number 12 Enter second number _

64 12 Enter second number 6 Guess the sum _

65 Enter first number 12 Enter second number 6 Guess the sum 14 Wrong answer! 12 + 6 = 18


Download ppt "Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);"

Similar presentations


Ads by Google