Easy coin project

simple bonus project i cant wrap my head around

Page 1 of 1

12 Replies - 1495 Views - Last Post: 08 October 2009 - 06:54 PM Rate Topic: -----

#1 over9000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-October 09

Easy coin project

Posted 07 October 2009 - 07:49 PM

I just started Java and its really easy but i cant figure out how to do this bonus project.

i need to get three coins
coin1 = lowest denomination
coin2 = middle denomination
coin3 = highest denomination

then i need to get a value(integer), say 18

finally i have to output how many coins i need to make the value

ex.(<input>2,3,5,18 <output>"you need 4 coins"(5+5+5+3 = 18))

its like my 5th java project ever and i dont have EasyReader at home so i commented out the input lines and assigned static values, i dont need the answer i just need to be pointed in the right direction

heres what i have, all the comments are for you guys or code that requires EasyReader

 public class Bonus1
{
	public static void main(String[] args)
	{
		
		//EasyReader console = new EasyReader();
		
		int coin1;
		int coin2;
		int coin3;
		int value;
		int coins;
		
		System.out.println("Please enter the value of coin 1: ");
		coin1 = 2;//console.readInt();
		System.out.println("Please enter the value of coin 2: ");
		coin2 = 3;//console.readInt();
		System.out.println("Please enter the value of coin 3: ");
		coin3 = 5;//console.readInt();
		
		System.out.println("Please enter the value you want to equal: ");
		value = 18;//console.readInt();
		
		/*using only if, else if, else and mod,
		 *(thats all ive learned in class)
		 * i need to do the third step mentioned above
		 *i suppose i can use while loops and other simple things i taught myself
		 */
		
		
		System.out.println(coin1);//makes sure static values are correct
		System.out.println(coin2);
		System.out.println(coin3);
		System.out.println(value);
	}
}

This post has been edited by over9000: 07 October 2009 - 08:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Easy coin project

#2 over9000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-October 09

Re: Easy coin project

Posted 07 October 2009 - 08:35 PM

22 views and nothing? not even flames? this forum is wierd
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Easy coin project

Posted 07 October 2009 - 10:34 PM

There is a modulo operator in Java which is % and returns the reminder of a division

so to give $ 58.00 in change you can

int amount = 58;

// number of $ 20
int nb20 = amount / 20; // should be 2
// what is left
amount = amount % 20; // should be 18
// number of $ 10
int nb10 = amount / 10; // should be 1
// what is left
amount = amount % 10; // should be 8
// number of $ 5
int nb5 = amount / 5; // should be 1
// what is left
int nb1 = amount % 5; // should be 3

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

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5833
  • View blog
  • Posts: 12,687
  • Joined: 16-October 07

Re: Easy coin project

Posted 08 October 2009 - 06:14 AM

Something like:
while(value>0) {
	if (value>=coin3) { value -= coin3; // other things
	} else if (value>=coin2) { value -= coin2; // other things
}



You have a special case, in that your lowest coin needn't be 1, so you may not be able to solve it.
Was This Post Helpful? 0
  • +
  • -

#5 over9000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-October 09

Re: Easy coin project

Posted 08 October 2009 - 11:49 AM

that while loop gives me an idea but it might need extra variables, ty
Was This Post Helpful? 0
  • +
  • -

#6 over9000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-October 09

Re: Easy coin project

Posted 08 October 2009 - 12:28 PM

ok my solution failed, i know what mod is and that i have to use it but i have no idea how to do this X(
Was This Post Helpful? 0
  • +
  • -

#7 over9000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-October 09

Re: Easy coin project

Posted 08 October 2009 - 01:53 PM

ok i got it to work in many solveable cases but sometines it doesnt and it seems to output "Your coins cannot equal that number.", my final else, randomly
please halp!

/*Dustin Raimondi
 *
 *Bonus1
 */
 
 public class Bonus1
{
	public static void main(String[] args)
	{
		
		//EasyReader console = new EasyReader();
		
		int coin1 = 0;
		int coin2 = 0;
		int coin3 = 1;
		int value = 4;
		int coins;
		int r1;
		int r2;
		int r3;
		
		System.out.println("Please enter the value of coin 1: ");
		//coin1 = console.readInt();
		System.out.println("Please enter the value of coin 2: ");
		//coin2 = console.readInt();
		System.out.println("Please enter the value of coin 3: ");
		//coin3 = console.readInt();
		
		System.out.println("Please enter the value you want to equal: ");
		//value = console.readInt();
		
		r1=value%coin3;
		r2=r1%coin2;
		r3=r2%coin1;
		coins=(value/coin3)+(r1/coin2)+(r2/coin1); 
		
		if(r3 > 0)
		{
			System.out.println("Your coins cannot equal that number.");
		}
		else
		{
			System.out.println("You need " + coins + " coins.");
		}
	}
}


This post has been edited by over9000: 08 October 2009 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#8 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: Easy coin project

Posted 08 October 2009 - 02:23 PM

I'm not sure if I get the problem. But if the goal is to the get the fewest number of coins possible a greedy solution where you automatically pick the highest valued coin if possible doesnt work.

example
coins : 1,4,5
value:12
the best solution is 4+4+4 = 12 which doesnt include 5 at all!

But then again maybe your goal is to just produce any possible solution...

This post has been edited by LaFayette: 08 October 2009 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#9 gawdlike  Icon User is offline

  • you can't fix stupid.

Reputation: -7
  • View blog
  • Posts: 647
  • Joined: 16-November 08

Re: Easy coin project

Posted 08 October 2009 - 02:37 PM

Didn't do it the way you were thinking but this should work. This is only for finding one possible solution though, not the least amount of coins. Fill upp with the most valuable coins until it would break the limit, and then fill with the second and so on.. As the code is now it assumes the first coin is the most valuable, you might want to do a check on that.

/*Dustin Raimondi
 *
 *Bonus1
 */
 
 public class Coins
{
    public static void main(String[] args)
    {
       
        //EasyReader console = new EasyReader();
       
        int coin1 = 0;
        int coin2 = 0;
        int coin3 = 1;
        int wantedValue = 4;
        int totalOfCoins = 0; 
        int coins;
        int r1 = 0;
        int r2 = 0;
        int r3 = 0;
       
        System.out.println("Please enter the value of coin 1: ");
        //coin1 = console.readInt();
        System.out.println("Please enter the value of coin 2: ");
        //coin2 = console.readInt();
        System.out.println("Please enter the value of coin 3: ");
        //coin3 = console.readInt();
       
        System.out.println("Please enter the value you want to equal: ");
        //value = console.readInt();
       
     	while(totalOfCoins < wantedValue){
     		if(totalOfCoins + coin1 <= wantedValue && coin1 != 0){
     			r1++;
     			totalOfCoins = totalOfCoins + coin1;
     		}
     		else if(totalOfCoins + coin2 <= wantedValue && coin2 != 0){
     			r2++;
     			totalOfCoins = totalOfCoins + coin2;
     		}
     		else if(totalOfCoins + coin3 <= wantedValue && coin3 != 0){
     			r3++;
     			totalOfCoins = totalOfCoins + coin3;
     		}
     		else if(totalOfCoins < wantedValue && totalOfCoins + coin3 > wantedValue) {
     			System.out.println("Didn't work.. ");	
                        totalOfCoins = wantedValue + 1;
     		}
     	}
//print out the amount of coins
    }
}



This post has been edited by gawdlike: 08 October 2009 - 02:48 PM

Was This Post Helpful? 0
  • +
  • -

#10 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: Easy coin project

Posted 08 October 2009 - 02:58 PM

I don't think a greedy solution works even if you are looking for just one solution come to think of it.

given coins 2,4,5 and value 8 the no solution contains coin 5.

edit: i'll happily post a solution for finding the fewest coins that adds up to an arbitrary value given an arbitrary list of coins tomorrow, it's getting pretty late now,..

This post has been edited by LaFayette: 08 October 2009 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#11 over9000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-October 09

Re: Easy coin project

Posted 08 October 2009 - 03:17 PM

i just need to find the lowest coin combination:
2,3,5,18 = 4
3,4,5,8 = 2 (4,4 || 5,3)
no zeros
im expected to use mod
Was This Post Helpful? 0
  • +
  • -

#12 over9000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-October 09

Re: Easy coin project

Posted 08 October 2009 - 05:45 PM

i need to find the lowest number of three coins, given by the user in ascending order, needed to equal the amount

2,3,5,18 = 4 5,5,5 and 3 = 4 coins

2,3,5,19 = unsolvableit is solvable, the answer is 5,5,5,2,2 5 coins, but it takes a three out i before it goes to 2, and only 1 is left

how can i get around this?

i dont have easy reader at home so i commented out the easyreader code and just put the user input in when i declare variables

 public class Bonus1
{
	public static void main(String[] args)
	{
		
		//EasyReader console = new EasyReader();
		
		int coin1 = 2;//coins
		int coin2 = 3;
		int coin3 = 5;
		int value = 19;//value to equal
		int coins;//total # of coins
		int r1;//remainders after each coin is subtracted from the total
		int r2;
		int r3;
		
		System.out.println("Please enter the value of coin 1: ");//coin denomination1
		//coin1 = console.readInt();
		System.out.println("Please enter the value of coin 2: ");//coin denomination2
		//coin2 = console.readInt();
		System.out.println("Please enter the value of coin 3: ");//coin denomination3
		//coin3 = console.readInt();
		
		System.out.println("Please enter the value you want to equal: ");//value to attempt to equal
		//value = console.readInt();
		
		r1=value%coin3;//calculates number of coins needed to make value
		r2=r1%coin2;
		r3=r2%coin1;
		coins=(value/coin3)+(r1/coin2)+(r2/coin1); 
		
		if(r3 > 0)//final output
		{
			System.out.println("Your coins cannot equal that number.");//states the task is impossible
		}
		else
		{
			System.out.println("You need " + coins + " coins.");//outputs lowest # of coins needed to make value
		}
	}
}


This post has been edited by over9000: 08 October 2009 - 05:52 PM

Was This Post Helpful? 0
  • +
  • -

#13 over9000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 07-October 09

Re: Easy coin project

Posted 08 October 2009 - 06:54 PM

bump...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1