Advertisement
Guest User

Untitled

a guest
Sep 5th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  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.  
  41.     // wait i think.... you can use mod...
  42.  
  43.     /**
  44.      * Intended only for debugging.
  45.      *
  46.      * <p>
  47.      * A generic toString implementation that uses reflection to print names and
  48.      * values of all fields <em>declared in this class</em>. Note that
  49.      * superclass fields are left out of this implementation.
  50.      * </p>
  51.      *
  52.      * @return a string representation of this Easter.
  53.      */
  54.     public String toString()
  55.     {
  56.         String str = this.getClass().getName() + "[";
  57.         String separator = "";
  58.  
  59.         Field[] fields = this.getClass().getDeclaredFields();
  60.  
  61.         for ( Field field : fields )
  62.         {
  63.             try
  64.             {
  65.                 str += separator + field.getType().getName() + " " + field.getName() + ":" + field.get( this );
  66.             }
  67.             catch ( IllegalAccessException ex )
  68.             {
  69.                 System.out.println( ex );
  70.             }
  71.             separator = ", ";
  72.         }
  73.         return str + "]";
  74.     }
  75.  
  76.  
  77.     /**
  78.      * Tester for the Coins class.
  79.      *
  80.      * @param args
  81.      *            command line arguments - not used
  82.      */
  83.     public static void main( String[] args )
  84.     {
  85.         Scanner console = new Scanner( System.in );
  86.  
  87.         System.out.print( "Please enter the number of cents --> " );
  88.         int cents = console.nextInt();
  89.  
  90.         Coins change = new Coins( cents );
  91.         change.calculate();
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement