(no subject)

Jan 16, 2009 10:21

I've received some very good comments to my blog, concerning the style of my comments in source code. It's true that in professional projects, very obvious comments are counterproductive, especially when changes are made to the code they are explaining. However, for the time being, I'm trying to remember Java itself, not the decisions particular to a specific program. So I am temporarily commenting my learning exercises in such a way that a complete newcomer might be able to follow it.

I wish I had done that on Herdcat when Bruce and I wrote it in Python a year ago, because going back now, I don't understand the code or remember the keywords. Fortunately I still have my Quizlet list about Python keywords and punctuation set up, and that is giving me a refresher.

Here is part two of my first Java homework assignment.

Enhance the application so that it prompts for and reads a double value representing a monetary amount. Then determine the fewest number of each bill and coin needed to represent that amount, starting with the highest (assume that a ten dollar bill is the maximum size needed). For example, if the value entered is 47.63, then the program should print the equivalent amount as:

4 ten dollar bills
1 five dollar bills
2 one dollar bills
1 dimes
0 nickels
2 pennies

Here is my solution.

// Import the Scanner class to receive console input.
import java.util.Scanner;

// The name of this program, or "class", is "currency".
public class currency
{

public static void main(String[] args)
{

// Use the Scanner class to find out what the user types.
Scanner keyboard = new Scanner(System.in);

// Print a request for the user to enter the input.
System.out.println("Enter a monetary amount for which you want to make change, followed by space or return.");

/**
* keyboard.nextDouble() reads one double from the keyboard.
* Then it is defined as a double variable to hold their input as a monetary amount.
*/
double userInput = keyboard.nextDouble();

/** This process uses remaindering, and is going to take remainders out of other remainders, and you can't remainder
* a double with another double. So it is best to turn it into cents right away and work with only integers.
* So we are going to make change in all pennies first.
*/
userInput = userInput * 100;

/** Now it's a pile of pennies called totalCents. Unfortunately because doubles are imprecise, Java would give
* userInput a partial penny and throw it out when userInput is type-cast into an int.
* I did some research and found Math.round to fix it.
*/
int totalCents = (int)Math.round(userInput);

/** 1.
* Dividing total cents by 1000 will result in the number of ten-dollar bills that leaves the least change.
* That value is named tenDollarBills and will be printed.
*/
int tenDollarBills = totalCents / 1000;

// Remaindering the total number of pennies by 1000 will give us how many pennies remain after changing as many
// as possible into $10 bills. That amount is named changeFromTens and will not be printed.
int changeFromTens = totalCents % 1000;

/** 2.
* Dividing changeFromTens, this time by 500, gives us the number of $5 bills that leaves the least change.
* That value is named fiveDollarBills and will be printed.
*/
int fiveDollarBills = changeFromTens / 500;

// Once again we do the remaindering step, this time by 500, to find out the change remaining after the $5 bills.
// That amount is named changeFromFives and will not be printed.
int changeFromFives = changeFromTens % 500;

/** 3.
* Dividing changeFromFives, this time by 100, gives us the number of $1 bills that leaves the least change.
* That value is named oneDollarBills and will be printed.
*/
int oneDollarBills = (int)changeFromFives / 100;

// Once again we do the remaindering step, this time by 100, to find out the change remaining after the $1 bills.
// That amount is named changeFromOnes and will not be printed.
int changeFromOnes = changeFromFives % 100;

/** 4.
* Dividing changeFromOnes, this time by 25, gives us the number of quarters that leaves the least change.
* That value is named quarters and will be printed.
*/
int quarters = changeFromOnes / 25;

// Once again we do the remaindering step, this time by 25, to find out the change remaining after the quarters.
// That amount is named changeFromQuarters and will not be printed.
int changeFromQuarters = changeFromOnes % 25;

/** 5. dimes
* Dividing changeFromQuarters, this time by 10, gives us the number of dimes that leaves the least change.
* That value is named dimes and will be printed.
*/
int dimes = changeFromQuarters / 10;

// Once again we do the remaindering step, this time by 10, to find out the change remaining after the dimes.
// That amount is named changeFromDimes and will not be printed.
int changeFromDimes = changeFromQuarters % 10;

/** 6. nickels
* Dividing changeFromDimes, this time by 5, gives us the number of nickels that leaves the least change.
* That value is named nickels and will be printed.
*/
int nickels = (int)changeFromDimes / 5;

// Once again we do the remaindering step, this time by 5, to find out the change remaining after the dimes.
// That amount is named pennies and will be printed.
int pennies = changeFromDimes % 5;

// Print results by concatenating strings with the output of those operations.
System.out.println(tenDollarBills + " ten dollar bills" );
System.out.println(fiveDollarBills + " five dollar bills" );
System.out.println(oneDollarBills + " one dollar bills" );
System.out.println(quarters + " quarters" );
System.out.println(dimes + " dimes" );
System.out.println(nickels + " nickels" );
System.out.println(pennies + " pennies" );

}

}

hacking, python

Previous post Next post
Up