简体   繁体   中英

Java overload method not used

I have this activity where I need to write an overloading method to compute for the area of square, circle, trapezoid and triangle. Ask the user to input values for the length of the sides, base, vertical height and radius. Use method name AREA(). This is the code that I tried but I always get error method already defined. I tried to change one of the data type of the parameter and I didn't get the error but when I try to run the program, only one method is used/called.

import java.util.*;
import java.lang.*;

public class Method
{
 static double getSides() 
{
 double sides;
 System.out.print("Input sides here: ");
 sides = input.nextDouble();
 return sides;
 }
 static double getRadius() 
 {
 double radius;
 System.out.print("Input radius here: ");
 radius = input.nextDouble();
 return radius;
 }
 static double getBase() 
 {
 double base1;
 System.out.print("Input base 1: ");
 base1 = input.nextDouble();
 return base1;
 }
 static double getUpperBase() 
 {
 double base2;
 System.out.print("Input base 2: ");
 base2 = input.nextDouble();
  return base2;
 }
 static double getHeight() {
 double height;
 System.out.print("Input height: ");
 height = input.nextDouble ();
 return height;
 }
 static double AREA(double sides){
 return(sides * sides);
 }
 static double AREA(double radius){
 return (Math.PI * radius * radius);
 }
 static double AREA(double base1, double height){
 return (0.5 * base1 * height);
 }
 static double AREA(double base1, double base2, double height){
 return (0.5 * (base1+base2)* height);
 }
 static Scanner input = new Scanner (System.in);
 public static void main(String[] args) {
 double sides, radius, base1, base2, height;
 sides = getSides ();
 radius = getRadius ();
 base1 = getBase ();
 base2 = getUpperBase ();
 height = getHeight ();
 System.out.printf ("\nThe area of the square is: %.4f", AREA(sides));
 System.out.printf ("\nThe area of the circle is: %.4f", AREA(radius));
 System.out.printf ("\nThe area of the triangle is: %.4f", AREA(base1,
height));
 System.out.printf ("\nThe area of the trrapezoid
 is: %.4f", AREA(base1,
base2, height));
 }
}

There is no problem with your idea, but there are compiler limitations when you try to achieve method overloading in java. Since you're using arguments of similar data types and the same return type for your methods, it creates a problem with these two functions:

static double AREA(double sides){
 return(sides * sides);
 }
 static double AREA(double radius){
 return (Math.PI * radius * radius);
 }

The other two overloaded functions are alright because you have differentiated them by a different number of parameters. REMEMBER you can use

  1. Different number of parameters
  2. Changed order of parameters
  3. Different data types for your method parameters to differentiate two methods while you try to achieve method overloading.

Another way you can use is to create an abstract method returnType Area( datatype parameter1 , datatype parameter2 ) and inherit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM