Question

In: Computer Science

In Java, Programmers do all money calculations using cents, represented as an integer, like 635 cents....

In Java,

Programmers do all money calculations using cents, represented as an integer, like 635 cents. A useful method in such an approach might convert cents into separate dollar and cents values. Write a method centsToDollarsCents whose parameter is given cents and returns an array of integers containing numDollars and numCents, respectively. If the first argument is 635, the array would contain [6, 35].

I understand how to solve for numDollars and numCents but I am having trouble returning the array. If anyone could help that would be appreciated.

Solutions

Expert Solution

Please find the below code

import java.util.Arrays;
import java.util.Scanner;

public class Sample {

public static int[] centsToDollarsCents(int numberInCents) {
int[] result = new int[2];
int numDollars = 0, numCents = 0;

// 100 cents = 1 dollar
if(numberInCents < 100) {
numCents = numberInCents;
result[0] = numDollars;
result[1] = numCents;
}
else {
numDollars = numberInCents / 100; // To get the dollar value
numCents = numberInCents % 100; // To get the cent value
result[0] = numDollars;
result[1] = numCents;
}
return result;
}

public static void main(String[] args)
{
System.out.println("Enter the number in cents:");
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int[] res = centsToDollarsCents(number);
System.out.println("Result: "+ Arrays.toString(res));
}

}

Please find the attached image as an output


Related Solutions

Assessment calculations. Complete the calculations using the following patient information for all calculations: 64 y.o., male,...
Assessment calculations. Complete the calculations using the following patient information for all calculations: 64 y.o., male, 6’3”, 254 lbs. Place your final answer in the blank spaces provided to the left of each question. (10 x 2 points each = 20 points) SHOW DETAILED WORK (in the space on the right)! __________      Determine his IBW assuming a medium frame. __________      Determine his % IBW. __________      If his usual BW is 286 lbs, what is his % UBW? __________     What...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
Make Excel do all calculations, using cell addresses. Don’t type numbers in your formulas. Report the...
Make Excel do all calculations, using cell addresses. Don’t type numbers in your formulas. Report the answers to the questions in your worksheet using the appropriate symbols (µ σ) and notation p(x>=5), p(X<3) etc...             Use Insert/Symbol to find mu and sigma for mean and std dev.                                     x P(x) 1. An auditor for Health Maintenance Services of Georgia reports 30 percent of policyholders 55 years or older submit a claim during the year. Twelve policyholders are randomly selected for...
(c) Explain TWO (2) differences between int and Integer in the context of Java programming using...
(c) Explain TWO (2) differences between int and Integer in the context of Java programming using suitable example,
using python without external libaries Using integer arithmetic operators '+' and '-', print all combinations that...
using python without external libaries Using integer arithmetic operators '+' and '-', print all combinations that sum up to 'sum' by inserting the operators between digits in 'number'. example for 'number=123456789' and 'sum = 0' Print the output using the terminal: Output should be exactly like this from 1 - 22 1 : +1+2-34-56+78+9=0 2 : +1-2-34+5+6+7+8+9=0 3 : +1-23-4-56-7+89=0 ... 12 : -1+2+34-5-6-7-8-9=0 13 : -1+23+4+56+7-89=0 14 : -1-2+34+56-78-9=0 ... 22 : -12-34+56+7-8-9=0
Credit cards are not considered as money even though people have been using them like money...
Credit cards are not considered as money even though people have been using them like money for many years. Explain. What is/are the difference(s) between a debit card and a credit card. Give relevant example(s) in Hong Kong. If you pay in virtual currencies such as bitcoin, do you think it is regarded as money according to traditional definitions?
Using Miller-Rabin primality test algorithm to write a Java program which can test if an integer...
Using Miller-Rabin primality test algorithm to write a Java program which can test if an integer is a prime. The input of the algorithm is a large positive integer. The output is “the number *** is a prime” or “the number *** is not a prime”. The error probability of the algorithm should be no more than 1 256 . Use this program to test some big integers. In Java, there is a class BigInteger. You can use methods of...
Answer the questions using the following information. Do calculations on back or a separate sheet of...
Answer the questions using the following information. Do calculations on back or a separate sheet of paper. Place only your final answer in boxes provided. No partial credit will be given. Round answers to nearest whole number for Q5-7. Q1-4 = 1 pt. each. Q5-7 = 2 pts. each. Daily sales = 325 units                                                 Cost of capital = 22.5% Std. deviation of daily demand = 39                           Inventory Risk = 1.5% Cost to place an order = $30                                      Storage...
Please do in Java!! Stay on the Screen! Animation in video games is just like animation...
Please do in Java!! Stay on the Screen! Animation in video games is just like animation in movies – it’s drawn image by image (called “frames”). Before the game can draw a frame, it needs to update the position of the objects based on their velocities (among other things). To do that is relatively simple: add the velocity to the position of the object each frame. For this program, imagine we want to track an object and detect if it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT