SC 4
SC 4
I/p : bSal,hra,da
i
Conditions :
thi
bSal mut be min 12000/-
ipa
Program : DemoMethods4.java
import java.util.Scanner;
Ma
class Salary
sh
{
ate
{
nk
return tSal;
Ve
class DemoMethods4
System.out.println("Enter Employee-bSal:");
int bS = s.nextInt();
if(bS>=12000)
i
{
thi
System.out.println("Enter HRA:(1 to 100)");
ipa
System.out.println("Enter DA:(1 to 100)");
int da = s.nextInt();
Ma
if((hra>=1 && hra<=100) && (da>=1 && da<=100))
{
sh
Salary ob = new Salary();
ate
System.out.println("Employee-TotSal="+totSal);
nk
else
Ve
else
{
System.out.println("Invalid bSal...");
i
o/p:
thi
Enter Employee-bSal:
18000
ipa
Enter HRA:(1 to 100)
51
===================================================================
===
Assignment:
nk
wap to read Six sub marks of a Student and calculate "totMarks and Per"
Ve
Condition:
=>float per(totMarks)
MainClass : StuMainClass
=>main()
i
=>read 6 sub marks
thi
=>calculate totMarks
ipa
o/p:
totMarks =
per =
Ma
===================================================================
sh
=========
Ex-program:
ate
wap to read two integer values and display the following based on user choice:
1.GreaterValue
nk
2.SmallerValue
Ve
Program : DemoMethods5.java
import java.util.Scanner;
class GreaterValue
{
int greater(int x,int y)
if(x>y)
return x;
i
}
thi
else
ipa
return y;
}
}
Ma
}
sh
class SmallerValue
ate
if(x<y)
Ve
return x;
else
{
return y;
class DemoMethods5
i
{
thi
public static void main(String[] args)
ipa
Scanner s = new Scanner(System.in);
Ma
System.out.println("Enter int value-1:");
int v1 = s.nextInt();
if(v1==v2)
{
nk
}
Ve
else
System.out.println("****Choice****");
System.out.println("1.GreaterValue\n2.SmallerValue");
switch(choice)
case 1:
i
int r1 = gv.greater(v1,v2);
thi
System.out.println("GreaterValue:"+r1);
break;
ipa
case 2:
Ma
SmallerValue sm = new SmallerValue();
int r2 = sm.smaller(v1,v2);
System.out.println("SmallerValue:"+r2);
sh
break;
ate
default:
System.out.println("Invalid Choice....");
nk
}
Ve
o/p:
13
****Choice****
1.GreaterValue
i
2.SmallerValue
thi
Enter the Choice:
ipa
GreaterValue:13
Ma
===================================================================
======
faq:
sh
define switch-case statement?
or
cases.
nk
syntax:
switch(value)
Ve
case 1 : statements;
break;
case 2 : statements;
break;
...
case n : statements;
break;
default : default_statements;
i
}
thi
Execution behaviour:
ipa
=>The switch-value is compared with available cases and,if match occurs the
Ma
statements are executed and switch-case is stopped using 'break'
=>If the switch-value is not compared with any available cases then 'default' is
executed.
sh
===================================================================
=====
ate
nk
Ve