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;
}

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.

Hello

I did not ask to write code to me at all. I already wrote mine,but I have doubts about correctness. Thank you

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.

I am so sorry,but I do not know where is code tags:)))
Can write my answers just below?:)

1) "Then answer is: no"
2) public int factorialSum()
{
int factorial =a;
int factorial =b;
factorialSum=a+b;
return;
}

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!

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

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.

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.

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 explanation
The 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

you lost your self in simple explanation
The 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

What are you talking about? How does:

int factorialA = a; or int factorialB = b;

possibly calculate the numbers' factorials? Java has no built-in factorial method ... we need to find it ourselves.

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.

Dear all

Thank you so much for helping me. I read all your hints and remade my code. Could you check it,please? Thank you!

Can I giva a value for the a & b? It will be easier to me.

int a=4;
int b=2;

public int factorialSum(new factorialSum)
{
for (int a =4;a>=1;a++)
for(int b=2;b>=1;b++)
factorialSum=a+b;

return factorialSum;
}

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.

dear all

again my vesrion about question #2 is

public int factorialSum(int sum)
{
int factorailA=a;
int factorialB=b;
int sum=factorialA+factorialB;


if (a<1&b<1)
System.out.println("Error!");
else
System.out.println(int sum);
}

Please evalute it. Thank you!

Read the previous post. It answers all your questions. The only thing you need to do is write the factorial function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.