FinishLine
FinishLine

Reputation: 27

What is the correct way to use return?

I know the program is sloppy atm but I'm not quite sure on if I'm using return correctly. I'm trying to find a way to print the square/cube/hypercube of the length in the main method, but the variables don't 'carry over' to output().

    public class Power2
{
    public static void main(String[] args)
    {
        Power2 p = new Power2();

        int length = 6;

        p.square( length );
        p.cube( length );
        p.hypercube( length );

        output();


    }

    public int square( int length )
    {
        int sSquare =  (length*length) ;
        return sSquare;
    }

    public int cube( int length )
    {
        int sCube = (length*length*length);
        return sCube;
    }

     public int hypercube( int length )
     {
       int sHypercube = (length*length*length*length);
       return sHypercube;
     }


    public static void output()
    {
        System.out.println(sSquare);
        System.out.println(sCube);
        System.out.println(sHypercube);
    }
}

Upvotes: 0

Views: 96

Answers (5)

mvieghofer
mvieghofer

Reputation: 2896

You have two options.

The first one is to declare your sHypercube, etc. variables as fields. This means that you need to add a

private int sHypercube;

to your class. Then you can use the code of the output method as you already wrote it.

The second option is that you leave all your method for calculations as they are and save the returned values in variables in your output method. I.e. something like this:

public static void output(int length)
{
    int sHypercube = hypercube(length);
    System.out.println(sHypercube);
}

If you need more information about the scope of a variable google it or take a look at this post (this was the first thing I found on google)

Upvotes: 1

Adam Arold
Adam Arold

Reputation: 30578

First you need to assign the return values:

int squareResult = p.square( length );
int cubeResult = p.cube( length );
int hypercubeResult = p.hypercube( length );

then you need to pass them as a parameter:

output(squareResult, cubeResult, hypercubeResult);

and your output method should look like something like this:

public static void output(int squareResult, int cubeResult, int hypercubeResult) {
    // ...
}

Some may tell you to use fields but resist the temptation to do so. Introducing state to utility functions is asking for trouble.

Upvotes: 1

Le Duy Khanh
Le Duy Khanh

Reputation: 1369

should be like this

    int a= p.square( length );
    int b =p.cube( length );
    int c = p.hypercube( length );

    output(a,b,c);

and

public static void output(int a,int b,int c) { System.out.println(a,b,c);

}

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 122026

They are returning. But you are not receiving them.

Just for clarification assign them to variables like,

int i = p.square( length );
int j = p.cube( length );
int k = p.hypercube( length );

Now you can print i ,j , k

or

directly print them like

System.out.println(p.square( length ));

like wise remaining.

Upvotes: 3

Rahul
Rahul

Reputation: 45090

You need to pass those values to the output method so that it can be used there. Something like this

// In main - values returned by the methods is sent to the output method
output(p.square( length ), p.cube( length ), p.hypercube( length )); // calling output method with the values to be printed

// Output method - signature changed to accept 2 input parameters
public static void output(int sSquare, int sCube, int sHypercube) { 

Upvotes: 1

Related Questions