Advertisement
Guest User

Untitled

a guest
Sep 5th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.lang.reflect.Field;
  2. import java.util.Scanner;
  3.  
  4.  
  5. /**
  6.  * Coins: This class accepts a certain amount of monetary change (in cents). The
  7.  * output is a list of the number of quarters, dimes, nickels, and pennies that
  8.  * will make that amount of change with the least number of coins possible.
  9.  *
  10.  * @author Krithika K Yetchina
  11.  * @version September 4th
  12.  * @author Period: 3
  13.  *
  14.  * @author Assignment: Lab Activity 3.2 - Coins
  15.  *
  16.  * @author Sources: none
  17.  */
  18. public class Coins
  19. {
  20.     private int myChange;
  21.     private int dime;
  22.     private int penny;
  23.     private int quarter;
  24.     private int nickel;
  25.  
  26.  
  27.     public Coins( int change )
  28.     {
  29.         myChange = change;
  30.     }
  31.  
  32.  
  33.     public void calculate()
  34.     {
  35.         dime  = 10;
  36.         penny = 1;
  37.         quarter = 25;
  38.         nickel = 5;
  39.  
  40.         int quarters = Math.round((int) change / quarter);
  41.         change = change % 25;
  42.         int dimes = Math.round((int) change / dime);
  43.         change = change % 10;
  44.     int nickels = Math.round((int) change / nickel);
  45.         change = change % 5;
  46.         int pennies = Math.round((int) change / penny);
  47.  
  48.     System.out.println(myChange + " cents =>"
  49.         System.out.println("Quarter(s): " + quarters);
  50.         System.out.println("Dime(s): " + dimes);
  51.         System.out.println("Nickel(s): " + nickels);
  52.         System.out.println("Pennie(s): " + pennies);
  53.     }
  54.  
  55.     /**
  56.      * Intended only for debugging.
  57.      *
  58.      * <p>
  59.      * A generic toString implementation that uses reflection to print names and
  60.      * values of all fields <em>declared in this class</em>. Note that
  61.      * superclass fields are left out of this implementation.
  62.      * </p>
  63.      *
  64.      * @return a string representation of this Easter.
  65.      */
  66.     public String toString()
  67.     {
  68.         String str = this.getClass().getName() + "[";
  69.         String separator = "";
  70.  
  71.         Field[] fields = this.getClass().getDeclaredFields();
  72.  
  73.         for ( Field field : fields )
  74.         {
  75.             try
  76.             {
  77.                 str += separator + field.getType().getName() + " " + field.getName() + ":" + field.get( this );
  78.             }
  79.             catch ( IllegalAccessException ex )
  80.             {
  81.                 System.out.println( ex );
  82.             }
  83.             separator = ", ";
  84.         }
  85.         return str + "]";
  86.     }
  87.  
  88.  
  89.     /**
  90.      * Tester for the Coins class.
  91.      *
  92.      * @param args
  93.      *            command line arguments - not used
  94.      */
  95.     public static void main( String[] args )
  96.     {
  97.         Scanner console = new Scanner( System.in );
  98.  
  99.         System.out.print( "Please enter the number of cents --> " );
  100.         int cents = console.nextInt();
  101.  
  102.         Coins change = new Coins( cents );
  103.         change.calculate();
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement