Ask Question
20 April, 14:38

6.12.1: Return number of pennies in total. Write a function NumberOfPennies () that returns the total number of pennies given a number of dollars and (optionally) a number of pennies. Ex: 5 dollars and 6 pennies returns 506.

+4
Answers (1)
  1. 20 April, 15:28
    0
    public class Pennies

    {

    public static void main (String[] args) {

    System. out. println ("Total pennies: " + NumberOfPennies (5, 6));

    System. out. println ("Total pennies: " + NumberOfPennies (5));

    }

    public static int NumberOfPennies (int amount, int pennies) {

    int total = (amount * 100) + pennies;

    return total;

    }

    public static int NumberOfPennies (int amount) {

    NumberOfPennies (amount, 0);

    int total = amount * 100;

    return total;

    }

    }

    Explanation:

    - Create a function called NumberOfPennies that takes two parameters, amount and pennies

    - In order to calculate the number of pennies, multiply the given amount by 100 and add the pennies. Set this result to the total and return the total.

    - Since pennies parameter should be optional, overload the function NumberOfPennies so that it can take one parameter, amount. When it takes one parameter, the value of the pennies set to 0.

    Inside the main, call the function with two possible scenarios
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “6.12.1: Return number of pennies in total. Write a function NumberOfPennies () that returns the total number of pennies given a number of ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers