Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create a program in Kotlin (or swift) with separate Value Returning Functions (dollars(), quarters(),dimes(),nickels(),pennies()) that counts how many dollars, quarters, dimes, nickels, and pennies are in my total. I have created the code, but I need to separate them with different functions. I am not able to get it to work with separate functions.

here is my code so far:

fun main(args: Array<String>) {
var remChange = .03 ;
monies(remChange);
}

fun monies(remChange: Double){
    
var dollars = Math.floor(remChange/1.0);
var remChange1 = remChange-(dollars*1.00);
    print(Math.round(dollars*1)/1)
        if(dollars<2) println("_Dollar"); else if (dollars>=2) println("_Dollars")
   


var quarter = Math.floor(remChange1/0.25);
var remChange2 = remChange1-(quarter*0.25);
    print(Math.round(quarter*4)/4)
        if(quarter<2) println("_Quarter"); else if (quarter>=2) println("_Quarters")
 

var dime = Math.floor(remChange2 / 0.10);
var remChange3 = remChange2 - (dime*0.10);
    print(Math.round(dime*10)/10)
        if(dime<2) println("_Dime") else if(dime>=2) println("_Dimes")
  

var nickel = Math.floor(remChange3 / 0.05);
var remChange4 = remChange3 - (nickel*0.05);
    print(Math.round(nickel*20)/20)
        if(nickel<2) println("_Nickel") else if (nickel>=2) println("_Nickels")
   

var penny = Math.floor(remChange4*100);
    print(Math.round(penny*100)/100)
        if(penny<2) println("_Penny") else if (penny>=2) println("_Pennies")

return Unit;
}


What I have tried:

here is what i have tried, but it isnt giving me output

fun main(args: Array<String>) {
val change = 10.88
Dollar(change);
Quarter(change);
Dime(change);
Nickel(change);
Penny(change);
}
fun Dollar(myChange: Double): Double{
val dollar = myChange/(1.00).toFloat();
val change1 = myChange - (dollar * 1.00);
print((dollar * 1)/1);
if(dollar<2) println("_Dollar"); else if (dollar>=2) println("_Dollars");
return change1
}
fun Quarter(myChange: Double): Double{
val quarter = myChange/(0.25).toFloat();
val change2 = myChange - (quarter * 0.25);
println((quarter*4)/4);
if(quarter<2) println("_Quarter"); else if (quarter>=2) println("_Quarters");
return change2
}
fun Dime(myChange: Double): Double{
val dime = myChange/(0.10).toFloat();
val change3 = myChange - (dime * 0.10);
println((dime * 10)/10);
if(dime<2) println("_Dime"); else if(dime>=2) println("_Dimes");
return change3
}
fun Nickel(myChange: Double): Double{
val nickel = myChange/(0.05).toFloat();
val change4 = myChange - (nickel * 0.05);
println((nickel*20)/20);
if(nickel<2) println("_Nickel"); else if (nickel>=2) println("_Nickels");
return change4
}
fun Penny(myChange: Double){
val penny = myChange/100);
print((penny * 100)/ 100);
if(penny<2) println("_Penny"); else if (penny>=2) println("_Pennies");

return Unit
}
Posted
Updated 5-Dec-21 5:45am
Comments
MissMacDaddy 5-Dec-21 10:40am    
my assignment instructions are as follows:

Create a local double variable called change and set it to a test value (e.g. 44.77)
Create five separate functions dollars(), quarters(), dimes(), nickels() and pennies().
Each function will
- accept a double amount as an argument
- calculate and display that denominations number (e.g. 7 Dollars)
- then return the recalculated change (e.g. 0.67)
In Kotlin, the function signature for quarters would be: quarters(myChange: Double): Double
Richard MacCutchan 5-Dec-21 10:43am    
So what is the problem?
MissMacDaddy 5-Dec-21 10:47am    
I cant figure out how to take my one function code and turn it into multiple functions. When I try, it's giving me errored output that I cant figure out how to work around. The first code works and is correct, but it's not the assignment. I needed to make multiple functions, which sucks because one function is just so much easier. The second code is the code I'm stuck on.
Richard MacCutchan 5-Dec-21 10:56am    
"it's giving me errored output "
You need to show us the errors, we cannot see your screen.
MissMacDaddy 5-Dec-21 11:25am    
I was actually able to fix some errors. Now it's giving me output, but not the correct output. It's currently giving me:

