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 = 1125.0
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
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
-5Below freezing
Enter another temperature
Enter another temperatureRUN12
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 = 1050.0
The comparison operators of Java
Operator Meaning
== 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
Operator Meaning
== 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
Operator Meaning
== 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{ }
The ‘'if…else’' statement
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?
52Congratulations, you passedGood luck with your other exams
What exam mark did you get?
_
What exam mark did you get?
35Im sorry, but you failedGood luck with your other exams
Combining tests
5 Celsius TEMPERATURE 12 Celsius
if ( ) temperature >= 5 temperature <= 12&&
Logical operator Java counterpart
AND
OR
NOT
&&
||
!
if ( ) temperature >= 18! ( )
Nested ‘if…else’ statements
group time
A 10.00 a.m.
B 1.00 p.m.
C 11.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
case value1: // 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….
group time
A 10.00 a.m.
B 1.00 p.m.
C 11.00 a.m
switch( )
{
}
someVariable
case value1: // instructions(s) to be executed
break;
case value1: // 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;
10.00 a.m1.00 p.m11.00 a.mNo such group No such group11.00 a.m
Combining options
group time
A 10.00 a.m.
B 1.00 p.m.
C 11.00 a.m
Combining options
group time
A 10.00 a.m.
B 1.00 p.m.
C 10.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 number12
Enter second number_
Enter first number12
Enter second number6
Guess the sum_
Enter first number12
Enter second number6
Guess the sum18
Correct answer!
Enter first number_
Enter first number12
Enter second number_
Enter first number12
Enter second number6
Guess the sum_
Enter first number12
Enter second number6
Guess the sum14
Wrong answer!12 + 6 = 18
Top Related