Java One Shot Part 1
Java One Shot Part 1
Part - 1
Flow - 1
1) Basic Printing, next line, printing numbers also with + and -.
2) Variables, printing variables, int, float, and +,-,*,/ of integers.
3) Variables naming rules.
4) Comments
5) Taking Input
6) Modulus Operator
7) Float to int, int to float
8) Hierarchy
9) Char and ASCII
Basic program in Java
Example :
System.out.print(“Hello PW”);
System.out.print(“Hello CW”);
Output will be :
Hello PWHello CW
Printing Numbers (what computer thinks is a number
and what is a number)
"Hello" -> text
Examples :
1) System.out.print(“4”); - U
2) System.out.print(4); U ->
3) System.out.print(4+3); 7 ->
4) System.out.print(“4+3”); - Ut 3
I
No
Let us focus on int data type as of now. soo
1) Variables as containers :
↓
22, Y
1 bytes
a -> eK dabba allot
integer
1 Mb - 1000Kb
·output
I
O soo
&
40 O
22 =
U;
Sout (m);
a
40
↳
Th
Output
I
500
500;
W
2 =
2 = 21+ 5;
Cm=
In
500 + 5;
2 =
585;
Printing Variables in C & Updation of Variables
Java
int x = 5; - F Output
I
sout(x);
x = 7; . S
sout(x); Th
13
x = x + 6; F
-
13-20
sout(x);
2 =
F
x = x - 20;
=-
sout(x);
-
Arithmetic operations on int data type -
↓
X-
int x = 5;
↳ R Output
I
-
int y = 2;
y
sout(x+y);
(512)
sout
sout(x-y); 2.5
sout(x*y); 5 -
integer
sout(x/y); 2 -
integer
<9-W
output
I
The value of a is S
she value is 8
of y
->
2 = 518 - 2 = 0 The value
of 2 is O
&
W io "The value
of i 3
"
int
y =8,
5
m =
,
z =
10;
2 - S
s out ("Hello" + a +
y +2);
↓
"Hello" + "+"8"
"
+ "10"
sout)"13"+2)
Increment - Decrement operators
PPP
I
int x = 5;
Output
6
x/+;
S
↑
sout(x);
G
x/-;
I+ + 4 =
5+ 1
5
sout(x);
->
Hi - 2 = i +
/+x;
W
sout(x); -- a ->
2 = 2-1
-
–-x;
W
sout(x);
-
Y
som,
fou
I
Example : Calculate sum of 2 numbers
inter =
5;
double data type int =2;
Sout (c/y);
double x = 3.1;
-> real number
int 3;
y
=
d
integer
-
W
-
-> double z=
2; ·itput: C.O
*
N W
Output: 2.5
5/2 + 2
⑱08 + 2.3
5/2.0 - 2.5
5.02.0 - 2.5
Arithmetic operations on double data type
double x = 5;
double y = 2;
sout(x+y);
sout(x-y);
sout(x*y);
sout(x/y);
-
-
-
-
⑧ 2
Output
I
~
7. O
3. O
10.0
2. S
Example : Calculating percentage of 5 subjects
double x1 = 90; // x1 can be physics
double x2 = 91; // x2 can be chemistry
double x3 = 92; // x3 can be maths
double x4 = 93; // x4 can be english
double x5 = 94; // ohh wait comments ke baare me to bataya hi nahi
xD
double y = (x1 + x2 + x3 + x4 + x5)/5;
sout(y);
// change the marks and run each time
double pi = 3.1415; A =
22xSXS
7
double area = pi*radius*radius;
sout(area);
-> A = T22
2
v = 10 c
·
= CI
=
#v 3.14 x 10 x 10
314.
=>
O 25 x 3. M4
-
Output
-> 201. SG
->
->
W Ws Ws
z
pi
-
1) Variables can start from an alphabet or underscore _ .
-
2) Special characters except _ are not allowed.
-
3) Some particular keywords are not allowed.
-
4) Commas or blanks are not allowed.
inton, inty,
r, a
doos z
PT ↑x20x3
-
so
=
r = 10;
t = 2;
I *
100 10 2/100;
si = (p*r*t)/100;
sout(si);
(100* 102) / 100;
Taking input // Let us take a simple example
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int x;
sout(”Enter a number\n”);
x = sc.nextInt(); // user will give ‘x’ a value.
int y = x*x;
sout(”square of number that you gave is : ”);
sout(y);
~ oput
Enter Radius: 10
~ -
circle is: 31415
~
The area
of
2
-
-
-
-
HW:
input of
WAP to take
NO SUS isa a
square
number &
of
print
it.
the
Output
I
Enter first number:10
- Enter second number ao
-
- The sum is: 30
~
-
- I
W W fe
-
Taking input // SUM of 2 given numbers
Example : Take two integers input, a and b : a>b, and find the
remainder when a is divided by b.
39 = S
Teimore,des
a = S a
b 2
=T
=
Divisor x
quotient + Remainder = Dividend ↳@
G dividend-divisonx
Remainder -
quotient
z & b 9
=>
v = a -
b*9
orators ! a + b
* a b
I, 0)
-
f
-, o
a *b
d
a)b
-
remainder
↓ a%b
int
Hierarchy of operators
int i = 2 * 3 / 4 + 7 / 4 + 8 - 2 + 5 / 8 ;
↓
olde
brackets order drain multiplicateadd sub
sout(i);
t
BODMAs -
I
maths
-
Javfint)
1) Brackets
23 = 6/4 1.5
243
=
2 = 6/4 =
1
Order
2)
&3/4 = 20.75: 15
3) Div) Multi
W
23 = 20 = 0
7) Add) sub
I
output
in
Try This!
Predict the output : I
* No of 0.02.0
psvm(String[] args){
int i = 2, j = 3, k, l;
⑩
-
-
double a, b;
- 6
k = i / j * j ; -
12,3183
+R
a
0
b
-
l =C
=
j / i * i;(312) 02
·
=2
~ O
a = i / j * j ;
~ -
·43= 0
-
b = j / i * i; a =
213 3
sout(k+” “+l+” “+a+” “+b);
W
} 0.68 3
(W. -> Code
char data type int, double
& -
real no
char ch = ‘a’; integer
d
character
ASCII values
char ch = ‘a’;
a -> 97 A - 63
6G
98
Ba
->
b -
S 67
c + 99
&
d -100 ⑭
"
:
!
2 120 8g
Y
- -
I ·R - 89
Z - 9 8
MCQ Time !
MCQ 1
Which of the following is NOT a character constant
(1) ‘Thank You’
(2) ‘Enter values of P, N, R’
(3) ‘23.56E-03’
-
(4) All the above
MCQ 2
In int b = 6.6 / a + 2 * n ; which operation will be performed first?
-
(1) 6.6 / a
(2) a + 2
(3) 2 * n
(4) Depends upon compiler
MCQ 4
If a is an integer variable, a = 5 / 2 ; will return a value
(1) 2.5
int a;
(2) 3
a 512;
=
(3) 2
-
(4) 0
MCQ 5
The expression, double a = 7 / 22 * ( 3.14 + 2 ) * 3 / 5 ; evaluates to
(1) 8.28 =
7122 "5.M 3/S
(2) 6.28 =>
o s*3/S Do
(3) 3.14
E 03/S
-
(4) 0
OfS
=>
= O
5.0) 2 = 2.5
vint
a
double
MCQ 7
↳mets, N, C
The expression int x = 4 + 2 % - 8 evaluates to
(1) -6
2%-8 =
208 =
2
(2) 6
-
int 1+2 6
(3) 4
=
a =
40 2 O
⑭
=
5 % 39 = 7
5%6 = 5
&
60 a 6
=
(where b< a)
**
a %-b = a% b
MCQ 8
What will be the value of d if d is a double after the operation
d = 2 / 7.0; ?
(1) 0
doubled;
d =
2) 7.0 j
Po
v
(2) 0.2857
d =
:R