Question

Write a public static method name productAll that takes in 2 arguments int a, int b,...

  1. Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative.

Example: productAll(2, 5); int returned by method: 120

Example: productAll(5, 2); int returned by method: 120

Example: productAll(3, 3); int returned by method: 9

Example: productAll(3, 5); int returned by method: 60

Example: productAll(-4, 3); int returned by method: 0

Example: productAll(-4, -2); int returned by method: -24

Example: productAll(-5, -2); int returned by method: 120

Example: productAll(-5, 2); int returned by method: 0

Example: productAll(-5, -3); int returned by method: -60

this is Java

Homework Answers

Answer #1
//TestCode.java
public class TestCode {
    public static int productAll(int a, int b){
        int result = 1;
        for(int i = a;i<=b;i++){
            result = result * i;
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(productAll(2,5));
    }
}

120

public static int productAll(int a, int b){
    int result = 1;
    for(int i = a;i<=b;i++){
        result = result * i;
    }
    return result;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a public static method name productAll that takes in 2 arguments int a, int b,...
Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative. Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5);...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
Write a method with the following header: public static int getValidInput(int low, int high, String message)...
Write a method with the following header: public static int getValidInput(int low, int high, String message) This method will return a user entered number between high and low inclusive. If a number is entered that is not between high and low, string message will be printed to the screen. If high is less than low, a -1 is returned. For example, given this call: int input = getValidInput(0, 100, “Not a valid grade, Please re- enter”); The following could occur:...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) →...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) → 2 maxOfTwo(2, 1) → 2 maxOfTwo(1, 1) → 1 **(to start use): public int maxOfTwo(int a, int b) { And then complete the function (java static method)MaxOfThree so that it returns the maximum (highest) of the three int values passed as the parameters. maxOfThree(1, 2, 3) → 3 maxOfThree(0, 2, 1) → 2 maxOfThree(88, 4, 5) → 88 **(to start use): public int maxOfThree(int...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)          ...
Consider the following two methods: public static boolean isTrue(int n){        if(n%2 == 0)           return true;        else           return false; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length)        return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 2, 3, 3,...
public class Mystery { public static String mystery(String str, int input) { String result = "";...
public class Mystery { public static String mystery(String str, int input) { String result = ""; for (int i = 0; i < str.length() - 1; i++) { if (input == 0) { str = ""; result = str; } if (input == -2) { result = str.substring(2, 4); } if (input == 1) { result = str.substring(0, 1); } if (input == 2) { result = str.substring(0, 2); } if (input == 3) { result = str.substring(2, 3); }...
Given the recursive method definition:    public static int recurMethod (int num) {         if (num...
Given the recursive method definition:    public static int recurMethod (int num) {         if (num > 10)             return num;         else             return num + recurMethod(num / 4);     } And the initial call: int answer = recurMethod(320); list the values that will be returned from each call to the previous call of recursMethod. Start with the last recursive call, and work backwards, with one space between each returned value, ending with the value returned to the original...
1.If you have defined a class,  SavingsAccount, with a public  static method,  getNumberOfAccounts, and created a  SavingsAccount object referenced by...
1.If you have defined a class,  SavingsAccount, with a public  static method,  getNumberOfAccounts, and created a  SavingsAccount object referenced by the variable  account20, which of the following will call the  getNumberOfAccounts method? a. account20.getNumberOfAccounts(); b. SavingsAccount.account20.getNumberOfAccounts(); c. SavingsAccount.getNumberOfAccounts(); d. getNumberOfAccounts(); e. a & c f. a & b 2.In the following class, which variables can the method printStats use? (Mark all that apply.) public class Item { public static int amount = 0; private int quantity; private String name; public Item(String inName, int inQty) { name...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT