473,786 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Still learning and am trying to teach myself java-Please help!

14 New Member
I am trying to write a program that tells the minium number of coins to give out for any amount of change from 1 cent to 99 cents. For example if the amount is 57 cents in the commmand prompt, the output would be:
57 cents can be given as
2 quarters, 1 nickel and 2 pennies.

while using coins 25, 10, 5, and 1

I am thinking

int computeCoin(coi nValue,):
//Precondition: 0<coinValue<100 ;
//Postconditionre turned value has been set to equal the max number of coins
//returnedValue*c oinValue
??????????????? ????
Nov 7 '06 #1
7 1679
r035198x
13,262 MVP
I am trying to write a program that tells the minium number of coins to give out for any amount of change from 1 cent to 99 cents. For example if the amount is 57 cents in the commmand prompt, the output would be:
57 cents can be given as
2 quarters, 1 nickel and 2 pennies.

while using coins 25, 10, 5, and 1

I am thinking

int computeCoin(coi nValue,):
//Precondition: 0<coinValue<100 ;
//Postconditionre turned value has been set to equal the max number of coins
//returnedValue*c oinValue
??????????????? ????
One of those programs where the language being used does not really affect the design of the solution. Get the algorithm right first. Here is the greedy approach

change (int n) {
int[] coins = {100, 25, 10, 5, 1};
ArrayList sol = new ArrayList();

int sum = 0;
while(sum != n) {
int x = largest item in coins such that sum + x ? n
if(no such item)
return "No Solution";
sol.add(x);
sum ? sum + x
}
return sol
Nov 7 '06 #2
hjc
14 New Member
Sorry to sound like a idiot but don't I still have to defind the method? how does the program know what int n is? I am really struggling with this---so thank you for the help---even if you don't reply

QUOTE=r035198x]One of those programs where the language being used does not really affect the design of the solution. Get the algorithm right first. Here is the greedy approach

change (int n) {
int[] coins = {100, 25, 10, 5, 1};
ArrayList sol = new ArrayList();

int sum = 0;
while(sum != n) {
int x = largest item in coins such that sum + x ? n
if(no such item)
return "No Solution";
sol.add(x);
sum ? sum + x
}
return sol[/quote]
Nov 7 '06 #3
enzoJava
4 New Member
You need to have a loop to go around (if else) statement
you need to see if you can divide the amount by 25, if yes then divide it, if no then try to divide it by 10, then 5, and finally by 1
your loop might look like something like this:

if(...){
(amount / 25);
}
else if (...){
(amount / 10);
}
else if(...){
(amount / 5);
}
...

I am trying to write a program that tells the minium number of coins to give out for any amount of change from 1 cent to 99 cents. For example if the amount is 57 cents in the commmand prompt, the output would be:
57 cents can be given as
2 quarters, 1 nickel and 2 pennies.

while using coins 25, 10, 5, and 1

I am thinking

int computeCoin(coi nValue,):
//Precondition: 0<coinValue<100 ;
//Postconditionre turned value has been set to equal the max number of coins
//returnedValue*c oinValue
??????????????? ????
Nov 7 '06 #4
sicarie
4,677 Recognized Expert Moderator Specialist
And the n is whatever amount of change you have passed to it (if you wanted 57 cents, you would pass it 57, or .57, depending on how you wanted to do it).
Nov 7 '06 #5
hjc
14 New Member
I am the dumbest human on the plant!

If I am understand every one correctly my program should look something like the following:

class Change{
public static void main(String[] args){
int n = Integer.parseIn t(args[0]);
System.out.prin tln("Your change is: ");

n = amount;

if(n / 25){
(amount / 25);
}
else if (n / 10){
(amount / 10);
}
else if(n / 5 ){
(amount / 5);
}
else if(n / 1 ){
(amount / 1);
}

change (int n) {
int[] coins = {100, 25, 10, 5, 1};
ArrayList sol = new ArrayList();

int sum = 0;
while(sum != n) {
int x = largest item in coins such that sum + x ? n
if(no such item)
return "No Solution";
sol.add(x);
sum ? sum + x
}
return sol
}
}
this is very much incorrect and i am more confused than ever--i need to be able to put an amount in the command prompt after having compiled the program and in the command prompt it would look like this

c:/java Change 57
your change on 57 cents is
2 quarters, 1 nickle and 2 pennies

Sorry to be a pain!!
Nov 8 '06 #6
sicarie
4,677 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1. public class Change{
  2.    public static void main(String[] args){
  3.     int n;
  4.  
  5.     n = amount;
  6.  
  7.     if(n / 25){             // this is not a conditional, this is a statement (see below)
  8.     (amount / 25);    // amount is not declared before this, plus it's not logically correct
  9.     }
  10.        . . . . 
  11.     }    
  12.  
  13. /* and more code */
  14.  
  15.  
If you're missing declaring 'amount' and missing conditionals, I would suggest reading the following link:

http://heather.cs.ucdavis.edu/~matlo...JavaIntro.html

Anything you use will have to be declared (such as amount) so that the Java compiler knows how to use it. As well, n/25 does not test anything, it's half of an assignment statement. The only way it would fail would be if it returned 0. Anything else is considered "true" by the java compiler. A test involves something like:

<=, >=, ==, !=

Those mean less than or equal to, greater than or equal to, equal to, not equal to, with conditionals. (n >= amount)

The statements that you have are part of conditionals, and if they were in regular statements (not in the 'if'), would need to be assigned to something.

Check out that tutorial, and then look over your code again.
Nov 8 '06 #7
maverick19
25 New Member
use the modulo operator
Nov 8 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1582
by: kneejerkreaction | last post by:
I'm seeking advice on learning .NET I have experience in ASP, HTML, Vb, Vbscript and Javascript but want to learn .NET I also have some experience with SQL Server and Oracle databases. My employer thinks even with the previous experience noted above, I am incapable of picking up .NET (they are hiring from outside) so I want to learn it independently. I have several books on .NET and XML etc. but am at the point where I need to be able work...
7
1588
by: Max | last post by:
On monday I start a semester course in Python (the alternative was Java). I was looking through the course outline and noticed the following: 1) UserDict is used. This is deprecated, right? 2) There is no mention of list comprehensions, but map and filter are taught early and then revisited later. I don't think this is good: list comprehensions are, IMO, one of Python's great features, Psyco prefers them, they're more pythonic, and map...
2
308
by: rich | last post by:
I'd like to improve my webdesign knowledge and learn how to write Javascripts. I have built my own website. I have javascripts on my site that I havent written. I download them and edit them where I need to to fit my site and then put them in. I have dabbed alittle bit into computer programming some years ago. Trying to teach myself and found it overwhelming. So I dont know how the process is going to be in learning javascript. I'm not...
4
1304
by: umpsumps | last post by:
Hello all, I've been trying to teach myself python from "How to Think Like a Python Programmer" and have been trying to write a script that checks 'words.txt' for parameters (letters) given. The problem that is the i can only get results for the exact sequnce of parameter 'letters'. I'll spare posting all the different ways I've tried to search for specific letters. But they are all generally: for line in fin:
10
1605
by: Michael Reach | last post by:
Can anyone suggest a really good course in Javascript that my son can take online, starting right away? It should be for someone without much programming experience, and I mean a real course, that covers things properly and gives problems to work, so that someone who puts in the time can really learn it. Or, the same for a book to buy. Thanks
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.