0% found this document useful (0 votes)
13 views

Chapter 3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Chapter 3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 3 selection

False TRUE
if<expression>

Statemnt2 statment1

Relational operators:-
Math java example java

= == (a==b)
≠ != (a!=b)
> > (a>b)
< < (a<b)
≥ >= (a>=b)
≤ <= (a<=b)

(15>20) false (14!=50) true (500<=600) true (500==700) false


('A'>'B') false ('H'>'C') true
(15.7>15.8) false (15%4 <17%4) false

One way selection:-


If<expression> statement1;
Statement2;
Example:- 150
int x=50; 250
if(x>=5) System.out.println(x+100);
system.out.println(x+200);

example 2
int x=50; 250
if(x<=5) system.out.println<<x+100;
system.out.println<<x+200;

2- Two way selection

If<expression> statement1;
else
Statement2;
Example :-
int x=25;
if(x<=100) ) system.out.println (x*2);
else
system.out.println (x/2);
example 1:-

write program to input two integers (number1,number2)then find and


print the maximum.
{
import java.util.Scanner;
public class q1 {
public static void main(String[] args) {
Scanner kbd=new Scanner(System.in);
int number1,number2, max;

number1=kbd.nextInt();
number2=kbd.nextInt();
if( number1>number2)max=number1;
else

max=number2;
System.out.println("the maximum="+max);
}
}

-----------------------------------------------------------------------------------------------------------

Q1: write a program to do the following:-


1- Prompts the user to enter from the keyboard the employee’s salary
in BD and work duration in months.
2- Calculate the Tax as shown in the table :-
Salary range in BD Tax
Between 0 and 500 5% from salary
More than 500 8% from salary

3- Calculate Net-salary, which equals the salary minus Tax.

4- Calculate the total salary =Net salary x months.

5- Display the value of Tax and Total salary.

Solution:-
import java.util.Scanner;
public class q1 {
public static void main(String[] args) {
Scanner kbd = new Scanner((System.in));
double sal, tax;
double dr;
System.out.println("Enter employee salary");
sal = kbd.nextDouble();
System.out.println("Enter the duration");
dr=kbd.nextDouble();
if (sal <= 500) tax = .05 * sal;
else
tax = .08 * sal;

double netsal=sal-tax;
double tsal=netsal*dr;
System.out.println("tax="+tax);
System.out.println("toatal salary=="+tsal);
}
}

Nested if statement:-
Syntax
If(expression 1) st1;
else If(expression 2) st2;

else If(expression 3) st3;


.
.
. else If(expression n) stn;

else
statement;
Example :-
Int x;

X=kbd.nextInt();
If( x==1) system.out.println("one");
else
If( x==2) system.out.println("two");
else

If( x==3) system.out.println("three");


else
System.out.println("invalid");

Q3:- write a program that asks a user to enter a temperature .The


Program then prints a message describing the weather according to the
Following table:-

Temperature Message
T <10 COLD
10≤T<20 PLEASANT
20≤T<30 WARM
T≥30 HOT

import java.util.Scanner;
public class q1 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int T;
System.out.println("Enter temperature\n");
T=kbd.nextInt();
if(T<10)System.out.println("COLD");
else
if(T<20)System.out.println("PLEASANT");
else
if(T<30)System.out.println("WARM");
else
System.out.println("HOT");

}
}

Q4:- write a program that asks user to input a digit from 2 to 9.your
Program print corresponding letters on mobile phone keyboard as shown
In the table below:-

digit 2 3 4 5 6 7 8 9
Letters ABC DEF GHI MNO PQRS TUV WXYZ TEV

Display an invalid message in case the user enters an invalid digit.


Sample run 1 sample run 2 sample run 3

Enter a digit 4 Enter a digit 7 Enter a digit 12


GHI TUV Invalid digit

import java.util.Scanner;
public class q1 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int digit;
System.out.println("Enter a digit\n");
digit=kbd.nextInt();
if(digit==2)System.out.println("ABC");
else
if(digit==3)System.out.println("DEF");
else
if(digit==4)System.out.println("GHI");
else
if(digit==5)System.out.println("MNO");
else
if(digit==6)System.out.println("PQRS");
else
if(digit==7)System.out.println("TUV");
else
if(digit==8)System.out.println("WXYZ");
else
if(digit==9)System.out.println("TEV");
else
System.out.println("INVALID");
}
}

