• Lucid Dreaming - Dream Views




    Results 1 to 9 of 9
    1. #1
      Member Achievements:
      Created Dream Journal Tagger First Class Made lots of Friends on DV Referrer Bronze 5000 Hall Points Veteran Second Class
      Daredevilpwn's Avatar
      Join Date
      Oct 2011
      LD Count
      15
      Gender
      Location
      MD
      Posts
      493
      Likes
      378
      DJ Entries
      63

      Help with this java program

      Hello guys. In my computer programming class I need to make a program that can compute the pocket change of any number specified by the user. So for example if I were to input 1.30 in the program it should output something like
      "You have 5 quarters"
      "You have one nickels"
      "You have 0 dimes"
      "You have 0 pennies"

      I understand this is most likely the most simplest question but I am stumped right now.
      What would be an appropriate algorithm for this task?
      Last edited by Daredevilpwn; 09-27-2012 at 03:39 AM.

    2. #2
      Wololo Achievements:
      Created Dream Journal Tagger Second Class 1000 Hall Points Made lots of Friends on DV Populated Wall Referrer Bronze Veteran First Class
      Supernova's Avatar
      Join Date
      Jul 2009
      LD Count
      Gender
      Location
      Spiral out, keep going.
      Posts
      2,909
      Likes
      908
      DJ Entries
      10
      Well, I don't know anything about Java in particular (my class is using C++), but as far as general logic, I would start by converting the decimal amount to cents, then divide by the value of a quarter and round that figure down to an integer. That integer becomes your number of quarters, and you then subtract that number times 25 form the total. Repeat for each coin until the remaining amount is 0. There, I did your homework.

      As my instructor said, "Programming is an art, not a science."

    3. #3
      Member Achievements:
      Created Dream Journal Tagger First Class Made lots of Friends on DV Referrer Bronze 5000 Hall Points Veteran Second Class
      Daredevilpwn's Avatar
      Join Date
      Oct 2011
      LD Count
      15
      Gender
      Location
      MD
      Posts
      493
      Likes
      378
      DJ Entries
      63
      Thanks, I understand now. Good luck with your c++ class
      and yeah I agree, programming is indeed an art.
      Last edited by Daredevilpwn; 09-27-2012 at 04:09 AM.

    4. #4
      Wololo Achievements:
      Created Dream Journal Tagger Second Class 1000 Hall Points Made lots of Friends on DV Populated Wall Referrer Bronze Veteran First Class
      Supernova's Avatar
      Join Date
      Jul 2009
      LD Count
      Gender
      Location
      Spiral out, keep going.
      Posts
      2,909
      Likes
      908
      DJ Entries
      10
      Thanks, good luck with your java class! And, I say that (he says that) because it is in many ways open to interpretation, depending on how you the programmer want to do something - for example, there is not one correct way to implement the outline I gave above. It's all about creative problem solving, a very fun sort of puzzle.

    5. #5
      Retired Post Whore-73PPD jarrhead's Avatar
      Join Date
      Jan 2010
      LD Count
      82
      Gender
      Location
      Tijeras/Albuquerque
      Posts
      1,937
      Likes
      122
      DJ Entries
      24
      From what I know of javascript, I would create an object called "Quarter," "Dime," etc. Assign a value to each object (0.25, 0.10...) then create an ask box for each object, where the user inputs the number value of how many coins they have of each. "How many quarters do you have?" "How many dimes do you have?"

      From here, it would look something like

      Code:
      Quarters * [ask value] + Dimes * [dimes ask value] ...
      It's been forever so I can't tell you the specific operators for things like the ask value. Only the logical process I would do for a simple program to calculate this.


      Also, this is javaSCRIPT, not java. Not sure if my answer is of any help, which is why I gave a process of logic rather than code.

    6. #6
      ^_^ Oros's Avatar
      Join Date
      Aug 2007
      LD Count
      Don't count
      Gender
      Location
      Sweden
      Posts
      680
      Likes
      49
      Quote Originally Posted by jarrhead View Post
      Also, this is javaSCRIPT, not java. Not sure if my answer is of any help, which is why I gave a process of logic rather than code.
      What is the difference between javascript and java? I thing I've heard that it's a different scripting language, but that sound illogical, why not rename it completely then? How could you notice it was javascript and not java? Java = huge piece of code that make up a program/app/whatever?

    7. #7
      Retired Post Whore-73PPD jarrhead's Avatar
      Join Date
      Jan 2010
      LD Count
      82
      Gender
      Location
      Tijeras/Albuquerque
      Posts
      1,937
      Likes
      122
      DJ Entries
      24
      Quote Originally Posted by Oros View Post
      What is the difference between javascript and java? I thing I've heard that it's a different scripting language, but that sound illogical, why not rename it completely then? How could you notice it was javascript and not java? Java = huge piece of code that make up a program/app/whatever?
      I put in the words "not sure" because I've been told they're not the same and I only know a little bit of javascript. I know nothing of java. For all I know it could be another language entirely. That's also why I only laid out an answer in logic rather than code.

    8. #8
      Banned
      Join Date
      Jan 2013
      LD Count
      5
      Gender
      Location
      East Coast
      Posts
      122
      Likes
      46
      DJ Entries
      44
      The simplest algorithm would divide 1.30 by .25, output the amount to be the quarters, divide the remainder by .10, output the remainder to be number of dimes, and so on.

    9. #9
      Member Achievements:
      1 year registered Veteran First Class 5000 Hall Points

      Join Date
      Sep 2004
      Gender
      Location
      Seattle, WA
      Posts
      2,503
      Likes
      217
      I assume this is only meant to support the standard american change (quarters, dimes, nickels and pennies). Then, it's super easy.

      0) Define the values of each coin in pennies (since you're in Java, the cleanest way to do this is to define a Coin enum, and then your QUARTER can have a value of 25, etc. Java enums are really versatile since version 1.5).
      1) Convert input to pennies (assuming you don't have to worry about input validation or anything, all you have to do is multiply by 100, and perhaps round to the nearest int just for posterity).
      2) For each coin value, in decreasing order (start with quarters, then dimes, etc.) figure out how many coins fit in how much is left, and then: output your line, subtract the right amount, and keep moving.

      I'm being a bit vague because this is your homework and you should at least have to think about it a little bit.

      I will say that if you setup your enum properly and just define a list or array of coins in the correct order, you won't need to repeat any code. Just loop over them.

    Similar Threads

    1. Stubborn about java
      By Daredevilpwn in forum Tech Talk
      Replies: 7
      Last Post: 09-22-2012, 04:43 AM
    2. My brainfuck (Java)
      By Sornaensis in forum Tech Talk
      Replies: 2
      Last Post: 02-04-2012, 09:00 PM
    3. PHP / Java scripting help.
      By Adam in forum Tech Talk
      Replies: 11
      Last Post: 05-16-2008, 08:42 AM
    4. Hmm, anyone know why Java never loads?
      By Jdeadevil in forum Tech Talk
      Replies: 7
      Last Post: 07-29-2007, 03:16 PM
    5. Starting On Java
      By Lonestar in forum Tech Talk
      Replies: 6
      Last Post: 01-02-2007, 02:18 PM

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •