Page 1 of 1

## 9 Replies - 8397 Views - Last Post: 21 September 2008 - 02:06 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=64597&amp;s=9c6ff3d3e41ea263ac2c91f6306ad607&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Net the Nabi

Reputation: 0
• Posts: 10
• Joined: 21-September 08

Posted 21 September 2008 - 12:48 PM

I am having a small bit of trouble with my homework and I was hoping you guys could help. I know asking for help is frowned upon but I just don't see how I can fix this. My code compiles and runs but doesn't output what it's suppose to.

I'm going to post the assignment and my code. If someone could help me please I would really appreciate it.

Assignment:

LAB ASSIGNMENT A3.2

Coins

Background:

Some cash register systems use change machines that automatically dispense coins. This lab will investigate the problem solving and programming behind such machinery. You always want to use the fewest coins possible. You should use integer mathematics to solve this problem.

Provide the number of cents through the constructor. Write a method that calculates the number of each type of coin.

Examples:

35 cents =>
Quarter(s) 1
Dime(s) 1
Nickel(s) 0
Penny(s) 0

41 cents =>
Quarter(s) 1
Dime(s) 1
Nickel(s) 1
Penny(s) 1

Assignment:

1. One class with a main method.

2. Run the samples from above to check your work.

3. Run the following three samples and copy the sample runs into your class file, print out the code for the class and hand in.

94 cents
59 cents
19 cents

4. Do not worry about singular versus plural endings, i.e. quarter/quarters.

My main problem is that when I am asked for my value I am given incorrect answers.

Take 54 cents for example.

I run my code and the output reads Enter a Value. I enter 54.

I am given...
2 Quarter(s)
5 Dime(s)
11 Nickel(s)
59 Penny(ies).

Where as I know it should be..
2 Quarter(s)
0 Dime(s)
1 Nickel(s)
4 Penny(ies)

Now here's my code...

```package coins;

import java.util.Scanner;

class Cents {

int Quarter;
int Dime;
int Nickel;
int Penny;

Cents(int Q, int D, int N, int P) { //Constructor for Cents
Quarter = 25;
Dime = 10;
Nickel = 5;
Penny = 1;

}

// Recursive Return Method to Obatin Lowest possible value of Coins

int Quarter(int value)
{
return value / 25;
}

int Dime(int value)
{
return value / 10;
}

int Nickel(int value)
{
return value / 5;

}

int Penny(int value)
{
return value / 1;

}

//End of "RRM"

public static void main(String[] args) {

new Cents();
}

public Cents()
{

Scanner snr = new Scanner(System.in); //Scanner for Cents
int C;

System.out.println("Enter a Value ");

C = snr.nextInt();

System.out.println(Quarter(C) + " Quarter(s)");
System.out.println(Dime(C) + " Dime(s)");
System.out.println(Nickel(C) + " Nickel(s)");
System.out.println(Penny(C) + " Penny(ies)");
}
}

```

Now that you have everything I think I'll head over to the intro forum as I feel its only right to introduce myself. I hope I posted this correctly and haven't broken any rules. If you can please help.

~Net

This post has been edited by Net the Nabi: 21 September 2008 - 04:15 PM

Is This A Good Question/Topic? 0

### #2 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

Posted 21 September 2008 - 01:07 PM

The problem is that you send the same parameter value to all functions. What you have to do is, when you have calculated the number of quarters returned you have to subtract the number of quarters * 25 from C before passing the value to the next method. Then you do the same for the dimes and nickles.

This post has been edited by Gloin: 21 September 2008 - 01:08 PM

### #3 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

Posted 21 September 2008 - 01:13 PM

What really makes this problem interresting is when you consider the case that there are no nickles.

Let's say this is the case and you want to return change for a dollar for an item that cost 43 cents.

2 quarters
7 pennies
= 9 coins

although the optimal solution would be

1 quarter
3 dimes
2 pennies
= 6 coins

This is a problem called subset-sum and is a very well known dynamic programming algorithm.

This post has been edited by Gloin: 21 September 2008 - 01:17 PM

### #4 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

Posted 21 September 2008 - 01:20 PM

By the way, in your constructor, you set the variables

Quarter = 25;
Dime = 10;
Nickel = 5;
Penny = 1;

but then you don't use these variables in your methods. Instead you hardcode

int Dime(int value)
{
return value / 10;
}

Use the variables, this is what they're for.

return value / Dime;

### #5 Net the Nabi

Reputation: 0
• Posts: 10
• Joined: 21-September 08

Posted 21 September 2008 - 01:22 PM

Gloin, on 21 Sep, 2008 - 01:07 PM, said:

The problem is that you send the same parameter value to all functions. What you have to do is, when you have calculated the number of quarters returned you have to subtract the number of quarters * 25 from C before passing the value to the next method. Then you do the same for the dimes and nickles.

I thank you for your quick response. I also see what your saying in the second post you made. I still have a problem after fixing it as you said. Now I am getting ridiculous numbers rather than something that somewhat made sense. I'm sure it's my noobishness thats leading me to either misinterpreting your help or I've done something wrong again.

Here is my updated code..

```//“I, Rob Barry, affirm that this program is my own, original work.
//I did not copy any part of this code from anyone or any source.”
//Signed Rob Barry

package coins;

import java.util.Scanner;

class Cents {

int Quarter;
int Dime;
int Nickel;
int Penny;

Cents(int Q, int D, int N, int P) { //Constructor for Cents
Quarter = 25;
Dime = 10;
Nickel = 5;
Penny = 1;

}

// Recursive Return Method to Obatin Lowest possible value of Coins

int Quarter(int value)
{
return value / 25;
}

int Dime(int value)
{
return value - Quarter(value) * 25;
}

int Nickel(int value)
{
return value - Dime(value) * 25;

}

int Penny(int value)
{
return value - Nickel(value) * 25;

}

//End of "RRM"

public static void main(String[] args) {

new Cents();
}

public Cents()
{

Scanner snr = new Scanner(System.in); //Scanner for Cents
int C;

System.out.println("Enter a Value ");

C = snr.nextInt();

System.out.println(Quarter(C) + " Quarter(s) ");
System.out.println(Dime(C) + " Dime(s) ");
System.out.println(Nickel(C) + " Nickel(s) ");
System.out.println(Penny(C) + " Penny(ies)");
}
}

```

Whatever I've done wrong I can't see. Again thanks for the help.

~Net

This post has been edited by Net the Nabi: 21 September 2008 - 01:23 PM

### #6 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

Posted 21 September 2008 - 01:33 PM

Don't worry, we'll sort this out.

Based on the code from your first submission,

System.out.println(Quarter( C ) + " Quarter(s) ");
System.out.println(Dime( C ) + " Dime(s) ");
System.out.println(Nickel( C ) + " Nickel(s) ");
System.out.println(Penny( C ) + " Penny(ies)");

exchange those rows for

System.out.println(Quarter( C ) + " Quarter(s) ");
C = C - (Quarter * Quarter( C ));
System.out.println(Dime( C ) + " Dime(s) ");
C = C - (Dime * Dime( C ));
System.out.println(Nickel( C ) + " Nickel(s) ");
C = C - (Nickel * Nickel( C ));
System.out.println(Penny( C ) + " Penny(ies)");

This post has been edited by Gloin: 21 September 2008 - 01:39 PM

### #7 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 21 September 2008 - 01:41 PM

A quick and dirty one

Edited: at the post owner request who does not want to see a complete solution
Never seen it before.... this guy must be congratulated... I'll monitor quite closely his next questions and will be happy to help

This post has been edited by pbl: 21 September 2008 - 06:40 PM

### #8 Net the Nabi

Reputation: 0
• Posts: 10
• Joined: 21-September 08

Posted 21 September 2008 - 01:47 PM

After changing those rows I got this in my output.

init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Netnabi4\My Documents\NetBeansProjects\Coins\build\classes
compile:
run:
Enter a Value
59
2 Quarter(s)
9 Dime(s)
-166 Nickel(s)
4209 Penny(ies)
BUILD SUCCESSFUL (total time: 2 seconds)

Also this is what my code looks like now...

```//“I, Rob Barry, affirm that this program is my own, original work.
//I did not copy any part of this code from anyone or any source.”
//Signed Rob Barry

package coins;

import java.util.Scanner;

class Cents {

int Quarter;
int Dime;
int Nickel;
int Penny;

Cents(int Q, int D, int N, int P) { //Constructor for Cents
Quarter = 25;
Dime = 10;
Nickel = 5;
Penny = 1;

}

// Recursive Return Method to Obatin Lowest possible value of Coins

int Quarter(int value)
{
return value / 25;
}

int Dime(int value)
{
return value - Quarter(value) * 25;
}

int Nickel(int value)
{
return value - Dime(value) * 25;

}

int Penny(int value)
{
return value - Nickel(value) * 25;

}

//End of "RRM"

public static void main(String[] args) {

new Cents();
}

public Cents()
{

Scanner snr = new Scanner(System.in); //Scanner for Cents
int C;

System.out.println("Enter a Value ");

C = snr.nextInt();

System.out.println(Quarter(C) + " Quarter(s) ");
C = C - (Quarter * Quarter(C));
System.out.println(Dime(C) + " Dime(s) ");
C = C - (Dime * Dime(C));
System.out.println(Nickel(C) + " Nickel(s) ");
C = C - (Nickel * Nickel(C));
System.out.println(Penny(C) + " Penny(ies)");
}
}

```

TBH after adding that it looks like I should change things back to the old way I code things which there was a problem with. Thanks for the help btw. Your reassurance and help is very nice and I am very grateful for it.

EDIT: is there anyway to make the code thing collapsible? I think that would making things look nicer.

This post has been edited by Net the Nabi: 21 September 2008 - 01:48 PM

### #9 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

Posted 21 September 2008 - 01:55 PM

Unfortunately you kept modyfing that last code instead of changing the first as I asked you.

### #10 Net the Nabi

Reputation: 0
• Posts: 10
• Joined: 21-September 08

Posted 21 September 2008 - 02:06 PM

Gloin, on 21 Sep, 2008 - 01:55 PM, said:

Unfortunately you kept modyfing that last code instead of changing the first as I asked you.

Im sorry bro. I'm trying to write 2 essays and do this all at once. Not to mention I have other people in the class who are having problems aswell. I see what you were saying. Thanks for your time.

BTW pbl Thanks for the code and it ran great. I just have one thing. I would have never thought to do this like that. atleast not at this stage in my java experience. I will still use my code I worked on though, as I feel that using this would be plagiarism.

EDIT: PBL I sent you a message which I would be greatful if you responded too.

This post has been edited by Net the Nabi: 21 September 2008 - 04:15 PM