Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Field;
- import java.util.Scanner;
- /**
- * Coins: This class accepts a certain amount of monetary change (in cents). The
- * output is a list of the number of quarters, dimes, nickels, and pennies that
- * will make that amount of change with the least number of coins possible.
- *
- * @author Krithika K Yetchina
- * @version September 4th
- * @author Period: 3
- *
- * @author Assignment: Lab Activity 3.2 - Coins
- *
- * @author Sources: none
- */
- public class Coins
- {
- private int myChange;
- private int dime;
- private int penny;
- private int quarter;
- private int nickel;
- public Coins( int change )
- {
- myChange = change;
- }
- public void calculate()
- {
- dime = 10;
- penny = 1;
- quarter = 25;
- nickel = 5;
- int quarters = Math.round((int) change / quarter);
- change = change % 25;
- int dimes = Math.round((int) change / dime);
- change = change % 10;
- int nickels = Math.round((int) change / nickel);
- change = change % 5;
- int pennies = Math.round((int) change / penny);
- System.out.println(myChange + " cents =>"
- System.out.println("Quarter(s): " + quarters);
- System.out.println("Dime(s): " + dimes);
- System.out.println("Nickel(s): " + nickels);
- System.out.println("Pennie(s): " + pennies);
- }
- /**
- * Intended only for debugging.
- *
- * <p>
- * A generic toString implementation that uses reflection to print names and
- * values of all fields <em>declared in this class</em>. Note that
- * superclass fields are left out of this implementation.
- * </p>
- *
- * @return a string representation of this Easter.
- */
- public String toString()
- {
- String str = this.getClass().getName() + "[";
- String separator = "";
- Field[] fields = this.getClass().getDeclaredFields();
- for ( Field field : fields )
- {
- try
- {
- str += separator + field.getType().getName() + " " + field.getName() + ":" + field.get( this );
- }
- catch ( IllegalAccessException ex )
- {
- System.out.println( ex );
- }
- separator = ", ";
- }
- return str + "]";
- }
- /**
- * Tester for the Coins class.
- *
- * @param args
- * command line arguments - not used
- */
- public static void main( String[] args )
- {
- Scanner console = new Scanner( System.in );
- System.out.print( "Please enter the number of cents --> " );
- int cents = console.nextInt();
- Coins change = new Coins( cents );
- change.calculate();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement