Sample Code For Project 1
[Kathy's Code][Zak's Code]
//Judy's code
public class DollarChanger {
public static void main(String[] args) {
int dollar = 57;
int twenties = 0;
int tens = 0;
int fives = 0;
int remainder= 0;
if (args.length > 0) {
//String tmp = args[0];
//after string class introduced add one
double temp = Double.parseDouble(args[0]);
if(temp < 0){
temp = (-1)*temp;
}
dollar = (int)temp;
}
twenties = toTwenty(dollar);
remainder = dollar%20;
tens = toTen(remainder);
remainder = remainder%10;
fives = toFive(remainder);
remainder =remainder%5;
String str = display(dollar, twenties, tens, fives, remainder);
System.out.println(str);
}
public static int toTwenty(int dollar) {
return dollar/20;
}
public static int toTen(int dollar) {
return dollar/10;
}
public static int toFive(int dollar) {
return dollar/5;
}
public static String display(int d, int t, int ten, int five, int one) {
String msg=d + " dollars equal\n" + t + " 20\'s\n"+
ten + " 10\'s\n" + five + " 5\'s\n" + one + " 1\'s";
return msg;
}
}
Return to top
//Author: Kathy Gerlach
public class DollarChanger {
public static void main(String[] args) {
//Variables
int twenties, tens, fivesandones, fives, ones;
int totaldollars = 57; //Initialization
int tempamount = 57; //Initialization
//Convert input string to integer type
if (args.length > 0) { //Test for command line value
String convertinput=args[0];
int tempdollars = Integer.parseInt(convertinput);
if (tempdollars < 0) { //Convert negative to positive
tempdollars = Calculator.mul(-1,tempdollars);
}
totaldollars = tempdollars;
}
//Calculate the number of twenties
twenties = toTwenty(totaldollars);
//Reduce input total by twenties amount
tempamount = Calculator.sub(totaldollars,(Calculator.mul(twenties,20)));
//Calculate the number of tens using reduced amount
tens = toTen(tempamount);
//Reduce input total by tens amount
tempamount = Calculator.sub(tempamount,(Calculator.mul(tens,10)));
//Calculate the number of fives using reduced amount
fives = toFive(tempamount);
//Calculate the number of ones
ones = tempamount % 5;
//Concantenate the results from the methods of the DollarChanger
String results = ("$" + totaldollars + " dollars equals" + "\n"
+ twenties + "\t" + " $20\'s" + "\n"
+ tens + "\t" + " $10\'s" + "\n"
+ fives + "\t" + " $5\'s" + "\n"
+ ones + "\t" + " $1\'s");
//Print the results
display(results);
}
/*Method for toTwenty
This method receives an integer value and returns the number
of 20's contained in the amount received */
public static int toTwenty(int amount) {
return amount / 20;
} //end of toTwenty method
/*Method for toTen
This method receives an integer value and returns the number
of 10's contained in the amount received */
public static int toTen(int amount ) {
return amount / 10;
} //end of toTen method
/*Method for toFive
This method receives an integer value and returns the number
of 5's contained in the amount received */
public static int toFive(int amount) {
return amount / 5;
} //end of toFive method
/*Method for display
This method receives a string and prints the string received */
public static void display(String msg) {
System.out.println(msg);
} //end of display method
} // end of class DollarChanger body
Return to top
//Author: Zak Anderson
public class DollarChanger
{
// Main function to control program flow
public static void main(String[] args)
{
int iDollars = 57;
int iTwenties = 0;
int iTens = 0;
int iFives = 0;
int iRemainder = 0;
// Check for command line arguments
if (args.length > 0)
{
String oneArg = args[0];
double temp = Double.parseDouble(oneArg);
// if negative convert to positive
if (temp < 0)
temp *= -1;
iDollars = (int)temp;
}
// call each method to convert the remaining value
if(iDollars >= 20)
{
iTwenties = toTwenty(iDollars);
}
if((iDollars % 20) > 0)
{
iTens = toTen(iDollars % 20);
}
if((iDollars % 10) > 0)
{
iFives = toFive(iDollars % 10);
}
if((iDollars % 5) > 0)
{
iRemainder = (iDollars % 5);
}
display(iTwenties, iTens, iFives, iRemainder);
}
/**
* Converts dollars to 20s
*/
public static int toTwenty(int iDollars)
{
return iDollars/20;
}
/**
* Converts dollars to 10s
*/
public static int toTen(int iDollars)
{
return iDollars/10;
}
/**
* Converts dollars to 5s
*/
public static int toFive(int iDollars)
{
return iDollars/5;
}
/**
* Display five quantities
*/
public static void display(int iTwenties, int iTens, int iFives, int iRemainder)
{
System.out.println(iTwenties + " 20's");
System.out.println(iTens + " 10's");
System.out.println(iFives + " 5's");
System.out.println(iRemainder + " 1's");
}
}
Return to top