randomly generates an amount of change and then calculates the fewest

program that randomly generates an amount of change and then calculate

Page 1 of 1

3 Replies - 1433 Views - Last Post: 26 March 2009 - 08:18 PM Rate Topic: -----

#1 gilcohen007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-March 09

randomly generates an amount of change and then calculates the fewest

Posted 26 March 2009 - 11:05 AM

an application that randomly generates an amount of change and then calculates the fewest coins required for that amount. The possible coins are quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent). The amount should be a random number between 0 and 99, inclusive. the program should generate a random number every time it runs.
For example, if the program randomly generates the number 57, then the program should output something like this:
The change of 57 cents yields 2 quarters, 1 nickels and 2 pennies.

I cant figure it out exactly here is what I have

   import javax.swing.*;

import java.util.Scanner;


		import java.util.Random;

		// Generate random #
	public  class pppp {

	  public static final void main(String[] args){



			//note a single Random object
		Random randomGenerator = new Random();

		for (int idx = 1; idx <= 1; ++idx){
		  int randomInt = randomGenerator.nextInt(100);
		  log("random # is : " + randomInt);
		}

}

private static void log(String aMessage){
		System.out.println(aMessage);

	int num;
	  int qwater =  num /25;
	int  nikle = num /10;
	int dime = num /5;
	int peny = num /5;

	  System.out.println ("qwaters: " + qwater );
	  System.out.println ("nikle: " +   nikle );
	  System.out.println ("dime: " +  dime );
	  System.out.println ("peny: " +  peny );

	  }
	}







Is This A Good Question/Topic? 0
  • +

Replies To: randomly generates an amount of change and then calculates the fewest

#2 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: randomly generates an amount of change and then calculates the fewest

Posted 26 March 2009 - 06:56 PM

You forget to remove from the amount to give back what was already gave back with higher coins

int amount;
int quater = amount / 25;
amount = amount - (quarter * 25);
int dime = amount / 10;
amount = amount - (dime * 10);
int nickel = amount / 5;
int cent = amount - (nickel * 5);

Hope this helps
Was This Post Helpful? 0
  • +
  • -

#3 gilcohen007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-March 09

Re: randomly generates an amount of change and then calculates the fewest

Posted 26 March 2009 - 08:10 PM

thanks this is very helpful :^: but i still have one error which i am not really sure?

here is my error:
//cannot find symbol
//symbol : variable quarter
//location: class pppp
//amount = amount - (quarter * 25);
^
//1 error


and here is my code.



	import javax.swing.*;

import java.util.Scanner;


		import java.util.Random;

		// Generate random #
	public  class pppp {

	  public static final void main(String[] args){



			//note a single Random object
		Random randomGenerator = new Random();

		for (int idx = 1; idx <= 1; ++idx){
		  int randomInt = randomGenerator.nextInt(100);
		  log("random # is : " + randomInt);
		}

}

private static void log(String aMessage){
		System.out.println(aMessage);

int amount;
int quater = amount / 25;
amount = amount - (quarter * 25);
int dime = amount / 10;
amount = amount - (dime * 10);
int nickel = amount / 5;
int cent = amount - (nickel * 5);
//int penny = amount / 1;
//int cent = amount - (penny * 1);

	  System.out.print("quaters: " + quater );
	  System.out.print("dime: " +   dime );
	  System.out.print("nickel: " +  nickel );
	  System.out.print("penny: " +  cent );

	  }
	}










thanks a lot

View Postpbl, on 26 Mar, 2009 - 05:56 PM, said:

You forget to remove from the amount to give back what was already gave back with higher coins


int amount;
int quater = amount / 25;
amount = amount - (quarter * 25);
int dime = amount / 10;
amount = amount - (dime * 10);
int nickel = amount / 5;
int cent = amount - (nickel * 5);

Hope this helps

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: randomly generates an amount of change and then calculates the fewest

Posted 26 March 2009 - 08:18 PM

If you can't find a single stupid typo don't predict you a great future in programming

int amount;
int quater = amount / 25; <------ r missing in quarter
amount = amount - (quarter * 25); <------ that is used here
int dime = amount / 10;
amount = amount - (dime * 10);
int nickel = amount / 5;
int cent = amount - (nickel * 5);
Was This Post Helpful? 1

Page 1 of 1