Hi,
I need help with some small exercises. I'll try my best to provide my code but these are ones that I don't know how. So... maybe you can help me with yours! Thanks

1. Question 1:

A program contains the following method definition:

``````public static int cube(int num)
{
return num*num*num;
}``````

write one statement that passes the value 4 to the method cube and assigns its return value to a variable named result.

Below is my code:

``````int number= 4;
cube(number);
int result=number;``````

## All 12 Replies

You are passing the value 4 OK, but that's not the right way to get the returned value. Check out your course notes for an example of calling a method that has a return value.

you are not assigning the return value, you are assigning the number

``````int number=4;
int result=cube(number);``````

you are not assigning the return value, you are assigning the number

``````int number=4;
int result=cube(number);``````

I don't understand this statement, may you explain?

``int result=cube(number);``

Hi forhacksf. Please be careful to help tracydo without actually doing her(?) homework for her - no complete solutions.

Thank you James,I guess... you have the point, but sometime for just 1 line of code could make a tremendous help with some explanation. But a non specific suggestion could make me eventually abandon my post and leaving the forum still not knowing anything. This is just 1 of many small exers that I need help with and offcourse it's all up to you giving me an effective helping or not.

Do you have any link to post here which provides exercises (include problem solving)for computer science 1? Thanks

Hi tracydo
Those of us who post here regularly always have great difficulty knowing how far to go in helping with homework questions. If you give someone a complete solution they will probably just copy/paste it and learn nothing (not to mention cheat). So we try to give enough info for the student to do a bit of research and find the answer for themselves, learning all the way. We do that through hints, keywords for Google searches, web references etc.
Sometimes we make it a bit too cryptic, in which case please don't be worried about asking for clarification. Our overall goal is to help people learn Java, as opposed to just helping them answer an exam question,
:-)
J

Totally understand James!
I'm currently working on this text book: http://www.amazon.com/gp/product/0321421027/ref=pd_lpo_k2_dp_sr_2?pf_rd_p=486539851&pf_rd_s=lpo-top-stripe-1&pf_rd_t=201&pf_rd_i=0321497686&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=15102XDV4PB1JW4NCX00 trying to do some exers but got hang up once in while with the answer cause this book isn't provided.
Is there any digital exers with problems solving explanation you've came across and wanted to share?

Speaking of learning java:

Methods either return a value, or they don't. A method whose return type is "void" doesn't return a value, a method with any other return type does return a value, and the value will be of that type.

``public int foo() {}``

must return an int - I don't know what int it must return, because "foo" is not a very informative name, but it must return an int.

In this case, because foo() is a pretty useless method, it just returns 4 in all cases:

``public int foo() { return 4;}``

If a method returns a value, a call to that method evaluates to that value. That is, a call to `foo()` can go anywhere an int can go**, and it will be evaluated at run-time and the int value will turn up there.

So if we know that "foo()" just returns the value 4, the line

``System.out.println("foo squared is "+ (foo()*foo()));``

is the same as

``````int f = 4;
System.out.println("foo squared is "+ (4 * 4));``````

or

``````int f = 4;
System.out.println("foo squared is "+ (4 * foo()));``````

Now, from all of that it should not surprise you that you can assign the return value of a function into a variable of the correct type, just as you would assign a variable or a literal. And this is how we get the assignment statement that forhacksf came up with.

[EDIT: ** you could say that there's an exception to this statement, depending on how you read it. Can you find the exception?]

Is there any digital exers with problems solving explanation you've came across and wanted to share?

I'm not the best person to ask about that, sorry. When I learned Java the only books were hand-printed on parchment scrolls, and I think they are no longer available except in museums!
I'm sure that someone else here can help with this very reasonable question. (Come on guys!)

Hi Jon.Kiparsky,
I like how you explained "Methods either return a value, or they don't. A method whose return type is "void" doesn't return a value, a method with any other return type does return a value, and the value will be of that type." It was very clear to me that you took time out to break it into steps for easy understanding. I appreciated!

That's OK James, I can just check this post as solved and if someone would like to post any helpful exers link (remember that have to be with answer and explanations) would greatly appreciate! I know there are some sites out there are paid for answering textbooks but I don't think they are legitimate sites and maybe even waste time and effort.

I'm not the best person to ask about that, sorry. When I learned Java the only books were hand-printed on parchment scrolls, and I think they are no longer available except in museums!
I'm sure that someone else here can help with this very reasonable question. (Come on guys!)

Hi Jon.Kiparsky, Thank you for your explanation!
I like how you stated "Methods either return a value, or they don't. A method whose return type is "void" doesn't return a value, a method with any other return type does return a value, and the value will be of that type. " It was clear to me that you took time out and break them into steps for easy understanding. I appreciated!

That's ok Jame, for now I just check this post as solved and if anyone has any link for exercises (remember that include the answers & explanation) to post here would be greatly appreciate!
I know there are some paid sites out there offering answers to texts for a fee but I personally think they are not legitimate sites and maybe even waste time and effort.

I just want to add that with the assignment operator ( `=` ), the right side of the expression is evaluated and then that value is assigned to the left side. Let's look at the code that forhacksf provided:

``````int number=4;
int result=cube(number);``````

The first line could be split into two separate lines like this:

``````int number;
number = 4;``````

`int number;` creates the reference variable `number` of type `int` and since it hasn't yet been assigned an integer value, Java defaults the value to `0` . With the statement `number = 4;` , the right side is evaluated; `4` evaluates to `4` . The integer value `4` is then assigned to `number` replacing the `0` .

The second line of forhacksf's code could also be broken down like this:

``````int result;
result = cube( number );``````

Just like before, a reference variable, `result` , is created of type `int` and defaulted to `0` . The right side of the second line is then evaluated. Since `number` evaluates to `4` , passing `number` into `cube()` is the same as passing `4` into `cube()` . Passing `4` into `cube()` returns the value `64` (because it multiplies `4*4*4` ). `64` is then assigned to the variable `result` replacing the `0` .

This explanation turned out to be a little longer than I originally intended but I hope it helps you understand the way the code forhacksf gave you is executed. Good luck with the rest of your exercises!

Be a part of the DaniWeb community

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