0% found this document useful (0 votes)
34 views8 pages

Function Types

The document describes a Java program that performs basic arithmetic operations. The program uses a Scanner to take user input of two numbers and an operation choice. Based on the choice, it either subtracts or adds the two numbers and prints the result. The program contains multiple classes each with a main method and static methods to define the logic. The code examples show various syntax errors being encountered and corrected.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views8 pages

Function Types

The document describes a Java program that performs basic arithmetic operations. The program uses a Scanner to take user input of two numbers and an operation choice. Based on the choice, it either subtracts or adds the two numbers and prints the result. The program contains multiple classes each with a main method and static methods to define the logic. The code examples show various syntax errors being encountered and corrected.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

·:;n %.

java • Notepad
f.i!e Edit ormat View .tfelp
i.mpo1t ja,·a.util. *;
class ss
{
public static ,·oid main(St:ring args[))
{
add0;
}
static , ·oid add()
{
Scaiuier in=ne\Y Scaiuier(System.i.n);
int a,b,c,choice :
a=in.nextintO;
b=i.n.uextlnt(); Mic~osoft Windows [ Uersion 6.1.7601]
Copyright Cc) 2009 Microsoft Corporation. All ~ights ~ese~ued.
choic e=in. . nextlntO;
C:\Use r s \Admin ) d :
switch(choice) !

:D: \)pat h="C: \P1•og1•am Piles\Jaua\jdk1.?. 0_45\bin"


I

case 1: D:\)c d jne,,,


~:\j new ) jauac ss.jaua
c=a-6;
D: \jne,,,>jaua ss
System.out.println(c); 12
break; 1
1
} 13
case~:
'o :\jne,~)
{
c=a-6 ;
System.out.println(c);
break ;
}
default
{
System.out println("Im·alid");
}
}
} ~
J:::is~2,,java· "Notepad
. tile. Edit f Qrmat Y1ew !:ielp
impoti java.util}';
dass ss2
{
public static Yoid main(String args(])
' {
Scanner in=new Scatme.r(System.in);
int x,y,choic e:
x=urnextlntQ;
n:,jnew)jauac ss2.jaua
~·=in.nextlntO;
D: ,jnew)jaua ss2
add(x,y); ~1
Q
} 11
static void add(int a,int b) ~3
{
Scanner in=new ScatU1er(System.in);
int c,choice;
choice=in.nextintO;
S\\"ttch(choice)
{
case L
{
c=a- b;
System.out .println(c);
break;
)
I
case 2:
{
c=a-b;
System.out println(c);
bre ak,
}
default
{
System.out pnntln("ltwalid");
}
-t ,' tie p
unp 0 11 _1.1,·~ un\
.:: lJ SS 55 ;
{
puH1.: st an.:: ..- oi-1 m.im Snm~ .ot gs(]'

S.::.llmerm=t1 e·,, 3.::ann er. S....-scem m' ·


UH \.y_ : .
\=u, t1<~\ t:m :
:=ul ne~.t:n( '
: =aJd ,:_y'·

St ,ltl.:: UH a.:id UH .l.Ult \:'

'iii C:\ Windows\ syst~m32\ cmd.exe


$.:: a.nner m=ne·,, 3.::.:!mc~ Sys:em.in' ;
mt .:: cl:01 .: e 1//use1' defined fun ct ion
.:: h01.::e=m :1e~t:n : : 2 el'l'Ol'S
s-.-1rd-. .:: :1.:- :.: ~
D: ,jnew)jauac ss3.jaua
::a sc i ss3 .jaua:35: el'l'Ol': missing l'etu1'n ualue
tetm'n ;
1 el'l'Ol'

D:,jne,-,)jauac ss3 .jaua


ss3.jaua:38: error: missing return statement
}//use1'
A
defined function
1 el'l'Ol'

c=a-c D:,jnew)jauac ss3.jaua


Sys t~::i 0ut p:-.::::n ;:
l:-n ak D:,jnew)jaua ss3
10
ddatli: 20
1
30
System 0ur pr-.m.l:!": !11. c~d
::::, La ll\.. lll l clUU Ul l u . uH '"-

mC:\Windows\system32\cmd .exe
Sca.tmer m=new S;:atmen Sys t em.in ); }//user defined function
mt c. cho1::e A

cho1ce=m next.:m :: 2 e1•1•01:•s


S\\ltch, chotce
D:,jnew)ja uac ss3.jaua
ss3.jaua:3 5: error: missing return ualue
case 1 1·etu1:•n
A

