Chapter 2
Chapter 2
}
}
public class ex1
{
public static void main(String[] args) {
System.out.println("* ");
System.out.println("** ");
System.out.println("*** ");
System.out.println("**** ");
System.out.println("***** ");
}
}
import java.util.Scanner;
public class j1 {
public static void main(String[] args) {
int no1,no2,no3,s,pr;
double av;
string x;
Order of precedence:-
1- * / %
2- + -
Example:-
3*7-6+2*5/4+6
21-6+2+6=23
2+5*10%6=4
a= (2+5)%10*2=14
import java.util.Scanner;
public class j1 {
public static void main(String[] args) {
int x=15;
double b;
import java.util.Scanner;
public class j1 {
public static void main(String[]
args) {
int x=10;
System.out.println(++x + 100);
System.out.println(x);
x=x--*2;
System.out.println(x);
System.out.println(x+100);
int z=5;
z-=3; // z=z-3;
System.out.println(z);
}
}
input statement:-
Q1:-Write a program that will ask the user to enter the width and
length of a rectangle then calculate and display the area and
circumference. The formulas are:
Circumference=2(length + width)
Q2- write a program that inputs three integers from the keyboard and
prints the sum, average and product. The screen dialogue should be as
follows: -
Sum= 22
Average = 7.3333
Product = 360
import java.util.Scanner;
public class ex1 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n1,n2,n3;
int s;
System.out.println("Enter three integers");
n1=input.nextInt();
n2=input.nextInt();
n3=input.nextInt();
s=n1+n2+n3;
int pr=n1*n2*n3;
double av=s/3.0;
System.out.println("sum="+s);
System.out.println("product="+pr);
System.out.println("Averag="+av);
}
Q3: Write a program that reads from the keyboard a distance in meters
and the time taken as three numbers, hours, minutes, and seconds. Your
program should display the time in hours only, the distance into miles,
and the speed in miles per hours. Note that 1 mile = 1609 meters. Then
compute speed =distance in miles/time in hours. See sample I/O below.
SAMPLE INPUT/OUTPUT
import java.util.Scanner;
public class p1 {
public static void main(String[] args)
{
Scanner kbd=new Scanner(System.in);
int dm;
int h,m,s;
System.out.print("Enter distance in meters:");
dm=kbd.nextInt();
System.out.print("Enter time:");
h=kbd.nextInt();
m=kbd.nextInt();
s=kbd.nextInt();
double th=h+m/60.0+s/(60.0*60.0);
System.out.println("The time in hours is"+th);
double dmile=dm/1609.0;
System.out.println("The distance in miles
is"+dmile);
double speed=dmile/th;
System.out.println("Speed in mile/hour ="+speed);
}
Q6: - write program that prompts user input time for event is seconds. Then the program
outputs the time in hours, minutes and seconds. (example if the time is 9630) seconds the output:
2:40:30
import java.util.Scanner;
public class p1 {
public static void main(String[] args)
{
Scanner kbd=new Scanner(System.in);
int time;
System.out.println("Entre time in
second");
time=kbd.nextInt();
int h=(time)/3600;
System.out.println("h="+h);
int s=time%60;
System.out.println("s="+s);
int m=(time %3600)/60;
System.out.println("m="+m);
}
Flowchart
Flowcharts are usually drawn using some standard symbols; however, some special symbols
can also be developed when required. Some standard symbols, which are frequently, required
for flowcharting many computer programs are shown in Table 1.
Symbol description
Computational steps or processing
function of a program
Flow line
start
Dispaly"Enter
width &length"
Input W,L;
Ar=w*l;
Display Ar;
END
type two :- selection:-
False TRUE
if<expression>
Statemnt2 statment1
Relational operators:-
= == a==b
≠ != 50!=500
≤ ≥ < >
Example
(50>20) the answer True (105!=200) true 200>=205 false 17.8>15.5 true
start
Dispaly" enter
salary &duration"`
Input salary,
duration
False TRUE
If salary <=500
Tax=.08*salary Tax=.05*salary
Nsalary=salary-tax
Tsalary=nsalary*duration
END
Q2:-Draw flowchart to enter number of cinema tickets to bye
and their type (all bought tickets are of same Type). The flowchart will then
calculate and display total tickets cost according to the table below:
Ticket type Price per ticket
Standard 3 BD
3D 4 BD
VIP 10 BD
start
Dispaly" enter
ticket no & type"
&duration"`
False TRUE
If
type="standard"
TRUE
If type="3D " Cost=3*no;
Cost=4*no;
false
Display cost
Cost=10*no;
end
1- Prompts the user to enter from the keyboard the item type and item cost in BD.
2- Calculate the profits as 30% from item cost.
3- Calculate and display the value added tax (VAT) on the item cost plus
profit as shown in the table below.
ESSENTIAL 3%
start
display"enter
type &cost"
Input
cost,type
Prpfit=30%*cost
Tcost=cost+profit
true
false
If type="
ESSENTIAL"
end
Q4:- The amount of medicine to be taken by patients is based on their weight according to the
following table:
PATIENT WEIGHT MEDICINE AMOUNT
import java.util.Scanner;
public class p1
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String st;
st="Bahrainaa";)
System.out.println(st.toLowerCase());
System.out.println(st.toUpperCase());
System.out.println(st.concat(" I love you"));
System.out.println(st.length());
System.out.println(st.charAt(2));
System.out.println(st.compareTo("Bahrain"));
System.out.println(st.equals("bahrain"));
System.out.println(st.toLowerCase());
System.out.println(st.equalsIgnoreCase("bahrain"));
System.out.println(st.indexOf("a"));
System.out.println(st.lastIndexOf("a"));
System.out.println(st.replace("x","*"));
System.out.println(st.substring(2));
System.out.println(st.substring(2,5));
String st2=" Bahrain university my country ****
";
System.out.println(st2.trim());
}
}