Saturday 5 January 2019

Methods In Java




Today we are going to learn the method (s) in java. We can not write all the logic inside main() method.

We need to separate block of code to perform specific takes.



Ques -- What is a method (s) in java?

Answer -- Method is a set of statements (instruction/code) to perform a specific task/operation.


Ques -- Why we create methods?

Answer -- because of Reusability of code.


Ques -- How we can create a method (s)?

Answer --

Access Modifires -- public, private, protected, no access specifire (default)
Specifires -- static,stictfp,volatile,syncornized etc
Return Type -- void, int, boolean etc

[ Will discuss these keywords in our next post ]


Ques -- How can we call a method (s) in java?

Answer --  There are two types of methods in java

  1. static method
  2. instance method

1 ) Static Methods



1 ) Instance Methods





# One Example of Add and Subtract method in java 





package com.method.blog.demo;

//going to create 2 methods
// 1 - static method 
// 2 - instance method (non-static method)


public class MethodDemo {

//method 1 -- static method
// return type is void
// taking two arguments

public static void addMethod(int num1,int num2){
int sum = num1 + num2;
System.out.println("sum is "+ sum);
}

//method 2 -- non-static method
//return type -- int
// taking two argument

public int subtractMethod(int num1,int num2){
int sub = num1 - num2;
return sub; // return type is must
}

public static void main(String[] args) {
// creating objectof the class
MethodDemo md = new MethodDemo();

// static method call
// this method wont return any thing
MethodDemo.addMethod(5, 6);

// non- static method call
//this method return -- integer value
int result = md.subtractMethod(6,5);
System.out.println("Subtaction is "+result);
}
}




Practice Programming Questions In Java ( Part 3 )




Today I am going to give you more programming questions based on String.
These programs are very important and useful for  Interview and Viva.

Try to solve these Questions in both ways -- using with/without string pre-defined methods.


Question 1 - WAP to read a string from the user and print it in double quotes?

input -- I am Java From Basics
output -- " I am Java From Basics "
Hint -- escape characters


Question 2 - WAP to reverse String in Java using Iteration and Recursion?


Question 3 - How to check if a String contains only digits?


Question 4 - How to find duplicate characters in a String?
Input - "Programming"
output -
g : 2
r : 2
m : 2


*Question 5 - How to count the occurrence of a given character in String?

[Hint -- This can be easily done using Map(Collection in java kind of Data Structure) and it is very important Ques for Interview]


Question 6 - How to check if String is Palindrome?


Question 7 - Also Check palindrome for the number?


Question 8 - WAP to reverse words in a sentence with/without using the method?

  1. Input -- I am a boy.      Output -- boy a am I
  2. Input -- I am a boy.      Output -- yob a ma I




This is one more site -- to solve more question on strings -- " javarevisited.blogspot.com "