Sorry, no one is going to do the homework for you. Post code you are having trouble with or specific questions about what you don't understand if you want any assistance.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Hello
I did not ask to write code to me at all. I already wrote mine,but I have doubts about correctness. Thank you
Then post it within code tags with any questions you have about it.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Never use == to compare anything that's not a primitive unless you are deliberately intending to find out whether 2 references refer to the same physical location in memory).
Use the equals method on an object reference instead.
And always remember that Strings are NOT primitives in Java!
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
1) "Then answer is: no"
Yes, 1 is correct.2) public int factorialSum()
{
int factorial =a;
int factorial =b;
factorialSum=a+b;
return;
}
Is it right? Thank you
Your assignment indicates that you need to calculate the factorial of each number and then add them. You are only adding them. Also, factorialSum is not defined in your method, you are not returning anything, you have not checked to make sure the parameters are valid, and the method does not print the result to the console as requested. You have a start, but more to do here.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Here's a hint for problem #2 ...
The factorial of a number is the product of all integers leading up to, and including, that number. For example: The factorial of 5 is: 1x2x3x4x5. The factorial of 3 is: 1x2x3.
Since the method you're writing takes in two unknown numbers, we need some way to ... listen closely ... loop through each integer of that number.
Each time we work with a new number, we need to add that to some variable that holds our total. You guessed it ... we need to declare a variable before we start looping though.
Good luck, let us know how you do.
Paragraph 1, 2, 5 is OK
Paragraph 3, 4 is useless, you lost your self in simple explanationThe method should calculate the factorial of both these numbers,then add them together.
Pseude code
int factorialA = a; // calculate factorial of a
int factorialB = b; // calculate factorial of b
int sum = factorialA + factorialB; // add them together
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
What I provided is pseudo code not working Java code, that why I made comment "calculate factorial". There is no reason to go into details how to calculate something so trivial.It was for poster to solve the issue. We are not here to generate code on request.
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
Write a separate helper function 'factorial()' which would calculate factorial of any number passed to it. Call that function from the 'factorialSum()' function.
private long factorial(int num)
{
//code here
return(result); //possibility of overflow
}
public long factorialSum(int a, int b)
{
//code to check the validity of inputs
long first = factorial(a);
long second = factorial(b);
return(first + second); //possibility of overflow
}
You have got to realize here that factorials grow exponentially and quickly get out of hand so using finite data types like long or int won't serve you any good. But I am pretty sure that is not what the exercise stresses on, so for the time being just concentrate on getting the logic and syntax correct.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Read the previous post. It answers all your questions. The only thing you need to do is write the factorial function.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734