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));
}
}
squareAreaandcircleArea. And while you're at it rename the other two as well even if it's not strictly necessary.