Programming Methodology (1)
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); }
RUN *** Product Price Check *** Enter initial price: _ 1000 Enter tax rate: _12.5 Cost after tax =
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); }
Selection
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.
Making choices
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 ?
Selection in Java if statement if…else statement switch statement
The ‘'if’' statement if ( ) { } // some code here // some code here // some code here /* a test goes here */ question
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
Remember: you can have multiple instructions inside an if statement!
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;
*** Product Price Check *** Enter initial price: 50 Enter tax rate: 10 Cost after tax = 55.0
*** Product Price Check *** Enter initial price: 1000 Enter tax rate: 10 Special Promotion: Your tax will be halved! Cost after tax =
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!"); }
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!"); }
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!"); }
The ‘'if…else’' statement if (/* a test goes here */ ) { } // some code here // some code here // some code here // some code here else { }
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
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"); } }
What exam mark did you get? _
What exam mark did you get? 52 Congratulations, you passed Good luck with your other exams
What exam mark did you get? _
35 Im sorry, but you failed Good luck with your other exams
Combining tests
5 Celsius TEMPERATURE 12 Celsius
if ( )temperature >= 5temperature <= 12&&
Logical operatorJava counterpart AND OR NOT && || !
if ( )temperature >= 18! ( )
Nested ‘if…else’ statements
grouptime A10.00 a.m. B1.00 p.m. C11.00 a.m
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') { } 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"); }
The 'switch' statement
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
The 'switch' statement: An example….
grouptime A10.00 a.m. B1.00 p.m. C11.00 a.m
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.");
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; a.m1.00 p.m11.00 a.mNo such group No such group11.00 a.m
Combining options grouptime A10.00 a.m. B1.00 p.m. C11.00 a.m
Combining options grouptime A10.00 a.m. B1.00 p.m. C10.00 a.m
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"); }
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"); }
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
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
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
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
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
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
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
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
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
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
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.
Enter first number _
Enter first number 12 Enter second number _
12 Enter second number 6 Guess the sum _
Enter first number 12 Enter second number 6 Guess the sum 18 Correct answer!
Enter first number _
Enter first number 12 Enter second number _
12 Enter second number 6 Guess the sum _
Enter first number 12 Enter second number 6 Guess the sum 14 Wrong answer! = 18