Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

/* * Author : * Purpose: This program calculates the factorial of an input number. (Factorial.java) */ import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner input=new Scanner(System.in); int number; Factorial factObj=new Factorial(); //1. Input a positive integer number do { System.out.print("Enter a positive integer number: "); number=input.nextInt(); if (number<0) System.out.println("Error: Invalid number."); }while (number<0);

OpenStudy (anonymous):

//2. Calculate and print the factorial of the previous input number System.out.println(number + "! = " + factObj.findFactorial(number)); }//end of main public int findFactorial(int number) {//Purpose: this method uses recursion to compute the factorial of a positive integer number. if (number<=1) return 1; else return number*findFactorial(number-1); }//end of findFactorial method }//end of Factorial class

OpenStudy (anonymous):

a- Explain briefly why it is illegal, in the example above, to call the findFactorial method inside the main method without creating an object of the class Factorial? How can you avoid this step? b- How many times the findFactorial method will be called if the user input number was 4? And what is the return value of each time it is invoked?

OpenStudy (konradzuse):

Well a. You cannot call a class within a class(at least I haven't tried it nor do I think it's a good idea) YOu would avoid it by creating the main class public void main(string[] args) { factorial f = new factorial(); } I would assume 4 since that's what you input right? It goes 0 1 1 2 or 1 1 2 3..... Soo depends where you start, didn't check over all fo the code.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!