Chapter 3
Chapter 3
False TRUE
if<expression>
Statemnt2 statment1
Relational operators:-
Math java example java
= == (a==b)
≠ != (a!=b)
> > (a>b)
< < (a<b)
≥ >= (a>=b)
≤ <= (a<=b)
example 2
int x=50; 250
if(x<=5) system.out.println<<x+100;
system.out.println<<x+200;
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:-
number1=kbd.nextInt();
number2=kbd.nextInt();
if( number1>number2)max=number1;
else
max=number2;
System.out.println("the maximum="+max);
}
}
-----------------------------------------------------------------------------------------------------------
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
statement;
Example :-
Int x;
X=kbd.nextInt();
If( x==1) system.out.println("one");
else
If( x==2) system.out.println("two");
else
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
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");
}}
}
}
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
Medium speed
Air 1,100 feet per seconds
Water 4,900 feet per seconds
Steel 16,400 feet per seconds
Sample input/output
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"))
}
}
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;
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
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".
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:-
else
System.out.println(x);