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 Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
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
Create a calculator in java without using the math library which converts an integer into a...
Create a calculator in java without using the math library which converts an integer into a hexadecimal number.
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...
Please write in java: Write a recursive method toNumber that forms the integer sum of all...
Please write in java: Write a recursive method toNumber that forms the integer sum of all digit characters in a string. For example, the result of toNumber("3ac4") would be 7. Hint: If next is a digit character ('0' through '9'), Character.isDigit(next) is true and the numeric value of next is Character. digit(next, 10).
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
*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.
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 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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT