Need help with Small Coded Excercises
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;
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 27
Solved Threads: 0
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.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
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);
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 27
Solved Threads: 0
Hi forhacksf. Please be careful to help tracydo without actually doing her(?) homework for her - no complete solutions.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
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
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 27
Solved Threads: 0
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
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 27
Solved Threads: 0
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?]
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
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!)
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
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.
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 27
Solved Threads: 0
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.
tracydo
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 27
Solved Threads: 0