Wednesday, November 6, 2013

Java Overloaded Methods Example

Write a program that implements two overloaded methods that
  • Finds area of either rectangle or square.
  • Finds sum of two numbers(int or double).

Solution:

package assignment1;

public class Question7 {
 
 //Area for square
 public void area(float a){
  System.out.println("Area of Square: "+(a*a));
 }
 
 //Area for rectangle
 public void area(float a, float b){
  System.out.println("Area of Rectangle: "+(a*b));
 }
 
 //Sum of integers
 public void sum(int a, int b){
  System.out.println("Sum of integers: "+(a+b));
 }
 
 //Sum of floats
 public void sum(float a, float b){
  System.out.println("Sum of float: "+(a+b));
 }
 
 public static void main(String args[]){
  
  //Creating class object
  Question7 obj = new Question7();
  
  //Calling the methods
  obj.area(10);
  obj.area(10,2.65f);
  obj.sum(10,20);
  obj.sum(10.30f,2.35f);
  
 }

}


No comments:

Post a Comment