Ask 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 dollars and (optionally) a number of pennies. Ex: 5 dollars and 6 pennies returns 506.

+5
Answers (1)
  1. 10 June, 07:54
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers