Вы находитесь на странице: 1из 22

-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

Sathya Technologies, Ameerpet

Methods
STRUTS
By Raghu
By : :AbhiRam

Sathya Technologies…

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 1|P a ge


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

Methods in Java
1. What is method ?
A) A Method is a group of statements that performs operation and provides result.

2. What is the syntax of creating a method?


A) <Access Speificer> <Access Modifiers> <ReturnType> <methodName> (
<Parameters>)
<throws Exceptions> {
// method body
}

3. What are optional parts in method?


A) Access specifier, Modifiers, Parameters and throws clause are optional. Only method
Name, returnType and method body are mandatory.

4. What is the return type of a method that does not return any value?
A) void

5. What is difference between Method Heading and signature ?


A) Heading : <specifier> <modifiers> <returnType> <methodName>(Parameters>)
Signature : <methodName>(Parameters>)

6. Which method executed first in Java?


A) main method

7. Can we create a method inside another method?


A) No. Nesting of methods not possible in Java.

8. How many Types of Methods in Java?


A) Two types : static method and instance method

9. What are different non-access modifiers we can apply to method?


A) static, abstract, final, native, synchronized, strictfp.

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 2|P a ge


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

10. How to call/access static method?


A) Using class Name*, using object Reference, using object

11. How to call/access instance methods?


A) Using Object Reference, using Object

12. Can we call a method inside another method?


A) Yes. We can call method only inside another method

13. Can we call a method within same method?


A) Yes we can call method within same method. It is called as method recursion. It must
be handled using some conditions and break at a point , else stack over flow error will
occure.

14. What are valid statements given below.


a. We can call static method inside static method (Valid/Not valid)
b. We can call instance method inside instance method (Valid/Not valid)
c. We can call static method inside instance method (Valid/Not valid)
d. We can call instance method inside static method (Valid/Not valid)
e. we can call methods inside constructor (Valid/Not valid)
f. we can call instance method without using Reference variable (Valid/Not valid)

15. What is difference between method parameters and arguments?


A) Arguments : variables used in methods calling
Parameters : variables used in method heading

16. One method can have how many Parameters?


A) Multiple (zero to n)

17. One Method can have how many return types ?


A) Only one.

18. How to return multiple values if method has only one return type?
A) using Reference Type (Arrays, Class, Interface, enum and Collections)

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 3|P a ge


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

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)

20. What are valid statements given below


a. We can write method using only Parameters But no Return Type(void)
b. We can write method using only Return Type(void) But no Parameters.
c. We can write method without return type(void) and parameter.
d. We can write method with ReturnType and Parametes
e. Return Type is Required if Method has Parameter.
f. Return Type is Not Required if Method has no Parameters.

Code Snippets:
1. class Message{
void show(){
System.out.println("Hello Raghu ");
}
}
Method Type Method name Return type Parameters

Invoke Method and write output:

public static void main(String[] args) {

2. class Message{
static void show(String name){
System.out.println("Hello :"+name);
}
}
Method Type Method name Return type Parameters

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 4|P a ge


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

Invoke Method and write output:

public static void main(String[] args) {

3. class Operation{
void add(int a,int b){
System.out.println("Result is :"+(a+b));
}
}
Method Type Method name Return type Parameters

Invoke Method and write output:

public static void main(String[] args) {

4. class Operation{
static void sub(int a,int b){
System.out.println("Result is :"+(a-b));
}
}
Method Type Method name Return type Parameters

Invoke Method and write output:

public static void main(String[] args) {

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 5|P a ge


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

5. class Admin{
static void message(boolean status){
if(status) {
System.out.println("Yes! valid");
}else {
System.out.println("Sorry! Not valid");
}
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call same method multiple times with possible
inputs)

public static void main(String[] args) {

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]

Method Type Method name Return type Parameters

Invoke Method and write output: (call same method multiple times with possible
inputs)

public static void main(String[] args) {

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);
}
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call same method multiple times with possible
inputs)

public static void main(String[] args) {

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 7|P a ge


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

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);
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call all method once)

public static void main(String[] args) {

9. class ArraysEx{
static void printMarks(int[] marks) {
for(int i=0;i<marks.length;i++)
System.out.println("Subject#"+i+"="+marks[i]);
}

Method Type Method name Return type Parameters

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 8|P a ge


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

Invoke Method and write output: (pass at least 3 values in array)

public static void main(String[] args) {

10. class ArraysEx{


void displayNames(String[] names) {
for(String s:names) {
System.out.println("Hello :"+s);
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (pass at least 3 names in array)

public static void main(String[] args) {

11. class StudentGrades{


void printGrades(String[] subjects,int[] marks) {
if(subjects.length !=marks.length) {
System.out.println("Pass Subjects and marks
Equally");
}else {
for(int i=0;i<subjects.length;i++) {
if(marks[i]>80) {
System.out.println(subjects[i]+" is 'A+'
Grade");
}else if(marks[i]>60) {
System.out.println(subjects[i]+" is 'A'
Grade");
}else if(marks[i]>45) {

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 9|P a ge


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

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");
}
}
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (pass at least 3 names in array)

public static void main(String[] args) {

12. class Dmart{


static void printBill(double[] costs,double discPer) {
double total=0.0;
for(double cst:costs) {
total+=cst;
}
double discountAmt=total*discPer/100;
System.out.println("Total Amount is:"+total);
System.out.println("Discount Amount is:"+discountAmt);
System.out.println("Bill Amount is:"+(total-
discountAmt));
}

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 10 | P a g e


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

Method Type Method name Return type Parameters

Invoke Method and write output: (pass at least 3 items cost in array)

public static void main(String[] args) {

13. class CharType{


static void printName(char[] c) {
String s=new String(c);
System.out.println("Hello :"+s);
}

Method Type Method name Return type Parameters

Invoke Method and write output: (pass your name as character array)

public static void main(String[] args) {

14. class Student {}


class Result{
static void print(Student s) {
System.out.println(s);
}
}

Method Type Method name Return type Parameters

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 11 | P a g e


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

Invoke Method and write output: (call print Method from Result class )

public static void main(String[] args) {

15. class Admin {}


class Dept{
void display(Admin a) {
System.out.println(a);
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call display method from Dept class)

public static void main(String[] args) {

16. class Customer {}


class Bill{
void info(Customer c) {
System.out.println(c);
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call info method)

public static void main(String[] args) {

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 12 | P a g e


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

17. class Part {


int id;
String name;
}
class Track{
void process(Part p) {
System.out.println("ID:"+p.id);
System.out.println("Name:"+p.name);
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call process method, with Part object having data)

public static void main(String[] args) {

18. class Ticket {


int tid;
String from;
String to;
double cost;
}
class Invoice{
void getData(Ticket t) {
System.out.println("ID:"+t.tid);
System.out.println("Source:" + t.from
+ ", Destination:" +t.to);
System.out.println("Amount:"+t.cost);
}
}

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 13 | P a g e


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

Method Type Method name Return type Parameters

Invoke Method and write output: (call getData method)

public static void main(String[] args) {

19. class Student {


void print() {
System.out.println("welcome to Sathya");
}
}
class Course{
static void info(Student s) {
System.out.println("-Course start- ");
s.print();
System.out.println("-Course end- ");
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call info method )

public static void main(String[] args) {

20. interface Person{}

class Employee implements Person{ }


class Student implements Person{ }

class Details{
static void view(Person s) {

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 14 | P a g e


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

if(s instanceof Employee) {


System.out.println("welcome to Employee");
}else if(s instanceof Student) {
System.out.println("welcome to Student");
}
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call view method in Details class)

public static void main(String[] args) {

21. interface Vehicle{


void show();
}
class Car implements Vehicle{
public void show() {
System.out.println("CAR");
}
}
class Bus implements Vehicle{
public void show() {
System.out.println("BUS");
}
}
class Mortor{
public void find(Vehicle s) {
System.out.println("Welcome to :");
s.show();
}
}

Method Type Method name Return type Parameters

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 15 | P a g e


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

Invoke Method and write output: (call find method in Motor)

public static void main(String[] args) {

22. interface Animal{


void getType();
}
class Bird implements Animal{
public void getType() {
System.out.println("Birds Can Fly");
}
}
class Lion implements Animal{
public void getType() {
System.out.println("Lion is King");
}
}
class LifeSource{
public void getDetails(Animal s) {
System.out.println("Hello :");
s.getType();
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call getDetails method )

public static void main(String[] args) {

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 16 | P a g e


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

23. interface Connection{


void createStatement();
}
class OracleConnection implements Connection{
public void createStatement() {
System.out.println("Oracle Connected");
}
}
class MySqlConnection implements Connection{
public void createStatement() {
System.out.println("MySQL Connected");
}
}
class JdbcOperation{
public static void save(Connection s) {
System.out.println("You are in JDBC");
s.createStatement();
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call save method in JdbcOperation)

public static void main(String[] args) {

24. interface Servlet{


void service();
}
class HomPage implements Servlet{
public void service() {

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 17 | P a g e


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

System.out.println("welcome to Home Page");


}
}
class InboxPage implements Servlet{
public void service() {
System.out.println("welcome to Inbox Page");
}
}
class WebApp{
static void execute(Servlet s) {
System.out.println("You are in Servlets");
s.service();
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call execute method in WebApp)

public static void main(String[] args) {

25. interface Message{


void showMessage();
}
class Mail implements Message{
public void showMessage() {
System.out.println("Hello Email");
}
}
class Sms implements Message{
public void showMessage() {
System.out.println("Hello SMS");
}
}
class Communication{
void process(Message s) {
System.out.println("You are in Messages");
ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 18 | P a g e
-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

s.showMessage();
}
}

Method Type Method name Return type Parameters

Invoke Method and write output: (call process metho)

public static void main(String[] args) {

Exercise
1. Write a static method which takes two numbers and print bigger number

2. Write a instance method which takes two Strings(firstName,lastName) and print


Hello Mr: <firstName> <lastName> format.

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.

6. write 2 instance methods


a. convert Foreign heat to Celsius and print result
b. convert Celsius to Foreign heat and print result

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 19 | P a g e


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

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%

Also print final product amount after discount.

10. Write static method to swap two number

11. Write instance method to print mathematics table for given number
ex: 8 then 8x1=8 8x2=16 ... 8x10=80

12. Write a instance method to convert decimal number to


a. Binary Number
b. Octal Number
c. Hexadecimal Number

13. Write a static method to convert seconds to Mins:Sec Format


ex: 190 sec => 3:10 Mins

14. write a instance method to convert seconds to Hours:Mins:Sec

15. Write a static method to convert given days into Years/Months/Days

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 20 | P a g e


-By RAGHU SIR [SATHYA TECHNOLOGIES, AMEERPET]

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

25. Write a static method to reverse a given number.

26. Write a instance method to print given number in words


Ex: 150 => ONE FIVE ZERO

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

ClassRoom Enquiry : +91 91 0092 0092 |info@sathyatech.com 22 | P a g e