Jeremy B
Jeremy B

Reputation: 863

using static method

I'm unsure how to use the static method in this program. I am coding a program that will calculate a factorial of a number between 0 and 10. The user can enter the number and the program should calculate the factorial. I had originally written a functional program with all of the code coming from the main and then when I double checked the assignment rubric I noticed I was supposed to place the calculation for getting the factorial in a static method. I believe my issue is towards the bottom where I'm asking the user to enter the number I don't send it to the calculator. I guess I'm unclear on how that is done. I'm new so I apologize for my poor coding and I appreciate any help.

Here is my code:

import java.util.Scanner; //import scanner class
public class FactorialCalculator {

    public static long calculator(long fact, int num) {
        for(int i = 1; i<=num; i++) {
            fact *= i;
        }
        return fact;
    }

    public static void main(String[] args) {
        Scanner calc = new Scanner(System.in); //create new scanner calc
        int num = 0;
        long fact = 1;
        //welcome user to the Factorial Calculator
        System.out.println("Welcome to the Factorial Calculator.");
        System.out.println();
        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {
            System.out.println("Please enter an integer that's greater than 0 and less than 10: ");
            num = calc.nextInt();

            System.out.println("The Factorial for " + num + " is " + fact +".");
            System.out.println();
            System.out.println("Would you like to continue? y/n");
            choice = calc.next();
            System.out.println();
        }
    }
}

Upvotes: 3

Views: 1883

Answers (6)

Hawken
Hawken

Reputation: 2119

You're never calling calculator(long fact, int num);

just call it with

    fact = calculator(fact,num);

right after:

    num = calc.nextInt();

EDIT:

I would recommend you also remove the long fact as an argument to calculator() and add it into the method body of calculator()

Ex:

public static long calculator(int num){
   long fact=1;
   for(int i = 1; i<=num; i++){
      fact *= i;
   }
   return fact;
}

Upvotes: 0

RanRag
RanRag

Reputation: 49597

As your calculator method is a static method so you can call it directly by using the class name.

num = calc.nextInt();
long result_factorial = FactorialCalculator.calcuator(fact,num)

Your method should be

public static long (int num)
        {
              long fact = 1;
                for(int i = 1; i<=num; i++)
                {
                fact *= i;
                }
            return fact;
        }

Remove the fact initialisation from your main method.

Upvotes: 0

Bob Wang
Bob Wang

Reputation: 606

Start with this:

fact = FactorialCalculator.calculator(fact, num)

And you should notice that the program will calculate factorial right for the first time, then it starts to fail since the second time. Fixing that bug is your job.

Upvotes: 0

cb1
cb1

Reputation: 940

import java.util.Scanner; //import scanner class
public class FactorialCalculator 
{

    public static long calculator(int num)
    {
            long fact = 1;

            for(int i = 1; i<=num; i++)
            {
                fact *= i;
            }
        return fact;
    }

    public static void main(String[] args)
    {
        Scanner calc = new Scanner(System.in); //create new scanner calc
        int num;
        //welcome user to the Factorial Calculator
        System.out.println("Welcome to the Factorial Calculator.");
        System.out.println();
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
            {
            System.out.println("Please enter an integer that's greater than 0 and less than 10: ");
            num = calc.nextInt();

            fact = FactorialCalculator.calculator(num);

            System.out.println("The Factorial for " + num + " is " + fact +".");

            System.out.println();
            System.out.println("Would you like to continue? y/n");
            choice = calc.next();
            System.out.println();
            }
    }
}

Upvotes: 0

user1188835
user1188835

Reputation:

You can call a static method of a class as:

variableType variableName = ClassName.MethodName(parameters);

In your case you can simply call:

        fact = FactorialCalculator.calculator(fact, num);

It should be inserted somewhere AFTER when you ASK the user for the input num, and BEFORE you print out the fact number to the user

Upvotes: 0

Borealid
Borealid

Reputation: 98559

Place this just before you print the result:

fact = FactorialCalculator.calculator(fact, num)

Upvotes: 1

Related Questions