Question

In: Computer Science

2. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string....

2. Create ACCESSOR FUNCTIONS in JAVA
a) String moneyToString(int[] money); // Returns a nice looking string. Ex, "$6.25", "$0.21", "$4.01", "$2.00". MAKE SURE TO CONSIDER ALL EXAMPLES!
b) *String moneyToText(int[] money); // Returns the Money as words. Ex,{123,51} => "one hundred and twenty three dollars and fifty one cents." YOU MAY ASSUME money <$1000.

context:

this is what I have so far

package com.company;
import java.util.*;

public class Main
{
    public static void main(String[]args)
    {
        int money[] = createMoney(12, 34);
        int copy[] = copyMoney(money);
    }

    static int[] createMoney(int dollars, int cents)
    {
        if (cents>99)
        {
            int extraDollars=cents/100;     
            cents=cents%100;
            dollars+=extraDollars;
        }
        int money[] = { dollars, cents };
        return money;
    }
    static int[] copyMoney (int[]money)
    {
        int copy[]=new int[money.length];
        for (int i=0; i<money.length; i++)
        {
            copy[i]=money[i];
        }
        return copy;
    }
}

Solutions

Expert Solution

2.

a)

CODE

public static String moneyToString(int[] money) {

String result = "$";

if (money.length == 1) {

result += money[0];

} else if (money.length == 2) {

result += money[0];

if (money[1] != 0) {

result += "." + money[1];

}

} else {

result = "";

}

return result;

}

B)

CODE

public static String convert(final int n) {

String[] units = { "", "One", "Two", "Three", "Four",

"Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",

"Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",

"Eighteen", "Nineteen" };

String[] tens = {

"", // 0

"", // 1

"Twenty", // 2

"Thirty", // 3

"Forty", // 4

"Fifty", // 5

"Sixty", // 6

"Seventy", // 7

"Eighty", // 8

"Ninety" // 9

};

if (n < 20) {

return units[n];

}

if (n < 100) {

return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];

}

return units[n / 100] + " Hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);

}

public static String moneyToText(int[] money) {

String result = "";

if (money.length == 2) {

result += convert(money[0]) + " dollars and " + convert(money[1]) + " cents.";

} else if (money.length == 1) {

result += convert(money[0]) + " dollars.";

}

return result;

}


Related Solutions

In Visual Basics 6.Two String returning functions to have nice looking output for numbers/money are? 7.Where...
In Visual Basics 6.Two String returning functions to have nice looking output for numbers/money are? 7.Where do we place a class-level variable declaration, and where is it accessible from? 8.Two forms of parameter passing: 9.FULLY define an array: 10.Show the VB code to declare an initialize an array called Ar1 of Integers to hold the value of 5, 10 and 15. 11. For Ar1above, Ar1.Count = ________ and legal index values are:_____ to ______. 12.Show the VB code to declare...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
JAVA /**    * dataAtPosition returns the int data at the described position.    * if...
JAVA /**    * dataAtPosition returns the int data at the described position.    * if the position is an invalid position it throws an Exception.    *    * Examples:    * pos : 0 and LinkedList : 2 --> 3 --> null ==> return 2    * pos : 1 and LinkedList : 1 --> -3 --> null ==> return -3    * pos : 2 and LinkedList : -2 --> 3 --> -2 --> null ==> return...
CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create...
CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create your own user defined exception, COMMENT it well, and add code so that it is thrown.
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
Java It would be nice if you use many Functions. rite a method, besides main(), which...
Java It would be nice if you use many Functions. rite a method, besides main(), which will calculate the lowest common multiple (LCM) of two numbers. For example, the multiples of 4 are 4, 8, 12, 16, 20, 24 …, and the multiples of 6 are 6, 12, 18, 24 …. The LCM is 12. Do NOT use any code from the Internet! Here are some more examples: 3:5 LCM is 15 4:8 LCM is 8 5:7 LCM is 35...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int x, int y) { x = 1; y = 3; } For each of the following parameter-passing methods, saw what the final values in the array A would be, after the call to f. (There may be more than one correct answer.) a. By value. b. By reference. c. By value-result.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT