[Java] I ran the example in the book while I was studying, and I got an error. java.lang.NoSuchMethodError

Asked 2 years ago, Updated 2 years ago, 80 views

Below is the code that was moved as it was in the book.

----------------------------------------------------------------------------------------------------------

class FruitSeller {
    int numOfApple;
    int myMoney;
    int APPLE_PRICE;

    public int saleApple(int money) {
        int num = money / APPLE_PRICE;
        numOfApple -= num;
        myMoney += money;
        return num;
    }

    public void showSaleResult() {
        System.out.println ("Remaining apple: " + numOfApple");
        System.out.println ("Sales Revenue: " + myMoney);
    }

    public void initMembers(int money, int appleNum, int price) {
        myMoney = money;
        numOfApple = appleNum;
        APPLE_PRICE = price;
    }
}

class FruitBuyer {
    int myMoney = 10000;
    int numOfApple = 0;

    public void buyApple(FruitSeller seller, int money) {
        numOfApple += seller.saleApple(money);
        myMoney -= money;
    }

    public void showBuyResult() {
        System.out.println ("current balance: " + myMoney);
        System.out.println ("Number of apples: " + numOfApple);
    }
}

class FruitSalesMain2 {

    public static void main(String[] args) {
        FruitSeller seller1 = new FruitSeller();
        seller1.initMembers(0, 30, 1500);

        FruitSeller seller2 = new FruitSeller();
        seller2.initMembers(0, 20, 1000);

        FruitBuyer buyer = new FruitBuyer();
        buyer.buyApple(seller1, 4500);
        buyer.buyApple(seller2, 2000);

        System.out.println ("current situation of fruit seller 1");
        seller1.showSaleResult();

        System.out.println ("current situation of fruit seller 2");
        seller2.showSaleResult();

        System.out.println ("the current situation of fruit buyers");
        buyer.showBuyResult();
    }

}

Running with Eclipse

Exception in thread "main" java.lang.NoSuchMethodError: chapter07.FruitSeller.initMembers(III)V
    at chapter07.FruitSalesMain2.main(FruitSalesMain2.java:46)

The error does not run. I searched and found that this error is an error that comes out when there is a problem with the main method, but I don't know what the problem is, so I'm asking like this. Help me.

java constructor instance class

2022-09-21 17:18

1 Answers

package testJava;

class FruitSeller { int numOfApple; int myMoney; int APPLE_PRICE;

public int saleApple(int money) {
    int num = money / APPLE_PRICE;
    numOfApple -= num;
    myMoney += money;
    return num;
}

public void showSaleResult() {
    System.out.println ("Remaining apple: " + numOfApple");
    System.out.println ("Sales Revenue: " + myMoney);
}

public void initMembers(int money, int appleNum, int price) {
    myMoney = money;
    numOfApple = appleNum;
    APPLE_PRICE = price;
}
}

class FruitBuyer { int myMoney = 10000; int numOfApple = 0;

public void buyApple(FruitSeller seller, int money) {
    numOfApple += seller.saleApple(money);
    myMoney -= money;
}

public void showBuyResult() {
    System.out.println ("current balance: " + myMoney);
    System.out.println ("Number of apples: " + numOfApple);
}
}

class testClass {

public static void main(String[] args) {
    FruitSeller seller1 = new FruitSeller();
    seller1.initMembers(0, 30, 1500);

    FruitSeller seller2 = new FruitSeller();
    seller2.initMembers(0, 20, 1000);

    FruitBuyer buyer = new FruitBuyer();
    buyer.buyApple(seller1, 4500);
    buyer.buyApple(seller2, 2000);

    System.out.println ("current situation of fruit seller 1");
    seller1.showSaleResult();

    System.out.println ("current situation of fruit seller 2");
    seller2.showSaleResult();

    System.out.println ("the current situation of fruit buyers");
    buyer.showBuyResult();
}
}

I built it under the name testJava once. It works well. Didn't you declare the package? Or did you copy it from the book and transfer it to ansi??


2022-09-21 17:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.