Question

Using Java, create functions that return specific values listed below. MUST use integer only, functions, loops,...

Using Java, create functions that return specific values listed below. MUST use integer only, functions, loops, conditionals. Money has to be stored as two different integers, one integer for dollars and another for cents. No use of double :

String moneyToString(int d, int c); // Returns the Money as words. Ex, moneyToString(10,24) => "$10.24"


int biggestMoney(int d1, int c1, int d2, int c2, int d3, int c3); // Returns which money is biggest.
Ex, biggestMoney(10,24,3,90,1,23) => 1
Ex, biggestMoney(3,90,1,23,10,24) => 3


void printChangeFrom20(int d, int c);
// You may assume you always pay with $20, you always owe a positive amount that is <= $20.
// If you owe $5.12 and pay with $20.00, your change should be $14.88
// If you owe $3.91 and pay with $20.00, your change should be $16.09

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

import java.io.*;
import java.util.*;
import java.lang.*;

public class Main
{
// Returns the Money as words. Ex, moneyToString(10,24) => "$10.24"
public static String moneyToString(int d, int c)
{
return "$"+String.valueOf(d)+"."+String.valueOf(c);
}
  
// Returns which money is biggest.
// Ex, biggestMoney(10,24,3,90,1,23) => 1
// Ex, biggestMoney(3,90,1,23,10,24) => 3
public static int biggestMoney(int d1, int c1, int d2, int c2, int d3, int c3)
{
// first we will compare dollars and if any dollar value
// is bigger than others no need to check for cents
if(d1 > d2 && d1 > d3)
return 1;
if(d2 > d1 && d2 > d3)
return 2;
if(d3 > d1 && d3 > d2)
return 3;
  
// only condition pending is when all three dollars value are same.
// compare cents now
if(c1 > c2 && c1 > c3)
return 1;
if(c2 > c1 && c2 > d3)
return 2;
if(c3 > c1 && c3 > c2)
return 3;
  
// Now case when dollar and cent values are same for all three
return 1;
}
  
// If you owe $5.12 and pay with $20.00, your change should be $14.88
public static void printChangeFrom20(int d, int c)
{
System.out.print("Change is: ");
int cents = 100 - c; // 1 dollar = 100 cents
int dollars = 19 - d;
System.out.println("$"+String.valueOf(dollars)+"."+String.valueOf(cents));
}

   public static void main(String[] args) {
   System.out.print("Money: ");
       System.out.println(moneyToString(10, 67));
       System.out.print("Biggest Money is: ");
       System.out.println(biggestMoney(10,24,3,90,1,23));
       System.out.print("Biggest Money is: ");
       System.out.println(biggestMoney(10,25,10,26,10,23));
       printChangeFrom20(5, 20);
   }
}

Test Output

Add a comment
Know the answer?
Add Answer to:
Using Java, create functions that return specific values listed below. MUST use integer only, functions, loops,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Java, create functions that return specific values listed below. MUST use integer only, functions, loops,...

    Using Java, create functions that return specific values listed below. MUST use integer only, functions, loops, conditionals. Money has to be stored as two different integers, one integer for dollars and another for cents. No use of double : String moneyToString(int d, int c); // Returns the Money as words. Ex, moneyToString(10,24) => "$10.24" void printSum(int d1, int c1, int d2, int c2); // Prints the sum of monies. Ex, printSum(10,24,3,90) => print "$14.14" int biggestMoney(int d1, int c1, int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT