ORs a simple java program to print just one star (* ) through the base case.
ed Wee | lo
ae
cy PRR CCR cu acu t18) bd
road i
7 ae
5
D ota er
D a
5
10 oetae es toiet)
" ‘
ry
a ees sao2Mrite a simple java program for to make a calculator for addition, subtraction
, division, multiplication of 2 numbers by using a method
Ev) B
Protea eee y CET Ia
Protect hc cuten rons O
int a=6; a
cased
Ten Fy
Siac
Multiplication(a,b):
Pee CCnon
3
public static void Add(int a, int b) {
Seeeeeetrsuens}
>
public static void Subtract(int a, int b) ¢
Seeeecssucn oy
5
public static void Multiplication(int a, int b) {
eee sustCuon
>
public static void Division(int a, int b) {
Scere eusuero}
syMate RMN eee e-mail M Cel me sl mer
Tae Ra oll oe Roel eee al
Mainjova re
fl
Peete cts Cree Ot a Cec
3 eee ere Neate ta ett
A
Peete sent htcccutsm ons
G
2 Ce ae Rees Ca
o
A
a Seen are eet)
"
2
13 Scns st Wee eee ae
7 Peeters ae eas
eer)
cy
roe)
coiv)Write a simple java program of Factorial of number given by user.
rer er
Pemisecratrat St)
Prorat sn yCn SU eet em CL)
cs
eRe
ee ee
Pot aes ee
Pen Ostet Reta
ees Scat atcae as
Poe tersutscre
pra crs
Soeur sts eeTureeas)
ra
poeiacrt
5
omer Ce tate Ce ae Re a
Bs
cose es
epee ered |
oro
out werent eaWrite a simple java program of Fibonacci Series of number.
ere rr
Poetic tee R Cen mL me
int n= 10, firstTerm = 0, secondTerm = 1;
System-out.println("Fibonacci Series till" +n +" terms:
for (int i= 1; i <= nj ++i) {
Sei Se dae Ue ee ed
en ana
Sees
eae ee
Pee ee
so
0, 1, 1, 2, 3, 5, 8 13, 21, 34,
Reo er ee cron)
ess ENTER to exit console.[]Define a method
return value method formal
modifier type name parameters
> Public static int{max(int num, int num2)] {
int result; 1
parameter list method
if (num1 > num2) signature
result = numl;
else
result = num2;
return result; < return value
}
A method definition consists of a method header and a method body.
Invoke a method
int z = max(x, y);
a
actual parameters
(arguments)Defining Methods
Amethod isa collection of statements that are grouped together to perform an operation.
modifier returnValueType methodName(list of parameters) {
// Method body;
Method Declaration
Return Type
Access Specifier | MethodName Parameter List
Iimethod body Method SignatureStatic Method
A method that has static keyword is known as static method. In other words, a method that _
belongs to a class rather than an instance of a class is known as a static method. We can also
create a static method by using the keyword static before the method name. ne
The main advantage of a static method is that we can call it without creating an object. Tt
access static data members and also change the value of it. It is used to create an instance
method. It is invoked by using the class name. The best example of a static method is
the main() method.®® ®@ram hs #
&
8
Veeco os Sten U4 ea
Cee arene
"
or
cE}
a7
or
iTS
vi
cr
ot
20
ra]
2
3 Output
na Cs 40
PTE tS E ESR iced 50
crete Cae Bean 40
Fra ee
PSC scot Sts70)
(
System. out.printin(a);
cue stisut
,
static void staticDisplay()
&
SUR a Is ua CO
b
PEC CESS UCLis SmI}
(
Co SOF
Sees ETOH
staticDisplay();
+Calling Methods
pass the value #
pass the value j
public static yoid main(StringL] args) {| -| public static int maxCint num, int num2) {
inti=5; 1 | int result;
int j a —
‘int k = maxi, 9); = if (uml > num2)
Bie result = numl;
System.out.print1n( — else
"The maximum between "+ 4 + ——~_ result = num2;
"and "+j+" is" +k);
} ~ return result;
Figure 5.2 When the max method is invoked, the flow of control transfers to it. Once the max method is finished, it \
returns control back to the caller.vii)Write a simple java program to find area of triangle with base & height.
Hint : Area of triangle = 0.5 *base *height
or & en
eta ae
i” ais public static void main(String args(1) ¢ ‘Area of Triangle is: 1933.75
F Pertti
7 2 Rae ns
5 double half = 0.5;
‘i Pert eerran reno
7 BCH E MEM Co ne See Os
oe