Ask Question
12 October, 20:48

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.

+3
Answers (1)
  1. 12 October, 21:26
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers