10 Replies - 4881 Views - Last Post: 12 December 2012 - 02:08 PM Rate Topic: -----

#1 The_Programmer-   User is offline

  • Paranormal Investigator
  • member icon

Reputation: 25
  • View blog
  • Posts: 694
  • Joined: 24-October 11

Change calculator going in an infinite loop

Posted 12 December 2012 - 12:36 PM

Well I've looked through the program and everything seems like it should work. What did I do wrong?

import java.util.Scanner;

public class QuizProgram1 {
	
    public static void main(String[] args) {
    	
    	System.out.print("How much money do you owe? (Please enter the ammount without a dollar sign like 33.89): ");
    	
    	Scanner in = new Scanner(System.in);
    	double amtOwed = in.nextDouble();
    	double amtRecv = 0.0;
    	double temp = amtOwed;
    	int twenty = 0, five = 0, one = 0, quarter = 0, dime = 0, nickel = 0, penny = 0;
    	
    	while (temp != 0.00) {
    		
    		if (temp >= 20.00) {
    			double t = ((int)temp / 20.00);
    			twenty += t;
    			temp -= t * 20.00;
    			amtRecv += t * 20.00;
    		} else if (temp >= 5.00) {
    			double t = ((int)temp / 5.00);
    			five += t;
    			temp -= t * 5.00;
    			amtRecv += t * 5.00;
    		} else if (temp >= 1.00) {
    			double t = ((int)temp / 1.00);
    			one += t;
    			temp -= t * 1.00;
    			amtRecv += t * 1.00;
    		} else if (temp >= 0.25) {
    			double t = (int) (temp / 0.25);
    			quarter += t;
    			temp -= t * 0.25;
    			amtRecv += t * 0.25;
    		} else if (temp >= 0.10) {
    			double t = (int) (temp / 0.10);
    			dime += t;
    			temp -= t * 0.10;
    			amtRecv += t * 0.10;
    		} else if (temp >= 0.05) {
    			double t = (int) (temp / 0.05);
    			nickel += t;
    			temp -= t * 0.05;
    			amtRecv += t * 0.05;
    		} else if (temp >= 0.01) {
    			double t = (int) (temp / 0.01);
    			penny += t;
    			temp -= t * 0.01;
    			amtRecv += t * 0.01;
    		}
    		
    	}
    	
    	
    	System.out.println("\nYou owe $" + amtRecv + "\nChange:\n");
    	
    	if (twenty > 0) {
    		System.out.println(twenty + " $20 bill(s)");
    	}
    	if (five > 0) {
    		System.out.println(five + " $5 bill(s)");
    	}
    	if (one > 0) {
    		System.out.println(one + " $1 bill(s)");
    	}
    	if (quarter > 0) {
    		System.out.println(quarter + " Quarter(s)");
    	}
    	if (dime > 0) {
    		System.out.println(twenty + " Dime(s)");
    	}
    	if (nickel > 0) {
    		System.out.println(nickel + " Nickel(s)");
    	}
    	if (penny > 0) {
    		System.out.println(penny + " Penny(ies)");
    	}
    	
    }
    
}



It only works if I enter an amount it can parse right away like 20.00, 40.00, 5.00, or 0.04. It goes in an infinite loop when I combine things like 15.97.

Is This A Good Question/Topic? 0
  • +

Replies To: Change calculator going in an infinite loop

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Change calculator going in an infinite loop

Posted 12 December 2012 - 12:53 PM

Off the top of my head - what happens when you put a break point in and check the data type conversions and what not going on?

Say, for example, I have 20.01. Follow the math and conversions... I am thinking you'll find you will have less than a penny floating around causing you grief. Why not use the math's "floor" to get the whole number (aka dollar) instead of casting it to an integer?
http://docs.oracle.c.../lang/Math.html
Was This Post Helpful? 1
  • +
  • -

#3 The_Programmer-   User is offline

  • Paranormal Investigator
  • member icon

Reputation: 25
  • View blog
  • Posts: 694
  • Joined: 24-October 11

Re: Change calculator going in an infinite loop

Posted 12 December 2012 - 01:07 PM

Actually I fixed it by multiplying the value by 100 and doing everything using whole numbers.
import java.util.Scanner;

/**
 * 
 * @author Kenneth Clark
 * @version 12-12-2012
 * 
 * Given the amount you have paid the cashier and how much your items cost this program will calculate the exact change you need.
 *
 */

public class QuizProgram1 {
	
    public static void main(String[] args) {
    	
    	System.out.print("How much money did you pay and how much did your items cost? (Please enter the amount you paid first then press the spacebar and enter the amount your items cost then press enter. The amounts should be entered without a dollar sign.): ");
    	
    	Scanner in = new Scanner(System.in);
    	double amtPaid = in.nextDouble();
    	double itemCost = in.nextDouble();
    	double amtRecv = 0.0;
    	int temp = (int) ((amtPaid - itemCost) * 100);
    	int twenty = 0, five = 0, one = 0, quarter = 0, dime = 0, nickel = 0, penny = 0;
    	
    	while (temp != 0) {
    		
    		if (temp >= 20 * 100) {
    			int t = (temp / 2000);
    			twenty += t;
    			temp -= t * 2000;
    			amtRecv += t * 2000;
    		} else if (temp >= 5 * 100) {
    			int t = (temp / 500);
    			five += t;
    			temp -= t * 500;
    			amtRecv += t * 500;
    		} else if (temp >= 1 * 100) {
    			int t = (temp / 100);
    			one += t;
    			temp -= t * 100;
    			amtRecv += t * 100;
    		} else if (temp >= (int) (0.25 * 100)) {
    			int t = (temp / 25);
    			quarter += t;
    			temp -= t * 25;
    			amtRecv += t * 25;
    		} else if (temp >= (int) (0.10 * 100)) {
    			int t = (temp / 10);
    			dime += t;
    			temp -= t * 10;
    			amtRecv += t * 10;
    		} else if (temp >= (int) (0.05 * 100)) {
    			int t = (temp / 5);
    			nickel += t;
    			temp -= t * 5;
    			amtRecv += t * 5;
    		} else if (temp >= (int) (0.01 * 100)) {
    			int t = (temp / 1);
    			penny += t;
    			temp -= t * 1;
    			amtRecv += t * 1;
    		}
    		
    	}
    	
    	
    	System.out.println("\nYou owe $" + (amtRecv / 100.00) + "\nChange:\n");
    	
    	if (twenty > 0) {
    		System.out.println(twenty + " $20 bill(s)");
    	}
    	if (five > 0) {
    		System.out.println(five + " $5 bill(s)");
    	}
    	if (one > 0) {
    		System.out.println(one + " $1 bill(s)");
    	}
    	if (quarter > 0) {
    		System.out.println(quarter + " Quarter(s)");
    	}
    	if (dime > 0) {
    		System.out.println(twenty + " Dime(s)");
    	}
    	if (nickel > 0) {
    		System.out.println(nickel + " Nickel(s)");
    	}
    	if (penny > 0) {
    		System.out.println(penny + " Penny(ies)");
    	}
    	
    }
    
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Change calculator going in an infinite loop

Posted 12 December 2012 - 01:14 PM

Really but really complicated for nothing

And cents are integer not double you can't have 12.00000001 cents or 3.99999998 quarters. So work with integer
Never say if(doubleValue == 0) because if after calculation doubleValue is 0.000000001 or 0.99999999 it won't be equal to 0.0.

and Java has a modulo operator no need to re-invent it

double change = .....
long nbCent = (long) change * 100;

long nbQuarter = nbCent / 25;
nbCent %= 25;
long nbDime = nbCent / 10;
nbCent %= 10;
long nbNickel = nbCent / 5;
nbCent %= 5;

This post has been edited by pbl: 12 December 2012 - 01:17 PM

Was This Post Helpful? 1
  • +
  • -

#5 The_Programmer-   User is offline

  • Paranormal Investigator
  • member icon

Reputation: 25
  • View blog
  • Posts: 694
  • Joined: 24-October 11

Re: Change calculator going in an infinite loop

Posted 12 December 2012 - 01:37 PM

I have another question. I'm supposed to find a point on a circle given the radius. I need to be able to change the radius to 1.0, 0.1, 0.01, 0.001, etc. Could you point me to an equation to help me out?

Sorry if it doesn't make sense. I'm supposed to go through the x values or something and print out the y?

Posted Image
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Change calculator going in an infinite loop

Posted 12 December 2012 - 01:37 PM

What does that even mean? Is the radius your only known input? What other inputs are there?
Was This Post Helpful? 0
  • +
  • -

#7 The_Programmer-   User is offline

  • Paranormal Investigator
  • member icon

Reputation: 25
  • View blog
  • Posts: 694
  • Joined: 24-October 11

Re: Change calculator going in an infinite loop

Posted 12 December 2012 - 01:41 PM

Something like this maybe?
double yPoint = Math.pow(radius, 2) - Math.pow(xPoint, 2);
yPoint = Math.sqrt(yPoint);


I don't really understand it either modi. The output is supposed to look like this:
Posted Image
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Change calculator going in an infinite loop

Posted 12 December 2012 - 01:44 PM

Perhaps going back to your teacher for clarification on the assignment would be prudent.
Was This Post Helpful? 0
  • +
  • -

#9 The_Programmer-   User is offline

  • Paranormal Investigator
  • member icon

Reputation: 25
  • View blog
  • Posts: 694
  • Joined: 24-October 11

Re: Change calculator going in an infinite loop

Posted 12 December 2012 - 01:45 PM

Okay. Thanks for the help though.
Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Change calculator going in an infinite loop

Posted 12 December 2012 - 02:07 PM

double radius = 1.0;
for(int i = 0; i < 10; ++i) {
radius /= 10.0;
...
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Change calculator going in an infinite loop

Posted 12 December 2012 - 02:08 PM

This is only half of it. You should be able to figure out the other half. It's a solution for y where

y = sqrt( 1 - x^2 )

for ( double x = 1 ; x > -1 ; x -= 0.1 )
{
    double y = Math.sqrt( 1 - Math.pow( x, 2 ) );
    
    System.out.println( x + ", " + y );
}


That's from the equation

r^2 = x^2 + y^2 where r = 1.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1