Beginner in Java: need a help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 18
Reputation: Ainur is an unknown quantity at this point 
Solved Threads: 0
Ainur Ainur is offline Offline
Newbie Poster

Beginner in Java: need a help

 
0
  #1
Aug 3rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Beginner in Java: need a help

 
0
  #2
Aug 3rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 18
Reputation: Ainur is an unknown quantity at this point 
Solved Threads: 0
Ainur Ainur is offline Offline
Newbie Poster

Re: Beginner in Java: need a help

 
0
  #3
Aug 3rd, 2007
Hello

I did not ask to write code to me at all. I already wrote mine,but I have doubts about correctness. Thank you
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Beginner in Java: need a help

 
0
  #4
Aug 3rd, 2007
Originally Posted by Ainur View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 18
Reputation: Ainur is an unknown quantity at this point 
Solved Threads: 0
Ainur Ainur is offline Offline
Newbie Poster

Re: Beginner in Java: need a help

 
0
  #5
Aug 3rd, 2007
I am so sorry,but I do not know where is code tags))
Can write my answers just below?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 18
Reputation: Ainur is an unknown quantity at this point 
Solved Threads: 0
Ainur Ainur is offline Offline
Newbie Poster

Re: Beginner in Java: need a help

 
0
  #6
Aug 3rd, 2007
1) "Then answer is: no"
2) public int factorialSum()
{
int factorial =a;
int factorial =b;
factorialSum=a+b;
return;
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Beginner in Java: need a help

 
0
  #7
Aug 3rd, 2007
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!
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 18
Reputation: Ainur is an unknown quantity at this point 
Solved Threads: 0
Ainur Ainur is offline Offline
Newbie Poster

Re: Beginner in Java: need a help

 
0
  #8
Aug 3rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Beginner in Java: need a help

 
0
  #9
Aug 3rd, 2007
Originally Posted by Ainur View Post
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 7
Reputation: eDeloa is an unknown quantity at this point 
Solved Threads: 0
eDeloa eDeloa is offline Offline
Newbie Poster

Re: Beginner in Java: need a help

 
0
  #10
Aug 4th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1642 | Replies: 16
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC