944,020 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1886
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 3rd, 2007
0

Beginner in Java: need a help

Expand Post »
Hello everybody! I am a beginner in Java and computing at all. I need to make some tasks if you help me,please.

So, the first taks is:
Write down what is stored in s2 after the following Java code has been executed:

String s1= new String("Hello");
String s2= new String("The answer is: ");

if (s2=="Hello") s2 = s2+"yes"; else s2=s2+"no";

Second is
write a java method called factorialSum,that takes two parameters called a and b. The method should calculate the factorial of both these numbers,then add them together. Method should return the result of calculation from the method as an integer, and aslo the print result to the console. It should also print an error message if any of the given parametrs are less than 1.


Thank you! I appreciate your help


I'm sorry,I forgot to add my code:
1) "Then answer is: no"
2) public int factorialSum()
{
int factorial =a;
int factorial =b;
factorialSum=a+b;
return;
}
Last edited by Ainur; Aug 3rd, 2007 at 2:29 pm. Reason: adding my code
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ainur is offline Offline
18 posts
since Aug 2007
Aug 3rd, 2007
0

Re: Beginner in Java: need a help

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 3rd, 2007
0

Re: Beginner in Java: need a help

Hello

I did not ask to write code to me at all. I already wrote mine,but I have doubts about correctness. Thank you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ainur is offline Offline
18 posts
since Aug 2007
Aug 3rd, 2007
0

Re: Beginner in Java: need a help

Click to Expand / Collapse  Quote originally posted by Ainur ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 3rd, 2007
0

Re: Beginner in Java: need a help

I am so sorry,but I do not know where is code tags))
Can write my answers just below?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ainur is offline Offline
18 posts
since Aug 2007
Aug 3rd, 2007
0

Re: Beginner in Java: need a help

1) "Then answer is: no"
2) public int factorialSum()
{
int factorial =a;
int factorial =b;
factorialSum=a+b;
return;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ainur is offline Offline
18 posts
since Aug 2007
Aug 3rd, 2007
0

Re: Beginner in Java: need a help

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!
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 3rd, 2007
0

Re: Beginner in Java: need a help

Dear jwenting

I also read about equality in the books,but it was task. My answer is
1) "Then answer is: no"
2) public int factorialSum()
{
int factorial =a;
int factorial =b;
factorialSum=a+b;
return;
}
Is it right? Thank you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ainur is offline Offline
18 posts
since Aug 2007
Aug 3rd, 2007
0

Re: Beginner in Java: need a help

Click to Expand / Collapse  Quote originally posted by Ainur ...
1) "Then answer is: no"
Yes, 1 is correct.

Quote ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 4th, 2007
0

Re: Beginner in Java: need a help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eDeloa is offline Offline
7 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java or mysql
Next Thread in Java Forum Timeline: From beginner in Java: Atomic type-what is it?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC