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

Chapter 2

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

Chapter 2

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

Chapter 2:-

First program in java:-


public class ex1
{
public static void main(String[] args) {
System.out.println("Welcome ");
System.out.println("To ");
System.out.println("JAVA ");

}
}
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;

Scanner kbd=new Scanner(System.in);


System.out.print("Enter no1");
no1=kbd.nextInt();
System.out.print("Enter no2");
no2=kbd.nextInt();
System.out.print("Enter no3");
no3=kbd.nextInt();
s=no1+no2+no3;
pr=no1*no2*no3;
av=s/3;
System.out.println("the sum="+s);
System.out.println("the product="+pr);
System.out.println("the avergae ="+av);
}
}
Data type:-
1- integer(int). 45 5478 9835
2- float (double). 69.55 5.8777 .0589 99,9
3- char 'A' '2' '$' '+'
4- string "Bahrain tell 5478 "
5- Boolean the value it will be TRUE or FALSE.

• A primitive type is used for simple, no


decomposable values such as an
individual number or individual
character.
• int, double, and char are
primitive types.
• A class type is used for a class of objects and has
both data and methods.
• "Java is fun" is a value of class type String
Basic element’s of java:-
1- Comments : you can write your name ,and to explain what
program.
2- Special symbols:-
+ - ? > >= <= != ==
3-Keywords or Reserved Words
Example keywords: int, public,
class,float.double
4- identifiers:- are name of things that appear
in program. Must consist of letters, digits,
underscore character (_) and must begin with
letter or underscore.
Yas first example1 _ex1 ex_3 (valid)
Invalid identifiers:-
Yase alkh space not allowed.
Example? Special symbol not allowed.
Xxy+tretr Special symbol not allowed.
2exaple start with number.

Arithmetic operators and operator precedence.

Math java example


+ + a+b
- - a-b
X (ab) * a*b
÷ / a/b
% % a%b(mod)

Example:- convert from math to java


ab+ c
d
a*b+c/d
example:-
14/3=4
17/5=3
17%3=2
100/10=10
100%10=0
10%100=10
Mixed mod operation:-
15/2=7
15/2.0=7.5

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

//Allocating Memory with Constants and Variables:

import java.util.Scanner;
public class j1 {
public static void main(String[] args) {
int x=15;
double b;

final int h=15;


b=17.5;
System.out.println("b="+b);
b=b*2;
System.out.println("b="+b);
System.out.println("x="+x);
System.out.println(x/2);
x=x%4;
x=x*4;
x=x*h;
System.out.println("x="+x);
int f=x+100;
System.out.println("f="+f);
}
}
Increment and decrement: -

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:

Area= length x width

Circumference=2(length + width)

Note: Use real number.


import java.util.Scanner;
public class p1 {
public static void main(String[] args)
{
Scanner kbd=new Scanner(System.in);
double le,wi;
double Ar,cr;
System.out.print("Enter length and width");
le=kbd.nextDouble();
wi=kbd.nextDouble();
Ar=le*wi;
cr=2*(le+wi);
System.out.println("Area="+Ar);
System.out.println("Circumference"+cr);
}

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: -

Enter three different integers: 5 9 8

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

Enter distance in meters: 2500


Enter time: 5 56 23
The time in hours is 5.939722
The distance in miles is 1.553760
Speed in mile/hour = 0.261588

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

MEANING OF A FLOWCHART a flowchart is a diagrammatic representation that illustrates the


sequence of operations to be performed to get the solution of a problem. Flowcharts are
generally drawn in the early stages of formulating computer solutions. Flowcharts facilitate
communication between programmers and business people. These flowcharts play a vital role in
the programming of a problem and are quite helpful in understanding the logic of complicated
and lengthy problems. Once the flowchart is drawn, it becomes easy to write the program in
any high level language. Often we see how flowcharts are helpful in explaining the program to
others. Hence, it is correct to say that a flowchart is necessary for the better documentation of a
complex program.

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

Start or end of the program

Input and output

Decision making and branching

Flow line

Type one sequence:-


Q1:- draw flowchart that prompts the user to enter from the keyboard the width and length of
rectangle and calculate and display the area of this rectangle.

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

(sal>=500) True or false ('a'>='c') true or false.


Q1: Draw a flowchart for 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(salary<=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.

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"`

Input no, type

False TRUE
If
type="standard"

TRUE
If type="3D " Cost=3*no;
Cost=4*no;
false

Display cost
Cost=10*no;

end

3: - draw flowchart for a program to do the following :

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.

Item type vat

ESSENTIAL 3%

Otherwise /anything else 9%

start

display"enter
type &cost"
Input
cost,type

Prpfit=30%*cost

Tcost=cost+profit

true
false
If type="
ESSENTIAL"

Vat=.09*Tcost Display vat Vat=.03*Tcost

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

LESS THAN 20 KG 50 millimeters

BETWEEN 20 KG AND 40 KG 65 millimeters

GREATER THAN 40 KG 75 millimeters


Draw a flowchart that reads the patient weight, then compute and print the amount of medicine to
be given to the patient.

Some methods in the class String

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());

}
}

You might also like