Methods New
Methods New
Methods
STRUTS
By Raghu
By : :AbhiRam
Sathya Technologies…
Methods in Java
1. What is method ?
A) A Method is a group of statements that performs operation and provides result.
4. What is the return type of a method that does not return any value?
A) void
18. How to return multiple values if method has only one return type?
A) using Reference Type (Arrays, Class, Interface, enum and Collections)
19. What are the different Type of Operation one method can do?
A) Business Logic ( calculations, validations,)
Read and write data from/to Database and User Interface(Browsers/Consoles)
Code Snippets:
1. class Message{
void show(){
System.out.println("Hello Raghu ");
}
}
Method Type Method name Return type Parameters
2. class Message{
static void show(String name){
System.out.println("Hello :"+name);
}
}
Method Type Method name Return type Parameters
3. class Operation{
void add(int a,int b){
System.out.println("Result is :"+(a+b));
}
}
Method Type Method name Return type Parameters
4. class Operation{
static void sub(int a,int b){
System.out.println("Result is :"+(a-b));
}
}
Method Type Method name Return type Parameters
5. class Admin{
static void message(boolean status){
if(status) {
System.out.println("Yes! valid");
}else {
System.out.println("Sorry! Not valid");
}
}
}
Invoke Method and write output: (call same method multiple times with possible
inputs)
6. class Colors{
void message(char code){
switch (code) {
case 'R':
System.out.println("Red");
break;
case 'G':
System.out.println("Green");
break;
case 'B':
System.out.println("Blue");
break;
default:
System.out.println("Im not sure");
break;
}
}
}
ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 6|P a ge
-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
Invoke Method and write output: (call same method multiple times with possible
inputs)
7. class Export{
void print(int code,String name,double sal){
if(code==10) {
System.out.println("Hello Admin:"+name+",Salary
is:"+sal);
}else if(code==20) {
System.out.println("Hello Developer:"+name+",Salary
is:"+sal);
}else if(code==30) {
System.out.println("Hello Tester:"+name+",Salary
is:"+sal);
}else {
System.out.println("Hello :"+name+",Salary
is:"+sal);
}
}
}
Invoke Method and write output: (call same method multiple times with possible
inputs)
8. class Extreems{
static void root(int x) {
int res= x++ + ++x ;
System.out.println("Result is :"+res);
}
void mul(int x,int y) {
System.out.println("Result is :"+(x*y));
}
void power(int x) {
System.out.println("Result is :"+(x*x));
}
void quote(int x,int y) {
int res = x+2 - (y/2)*4;
System.out.println("result is :"+res);
}
9. class ArraysEx{
static void printMarks(int[] marks) {
for(int i=0;i<marks.length;i++)
System.out.println("Subject#"+i+"="+marks[i]);
}
System.out.println(subjects[i]+" is 'B'
Grade");
}else if(marks[i]>35) {
System.out.println(subjects[i]+" is 'C'
Grade");
}else {
System.out.println(subjects[i]+" is
Fail");
}
}
}
}
Invoke Method and write output: (pass at least 3 items cost in array)
Invoke Method and write output: (pass your name as character array)
Invoke Method and write output: (call print Method from Result class )
Invoke Method and write output: (call display method from Dept class)
Invoke Method and write output: (call process method, with Part object having data)
class Details{
static void view(Person s) {
Invoke Method and write output: (call view method in Details class)
s.showMessage();
}
}
Exercise
1. Write a static method which takes two numbers and print bigger number
3. Write a static method to calculate and print cube and square of given number.
4. Write a instance method to calculate and print perimeter and area of circle using
input radius.
5. Write a static method takes two inputs length and breadth, calculate and print
area and perimeter of rectangle.
7. Write instance method to calculate employee salary details by taking basic salary
as input. HRA is 8% of basic, TA 3% is of BASIC, DA is 2% of BASIC. ProfTx 200 (fix).
Total salary is sum of Basic, HRA, TA and DA and reduce ProfTx.
8. write static method that takes student marks as single input then calculate, and
print total, average.
9. Write instance method that takes product cost as input. Calcuate discount based
on cost slab.
Slab#1 1-1000 , then discount 1.2%
Slab#2 1001-10000 , then discount 2.5%
Slab#3 10001-any , then discount 5%
11. Write instance method to print mathematics table for given number
ex: 8 then 8x1=8 8x2=16 ... 8x10=80
16. Write a instance method to convert given centimeters into Meters, Feets, Inches
and print result.
17. Write a static method to take multiple course name as single input and print
them in A-Z(Sorting) order. [At least 4 course names]
18. Write a instance method to print week day full name by taking week character
as input.
H - Sunday, M- Monday, T- Tuesday, W-Wednesday, R-Thursday, F-Friday, S-
Saturday. Any other Char- No Day Found
19. Write a static method to take two integer arrays and print common values in
both of them.
20. Write a instance method to take one integer array (min length is 3) find lowest
and highest numbers in that, then print.
21. Write a static method to take array of numbers and find average of them and
print.
22. Write a instance method that takes array of numbers and swap even and odd
positions of array.
Ex: [2,4,12,22,67,80] => [4,2,22,12,80,67]
23. Write a static method to print all even numbers up to given input number
24. Write a instance method to print all prime numbers up to given input number
27. Write a static method to find 2nd biggest number in given array.
ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 21 | P a g e
-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]
28. Write a instance method to take input number and find sum of their individuals.
ex: 4321 => then 4+3+2+1 =10
29. Write a static method to check given number is Armstrong number or not?
30. Write a instance method to check given 2 String inputs are anagrams or not?
31. Write a instance method to check given number is Strong number or not?
32. Write a static method to take one number (4 or more digits input)
a. find sum of first and last digits
b. find sum of middle digits