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

New Topic/Question
Reply


MultiQuote





|