| | |
Beginner in Java: need a help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2007
Posts: 18
Reputation:
Solved Threads: 0
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;
}
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
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!
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.
Yes, 1 is correct.
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.
•
•
•
•
2) public int factorialSum()
{
int factorial =a;
int factorial =b;
factorialSum=a+b;
return;
}
Is it right? Thank you
•
•
Join Date: May 2007
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Exception in thread "main" java.lang.NoClassDefFoundError: Invaders Error (Java)
- full screen applet in java (Java)
- java.lang.NoSuchMethodError (Java)
- Beginner Outputing to a file (Java)
- Any Java Source for 16 Questions (Java)
- need help to create a java program (Java)
- Credit card payments assignment (Java)
Other Threads in the Java Forum
- Previous Thread: java or mysql
- Next Thread: From beginner in Java: Atomic type-what is it?
Views: 1642 | Replies: 16
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle os pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows







))