Wednesday, October 5, 2016

Muhammad Akbar

What is a method in Java Tutorial

We hope you have gained some knowledge on class and object concept. Now we are going to learn about basics of methods.
So far, the program we have written has only one method: main method.
The structure of all methods remain like below:
<access-modifier><return-type><method-name>(parameter list)
{       
        // body of method
}
Like for our main method the structure was:
public static void main(String args[])
{
	// some statement
}
Let’s create two classes to understand the basics of method. The two classes will be created in same package as before, that is in com.java.
package com.java;

public class BookWithMethod 
{
	// instance variable
	String title;
	String author;
	String genre;
	double price;
	double revisedPrice;
	
        // this method calculates the revised price
	public double getRevisedPrice(double rebateForStudents) 
	{
		double rebateAmount = price * rebateForStudents / 100;
		revisedPrice = price - rebateAmount;
		return revisedPrice;
	}
}
 The second class from which we will ‘call’ the method: getRevisedPrice
package com.java;

public class BookWithMehodImpl
{
	public static void main(String[] args) 
	{
		// give rebate for students
		double rebateForStudents = 5;

		BookWithMethod book1 = new BookWithMethod();
	
		// assigning instance variable values
		book1.title = "Java Complete Reference"; 
		book1.author = "Herbert Schield";
		book1.genre = "Computer Programming";
		book1.price = 300.0;
		
		// calculating revised price
		book1.revisedPrice = book1.getRevisedPrice(rebateForStudents);

		// creating first object of BookWithMethod
		BookWithMethod book2 = new BookWithMethod();
		
		// assigning instance variable values
		book2.title = "One, Two, Three, Infinity";
		book2.author = "George Gamow";
		book2.genre = "Science";
		book2.price = 600.0;
			
		// calculating revised price
		book2.revisedPrice = book2.getRevisedPrice(rebateForStudents);

		// output
		System.out.println("I have two books. Which are");
		System.out.println(book1.title + " written by: " + book1.author + " and the price is :" + book1.revisedPrice);
		System.out.println("And");
		System.out.println(book2.title + " written by: " + book2.author	+ " and the price is :" + book2.revisedPrice);
	}
}
The BookWithMethod class has five instance variable and one method.
// this method calculates the revised price
public double getRevisedPrice(double rebatePercentageForStudents) 
{
	double rebateAmount = price * rebatePercentageForStudents / 100;
	revisedPrice = price - rebateAmount;
	return revisedPrice;
}
public is the access-modifier, which means this method can be accessed from anywhere of the java code. You will understand this point when we will cover Access Modifiers in details.
double is specifying the return type. That means this program will return a double value. As it is doing in the return statement: return revisedPrice;
double rebateForStudents specifying that this method will accept one double value as parameter.
Now look at the second class: BookWithMehodImpl
This class has created two objects of BookWithMethod class. Let’s look at the method call.
book1.revisedPrice = book1.getRevisedPrice(rebateForStudents);
In java when you invoke a method it is called method call. So in this line we are calling getRevisedPrice method of BookWithMethod class. Same as with the instance variable, if you want to access a method we need the help of reference, which is in the above line book1.
We have defined a double variable as the first line of main method as:
double rebateForStudents = 5;
we are using that variable while calling getRevisedPrice method. It is called passing argument to the method. As the method is expecting a double value as parameter, we are passing a double value as argument to the method.
You can only call a method as per it’s definition. For instance you cannot call the method getRevisedPrice without passing any argument or with passing an argument of different type or passing more than one argument. For instance the below code will give you compilation error.
String abc = "5";
book1.getRevisedPrice(); // compilation error ( no argument)
book1.getRevisedPrice(rebateForStudents, abc); // compilation error ( more than one argument)
book1.getRevisedPrice(abc); // compilation error ( argument of a different type)
Now look at the method body:
double rebateAmount = price * rebatePercentageForStudents / 100;
revisedPrice = price - rebateAmount;
return revisedPrice;
where from it is getting the value of ‘price’ (300) ? Since you have called this method with book1 object (book1.getRevisedPrice) the ‘price’ will contain the value as you have specified while creating the book1 object.
Check this line in BookWithMehodImpl class:
book1.price = 300.0;
In contrast, When you call the same method with book2 reference, the ‘price’ will contain the value 600, since in the code you have written:
book2.price = 600.0;
In the last line the method returning the revisedPrice after calculating the revised price.
Run the BookWithMehodImpl class, you will get an output like:
I have two books. Which are
Java Complete Reference written by: Herbert Schield and the price is :285.0
And
One, Two, Three, Infinity written by: George Gamow and the price is :570.0

          

 Constructor in Java

Muhammad Akbar

About Muhammad Akbar -

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you