Thank you for any help with following programming questions! 1) This function ca
ID: 3851225 • Letter: T
Question
Thank you for any help with following programming questions!
1) This function calculates what?
long x(int n)
{
if (n == 1) return 1;
else return n*x(n-1);
}
2) What list operation does the operation method below define?
public void operation(int location, int item)
{
if (location < 0 || location >= length)
{
System.out.println("Error: invalid location");
} else
{
list[location] = item;
}
}
3) Consider the recursive function below: What does this code do?
int RecFunction(int n)
{
if (n==0) return 0;
else return RecFunction(n-1) + n;
}
4) Consider the following code that uses the class java.util.LinkedList.
LinkedList constants = new LinkedList();
Which of the following statements correctly inserts PI in the list constants?
Explanation / Answer
Question 1:
Answer: This function return the factorial of the given number positive number;
For example:
x(5) return 120
x(4) return 24
Question 2:
Answer: It place the given item in the given location
if (location < 0 || location >= length) { /// check if location is not negitive and must not be greater then length
System.out.println("Error: invalid location");
} else {
list[location] = item; // place the item in given index location
}
Question 3:
Answer: It return the sum of given number
example:
RecFunction(3) = 3 + 2 + 1 = 6
RecFunction(6) = 6 + 5 + 4 + 3 + 2 + 1 = 21
Question 4:
Answer:
constants.add(Math.PI);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.