-1

this is a part of a code but when i'm executing i'm getting the error The program is about linear programming Sorry about my english, or about by dumb question

"error: non-static method get_total_profit(String) cannot be referenced from a static context

output = get_total_profit(ip1); under the output^ 1 error"

import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.DecimalFormat;

public class CandidateCode {

 String get_total_profit(String input1) {
    String[] inputs = input1.split(",");
    // Piece of farm land in square kilometer
    float L = Float.valueOf(inputs[0]);
    // Fertilizer in kg
    float F = Float.valueOf(inputs[1]);
    // Insecticide in kg
    float P = Float.valueOf(inputs[2]);
    // Fertilizer required in kg for square kilometer of Wheat
    float F1 = Float.valueOf(inputs[3]);
    // Insecticide required in kg for square kilometer of Wheat
    float P1 = Float.valueOf(inputs[4]);
    // Fertilizer required in kg for square kilometer of Rice
    float F2 = Float.valueOf(inputs[5]);
    // Insecticide required in kg for square kilometer of Rice
    float P2 = Float.valueOf(inputs[6]);
    // Selling price of wheat per square kilometer
    float S1 = Float.valueOf(inputs[7]);
    // Selling price of rice per square kilometer
    float S2 = Float.valueOf(inputs[8]);

    // Result Variables
    float totalRiceInsecUsed = 0f;
    float totalRiceFertUsed = 0f;
    float totalWheatInsecUsed = 0f;
    float totalWheatFertUsed = 0f;
    float areaOfWheat = 0.00f;
    float areaOfRice = 0.00f;
    float amount = 0.00f;

    while (true) {
        if ((L == areaOfRice + areaOfWheat)
                || P == totalRiceInsecUsed + totalWheatInsecUsed
                || F == totalRiceFertUsed + totalWheatFertUsed || F2 == 0
                || F1 == 0 || P2 == 0 || P1 == 0) {
            break;
        }

        float calRiceProfit = Math.min(F / F2, P / P2) * S2;
        float calWheatProfit = Math.min(F / F1, P / P1) * S1;

        if (calRiceProfit > calWheatProfit) {
            float areaInsecUsed = P / P2;
            float areaFertUsed = F / F2;
            if (areaInsecUsed > areaFertUsed) {
                L = L - areaFertUsed;
                F2 = 0;
                totalRiceFertUsed = totalRiceFertUsed + F2;
                areaOfRice = areaOfRice + areaFertUsed;
                amount = amount + areaFertUsed * S2;
            } else if (areaInsecUsed < areaFertUsed) {
                L = L - areaInsecUsed;
                P2 = 0;
                totalRiceInsecUsed = totalRiceInsecUsed + areaInsecUsed;
                areaOfRice = areaOfRice + areaInsecUsed;
                amount = amount + areaInsecUsed * S2;
            }
        } else {
            float areaInsecUsed = P / P1;
            float areaFertUsed = F / F1;
            if (areaInsecUsed > areaFertUsed) {
                L = L - areaFertUsed;
                F1 = 0;
                totalWheatFertUsed = totalWheatFertUsed + F1;
                areaOfWheat = areaOfWheat + areaFertUsed;
                amount = amount + areaFertUsed * S1;
            } else if (areaInsecUsed < areaFertUsed) {
                L = L - areaInsecUsed;
                P1 = 0;
                totalWheatInsecUsed = totalWheatInsecUsed + areaInsecUsed;
                areaOfWheat = areaOfWheat + areaInsecUsed;
                amount = amount + areaInsecUsed * S1;
            }
        }

    }
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);
    return df.format(amount) + "," + df.format(areaOfWheat) + ","
            + df.format(areaOfRice);
}
}
Deepak Mann
  • 111
  • 11

3 Answers3

3

If you do not want to make a new instance of your class, then define your method as static, as shown below:

public static String get_total_profit(String input1) {
    /**
    * your code goes here
    */
}
Tarang Bhalodia
  • 1,185
  • 11
  • 21
2

If you must reference the method get_total_profit in a static context and you cannot make the method get_total_profit static, you can declare a reference variable to the class that contains the get_total_profit method from the static context. Replace your line with the code below:

    CandidateCode candidateCode=new CandidateCode();
    output=candidateCode.get_total_profit(ip1);
J. Alfonso
  • 21
  • 2
1

this error occurrs, when you try to use a non-static method out of a static one. I don't know the rest of your code, but it seems that you try to call the (non-static) get_total_profit-operation out of something like your main-method?

there are two possibilities: on the one hand you can change get_total_profit to static (which you probably don't want) or on the other hand you can create a function in the class of ip1 without parameter referring to "this".

Uschi003
  • 11
  • 2