Grey
Grey

Reputation: 45

Method in another class cannot be applied to given types

Basically, there are two classes... One is supposed to work out the area of a circle, the other is for the user to enter the number of circles they want to work with, the radius of each circle and the program is then to display it after each other. At the end, the biggest area is displayed. It displays an error that the method in the first class cannot be applied to the given types...

The first class:

public class Prac4 {

private float radius;

public Prac4() {
}

public void setRadius(float radius) {
    this.radius = radius;
}

public float getRadius() {
    return radius;
}

public double calcArea() {       //Getting errors if I don't use double
    return 3.14* radius*radius;  //Getting errors if I try to use pow function as pow(radius, 2) even with java.lang.math
}
}

So the calcArea is the part being called in the second function to calculate the area of the circles. I have tried making it public float calcArea() but that brought up a whole new set of errors.

The second class:

import java.util.*;
public class Prac5
{


public Prac5() {   //Not sure why, but my lecturer says it should be like this
}


public static void main(String[] args) {

    int circleNum;
    float radius=0;
    float big=0;

    Scanner scan =new Scanner(System.in);
    Scanner rad =new Scanner(System.in);

    System.out.print("Enter the number of circles: ");
    circleNum = scan.nextInt();

    for(int i=1; i<circleNum;i++)
    {
    Prac4 circle1 = new Prac4();  //Trying to call in the other class
    System.out.printf("Enter the circle %d radius: ", i);
    circle1.setRadius(radius);
    System.out.printf("The radius is: %f", rad);
    double area = circle1.calcArea(radius); //This is where the error occurs, the .calcArea is highlighted

    if (big<area)
    {
        big = area;
}
}

System.out.printf("The biggest area is: %f",big);
}
}

I've declared the area as a double thinking it would work because calcArea is a double, and there was errors when I tried keeping everything as a float or double. I'm still new to java, so maybe there is something I'm missing?

Edit: The full error - method calcArea in class Prac4 cannot be applied to given types; required: no arguments found: float Reason: actual and formal argument lists differ in length

Upvotes: 0

Views: 1483

Answers (2)

npinti
npinti

Reputation: 52205

From what I am seeing, you are calling this: double area = circle1.calcArea(radius); but you have this: public double calcArea() {, that is, your calcArea does not take any parameters. Calling it like so: double area = circle1.calcArea(); should fix the problem.

Upvotes: 1

Eran
Eran

Reputation: 394146

This call

double area = circle1.calcArea(radius);

doesn't match the method defined in the Prac4 class, which takes no arguments:

public double calcArea()

Change it to :

double area = circle1.calcArea();

You don't have to pass the radius to calcArea(), since you're already passing it to setRadius here - circle1.setRadius(radius); - which stores the radius in the circle1 instance.

Upvotes: 3

Related Questions