Compound if statement:-
import java.util.Scanner;
public class ex1
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double grade;
System.out.println("Enter your grade ");
grade=input.nextDouble();
if(grade>=60) {
System.out.println("pass");
System.out.println("GOOD STUDENT");
System.out.println("NOW YOU CAN TAKE ITCS114");
}
else {
System.out.println("FAIL");
System.out.println("TRY AGAIN");
System.out.println("you can't take itcs114");

}}

Java logical operators:-

Name Java notation example answer


and && (50>2&&100<50) false
Or || (50>2||100<50) true
not ! !(100>2) false
True && True true (100<90 && 112<200) F
True && false false
False && True false
False && false false
True || True True (15>200 || 200>1000)F||F=F
True || false true
False || True True
False || false false
Q5:- write program to input three integers (number1,
number2, number3) then find and print the maximum.
import java.util.Scanner;
public class q2 {

public static void main(String[] args) {


Scanner kbd=new Scanner(System.in);
int n1,n2,n3,max;
System.out.println("Enter three integers");
n1=kbd.nextInt();
n2=kbd.nextInt();
n3=kbd.nextInt();
if(n1>n2 && n1>n3) max=n1;
else
if(n2>n1 && n2>n3)max=n2;
else
max=n3;
System.out.println("mximum="+max);

}
}
Q2:-Write a program that asks the user to input the “air” ,”water” ,or “steel” in any case,
and the distance in feet that a sound wave will travel in medium. Any other medium print
invalid message and quit the program. Your program is required to display the amount
of time it will take using the speed specified in table below. Note the

Time =distance÷ speed.

Medium speed
Air 1,100 feet per seconds
Water 4,900 feet per seconds
Steel 16,400 feet per seconds

Sample input/output

Enter a medium and distance


Water 6304.6
The time to travel water is 1.28665 Seconds

Solution
import java.util.Scanner;
public class q2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double time=0;
String name;
double distance;
System.out.println("Enter medium and distanc");
name = input. Next();
distance = input.nextDouble();
if (name.equalsIgnoreCase("air"))
time = distance / 1100;
else if (name.equalsIgnoreCase("water"))

time = distance / 4900;


else if (name.equalsIgnoreCase("steel"))

time = distance / 16400;


else {
System.out.println("invalid medium");
System.exit(0);
}
System.out.println("the time to travel in " + name + "is" +
time + "seconds");

}
}

Switch statement:-
Syntax:-
Switch(expression)

case v1:statements;

break;

case v2:statements;

break;

case v3:statements;

break;

default:statements;

break;

example :-
int x=23;

switch(x%7)

case 5:system.out.println(x*2);

break;

case 10:case 15: system.out.println(x*3);

break; 11
case 2:system.out.println(x/2);
break;

default: system.out.println(x/5);

break;

============================================.
Example 2:
1- What is the ouput
2- Convert from (switch) to nested if else
import java.util.Scanner;
public class ifstring {
public static void main(String[] args) {

int x=53;
switch(x%7) {
case 1:
case 2:
System.out.println(x * 2);
break;
case 4:
System.out.println(x / 2);
case 10:
case 7:
case 6:
System.out.println(x % 5);
break;
}
}
}

1-Output 26

3- Convert to nested if else


int x=53;
If(x%7==1) ||(x%7==2) System.out.println(x * 2);
else
If(x%7==4) System.out.println(x / 2);
else

If(x%7==10 || (x%7==7) || (x%7==6)) System.out.println(x %5);

Q5:- write java program that prompt user to input the number of day from (1-7).if input
day number from (1-5) display "week day" and if input (6,7)display "week end".
Otherwise, display "Invalid day number".

Please use only switch.


import java.util.Scanner;
public class q1 {
public static void main(String[] args) {
Scanner kbd = new Scanner((System.in));
int day;
System.out.println("Enter number of day");
day=kbd.nextInt();
switch (day)
{
case 1: case 2: case 3: case 4: case
5:System.out.println("week day");
break;
case 6: case 7:System.out.println("Week end");
break;
default:System.out.println("invalid day");
break;
}
}
}

Conditional operator
WHAT IS THE OUPUT OF THE FOLOWING C++ CODE:-

int a=-4,j; 16
j=(a>=0?0:a*a);
System.out.println(j);
Convert from conditional operator to if… else
if(a>=0) j=0;
else
j=a*a;
System.out.println(j);
WHAT IS THE OUPUT OF THE FOLOWING C++ CODE:-

int x=10 ,y=20;


20
if(x>=0 && x<=10) System.out.println(y);

else

System.out.println(x);

You might also like