10.88_Dollars
43.52
_Quarters
108.79999837875371
_Dimes
_Nickels
0.10880000000000001_Penny

This is the output I need:
10_Dollars
3_Quarters
1_Dime
0_Nickel
3_Pennies

1 solution

Here is a sample that may help. It is far from perfect but should give you an idea how to proceed. Ideally you would have another function that is called to do the printing, but you can leave that for the next assignment.
Kotlin
fun main(args: Array<String>) {
    val total = 10.88
    var change = Dollar(total);
    change = Quarter(change);
//    change = Dime(change);
//    change = Nickel(change);
//    Penny(change);
    println("")
}

fun Dollar(myChange: Double): Double{
    val dollars = Math.floor(myChange)
    val remainder = myChange - dollars;
    print(dollars)
    print(" Dollar")
    if (dollars != 1.0)
        print("s");
    print(", ")
    return remainder
}

fun Quarter(myChange: Double): Double{
    val quarters = Math.floor(myChange / 0.25)
    val remainder = myChange - (quarters * 0.25);
    print(quarters)
    print(" Quarter")
    if (quarters != 1.0)
        print("s");
    print(", ")
    return remainder
}
 
Share this answer
 
Comments
MissMacDaddy 5-Dec-21 12:25pm    
You, sir, are a God. It even makes sense to me what you are doing. I totally did not connect that the var can cycle down in the main like that. I have built a code based on what you've shown me, but I've ran into an error, would you help me out still?

fun main(args: Array<string>) {
val total = 10.30
var change = Dollar(total);
change = Quarter(change);
change = Dimes(change)
change = Nickel(change)
Penny(change)
println("")
}

fun Dollar(myChange: Double): Double{
val dollars = Math.floor(myChange)
val remainder = myChange - dollars;
print(dollars)
print(" Dollar")
if (dollars != 1.0)
print("s");
print(", ")
return remainder
}

fun Quarter(myChange: Double): Double{
val quarters = Math.floor(myChange / 0.25)
val remainder = myChange - (quarters * 0.25);
print(quarters)
print(" Quarter")
if (quarters != 1.0)
print("s");
print(", ")
return remainder
}

fun Dimes(myChange: Double): Double{
val dime = Math.floor(myChange / 0.10)
val remainder = myChange - (dime * 0.10);
print(dime)
print(" Dime")
if (dime != 1.0)
print("s");
print(", ")
return remainder
}

fun Nickel(myChange: Double): Double{
val nickel = Math.floor(myChange / 0.05)
val remainder = myChange - (nickel * 0.05);
print(nickel)
print(" Nickel")
print(", ")
return remainder
}
fun Penny(myChange: Double): Double{
val penny = Math.floor(myChange / 0.01)
print(penny)
if (penny != 1.0)
print("s");
print(", ")

return Unit
}

Output tells me:
Type mismatch: inferred type is Unit but Double was expected

If I had to guess, id say it's the last return type that is throwing me off here.
Richard MacCutchan 5-Dec-21 12:44pm    
The Penny function should not return anything, since it is the last one. So you need to change the function signature, and remove the return statement.
MissMacDaddy 5-Dec-21 13:00pm    
I GOT IT! Richard, if i knew you in person, id buy you dinner. Thank you, I appreciate you!
Richard MacCutchan 5-Dec-21 13:43pm    
That's OK. We are all volunteers here and do not look for any reward beyond a simple "thank you".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900