An assignment I'm working on requires me to write a method for the sole purpose of calling that method in a later method. As such I am required to write the first method to not display anything on screen and then I need to comment the calculation within the method. This is what I have so far.

public double calcProfit ()
    {
//         double totalProfit = sellingPrice - purchasePrice;
    }
public double calcTotalProfit ()
    {
        double totalProfit = calcProfit () * quantitySold;
        return totalProfit;
    }

I've tried writing calcProfit () as a void method, but you can't call a void method. But I don't know what to put for the return value. Any help would be appreciated.

return null;

You can't return a null from a method that's declared as returning a primitive.
If you want a return that clearly signals the fact that the method isn't complete yet, then there is a special double value called NaN ("Not a Number") that you can access via the Double class, so you can code

return return Double.NaN;

and that is valid code for a method that returns double, while making it very obvious that it's not the real working version of the method.

So if I'm understanding this right, my code should look like:

public double calcProfit ()
    {
//         double totalProfit = sellingPrice - purchasePrice;
        return Double.NaN;
    }
public double calcTotalProfit ()
    {
        double totalProfit = calcProfit () * quantitySold;
        return totalProfit;
    }

However, now when I try to use my calcTotalProfit () method, I get a NaN result. I mean it makes sense that if the only part of the calcProfit () method that is visible to Java is the NaN part, then trying to call this method in another would result in a NaN result. If I may, I'm going to post the text from the assignment, maybe I'm misunderstanding it.

"Add a method called calcProfit() that calculates and returns the profit
from selling one instrument, based on the purchase price and selling price.
This method will not display anything on the screen. Comment the calculation."

"Add a method called calcTotalProfit() that calculates and returns the
profit from all the instruments that have been sold. This method must invoke
calcProfit()."

Is there something I'm missing?

Your code is doing what you would expect; since calcProfit doesn't actually calculate anything it returns an error value (NaN). Then any other calculation that tries to use that result also returns the error value. That's right.

When your teacher says "Comment the calculation." Maybe that means that you should code the calculation, and add a comment that explains it, not code the calculation and turn that into a comment?

Your interpretation of the assignment makes no sense.

calcProfit() calculates and returns the profit

You display on the screen by call the print or println method.

and what do you mean, you can't call a void method?
either you make it a void method and have the variable whose value you are setting on either class or instance scope, or make it a method that actually returns the result of your calculation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.