' c=a- b:
l
1 e1•1•01:•
Sys tem ouLpnmlnrc;·
D:,jnew>ja uac ss3.jaua
break s s3 .j a ua:38: error: missing return statement
1 //us er defined function
case:
1 e r1•01•
c= a-b n: ,jnew)jau ac ss3.jaua
Sys cem.ouc pnntln c ;
break D:,jnew)ja ua ss3
} 10
d efault 20
1
{ 30
Sys te m out pnntln' !.r),; ahd ·
1
}
return O.
s~A ._1 a . a - I lott'p ad
trle Ed t Fe rrnat ~I E v .tfelp
import p , -a.u tt! ".;
class ss ~
{
public s tatic mid main (Strin g args [];
{
Scanner in=new Scann er(Sys t em.in):
int z=addQ;
}
static int add0
{
Scanner in=n ew Scanner(Sy s t em .in) ;
int a.b,c, ch oic e:
a=in.nex:timO;
b=itrnextlm Q;
iii C:\ Windows\ system32\ cmd .exe
choice=in _ne :-:tlm O; }//class ends
A

switch (ch oic e) 3 el:'1:'01:'S


{
ca se 1: D:,jnew}jauac ss4.jaua
ss4.jaua:3?: el:'1:'0l:': illegal stal:'t of expl:'ession
}//usel:' defined function
c=a- b ; A

System.ou t.println(c) ; ss4.jaua:36: erl:'ol:': not a statement


break: :re tul:'n c; _!
}
ss4.jaua:38: el:'1:'0l:': 1:'eached end of file while parsing
case 2: }//class ends
A

c=a-b : 3 el:'1:'01:'S
Sy swn .out_prim ln(c):
D:,jnew}jauac ss4.jaua
break :
} D:,jnew}jaua ss4
default: 10
{ 1
1
System.ou t.pnntln( 'In,:alid ·;; 11
}
re turn O; D:,·new>
~ s·,r,i rr h , r::i , .. m ... n r .:.n
~s4 .java - Notepad
file f d1 t F2rr11at l.1ew !jelr
impon Ja,·a.uttl ·';
clas s s s.:
{
public sta tic ,·01d m.im ,Smn g arg-s [];
{
S.::a.rm cr m=ncw S-::annc1, Syst em.in );
mt ::=add~ :
1
:;ranc mt add :

Sc mne:- m =nF· $-::a~mcn System.in):


un a. b.c cho: .::e
a=m nc --:-,_n: kli C:\ Windows\system32\ cmd.exe I 9 __11_~ _ /
b=rn neu :m :·
)//class ends
.::ho1c t =:n ~-:> -i: r:.n: · A

3 errors

D:,jnew}jauac ss4.jaua
ss4.jaua:3?: error: illegal start of expression
)//user defined function
A
:: = 2- t
~-.·s:-: r ·. c,·.:: ?n..ri:.in c · ss4.jaua:36: error: not a statenent
::::-':2.i 1·etLU'n C; A+
ss4.jaua:38: error: reached end of file ,-,hile parsing
)//class ends
A

D:,jnew}jauac ss4.jaua
D:,jnew>jaua ss4
d-: fa.tl · 10
1
1
11

11:n.u-n
, ~· 111 - h !. t ::i · ~;n ~r. · --~ 1·
·J ss4 .ja\ a - Notepad
Elle td1t Format _y1ew ,tielp
impotl Ja,·a.uttl ".
clas5 s s..:
{
public stat1c ,·01d mam\Stnn g arg s[])
{
Scanner m=new S.: atuH· n_Syswn .m);
mt ;:=add;
}

statt.: mt add :
{
S.:211ne:· m=n e-.,· $ca.nner, Sysrem.i.n);
mt 2...l'.:: .:h01ce ·
a=t.'l m "\~:n::
c=in ~e·n.:.m ~-
ii C:\ Windows\system32\ cmd.exe
ch0:.::e=-m ne:-:t:m c· }//class
A
ends
3 e1·1·01•s

D:,jnew)jauac ss4.jaua
ss4.jaua:3?: eN·o1•: illegal start of expression
}//user defined function
:=z.- t A

~:-·:::~~- o·. it pnntln c;- ss4.jaua:36: error: not a statement


1·etLu•n c; .!
ss4.jaua:38: e1•ro1•: reacl1ed end of file ,.,J1ile parsing
)//class ends
A

: "' 2-'::.. 3 e1•1•01•s


~ · - : • ~ :-:-: (,'j '. p :--tn tln;:
D:,jnew)jauac ss4.jaua
c-i-- a.
D:,jnew>jaua ss4
10
1
1
11

return .
s ·, 1tch :t._ , .. :, , .. rl! .. n1·.
retum c T
• user dtft"n-::d fur. 11 ,~n
} clas~ l:' nd ~
import java.util.*;
class sl
{
public static
{

}
static int addO·•
{
Scanner·in.·ne\ v:S,ca11
~int aff "new illt{t0}; '
-int i~sum 'O.;
for(i 0.;i<5 ;i++)" ',,1
'I.Ir •
:.11

{ !ll:,1
'" ' I
i·•• i

a [i]=in~nextlntO;
sum=sum+a[i] ;,
-.
J
return sum ;
-t
f
-t
J

,I_,...,,...,, "., ., I I I.

You might also like