简体   繁体   中英

How do I call a method from another class in Java?

here are my 3 classes: which are the main method, dog class and getFee class.

Main method

package test;

import java.util.Scanner;
import pets.Dog;
import Utilities.FeeCalculator;

public class TestKennel 
{
    public static void main(String[] args) 
    {   
        Scanner input = new Scanner(System.in);

        Dog dog = new Dog();

        System.out.println("Hello " + dog.name + ", who's a good dog?");

        dog.talk(); 

        FeeCalculator getFeeStatment = new FeeCalculator();

        getFeeStatement.getFee();

        input.close();
    }

}

Dog Class

package pets;

public class Dog 
{
    public String name = "Fido";
    public byte age = 10;
    public void talk()
    {
        System.out.println("Woof Woof Woof!!!");
        System.out.println("I am " + age * 7 + " the equivalent of human years old.");
    }
}

and finally the getFee Class

package Utilities;

import pets.Dog;
import java.util.Scanner;

public class FeeCalculator 
{
    double dailyRate;

    public String getFee(Dog dog, Scanner input)
    {
        System.out.println();
        System.out.println();

        System.out.println("What is the name of your pet?");
        dog.name = input.next();

        System.out.println("Input how many days your pet will be staying.");
        int numberOfDays = input.nextInt();

        String feeStatement = "The cost for boarding "
                                + dog.name
                                + " for "
                                + numberOfDays
                                + " days is $" + numberOfDays * dailyRate;

        return feeStatement;
    }
}

at the main method part, why is eclipse giving me an error? isn't this how i call a method?:

FeeCalculator getFeeStatment = new FeeCalculator();

getFeeStatement.getFee();

help will be really appreciated!

FeeCalculator class has method getFee() with two arguments.

public String getFee(Dog dog, Scanner input){}

you need to pass the arguments in order to invoke it.

change

getFeeStatement.getFee();

to

getFeeStatement.getFee(dog,input );

also variable names are different, FeeCalculator getFeeStatment = new FeeCalculator(); and getFeeStatement.getFee(dog, input); . Spelling mistake for getFeeStatment

您必须传递方法参数,例如:

getFeeStatement.getFee(dog, input);
getFee(dog,input); 

--> Returns a String so do someting with it :-)

For example:

String s = getFeeStatement.getFee(dog, input); System.out.println(s);

Since the prototype is

public String getFee(Dog dog, Scanner input)

your call getFeeStatement.getFee(); requires two arguments, one of Dog type and the other of Scanner type.

As per your code I think what you need to pass is getFeeStatement.getFee(dog, input);

Also if you require to save the value that you're getting from this function, remember to assign this statement to a String variable. Hope this helps.

It would be helpful if you provided the error that eclipse is throwing. For instance one reason you're getting an error is probably because of spelling mistakes. Check your Initialization parameters or not passing parameters, but I suspect the former. You have:

FeeCalculator getFeeStatment = new FeeCalculator();
getFeeStatement.getFee();

Now, the variables getFeeStatment is different from getFeeStatement , check the spellings . Another one is you do not have parameters in your method call. The method signature shows you expect two parameters. You need to pass the two parameters for it to work.

PS Also desist from using capitalization in your package names. It's a Java conventation to use lower case for package names.

Either Add Parameters for the method getFee(); in TestKennel class or implement the getFee(); method in FeeCalculator Class without parameters!

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