Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Newb, working on Computer Science homework. Need ideas

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Location
    Kentucky
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Newb, working on Computer Science homework. Need ideas

    I know this is very simple for most, I am having a few issues understanding the coding. Pretty sure I am close to understanding but just can't make it there, especially with lack of sleep. Any help is greatly appreciated, I plan on sticking to these forums and learning as much as I can. I'm a computer science major also so it's very serious to me.

    I have started writing this Java program for homework in Netbeans 8.0 IDE. The project is as; "This program reads a monetary amount in cents and prints the smallest possible number of coins equaling the amount." I am trying to use 289 as a test in cents. It should be 2 Dollar Coins, 3 Quarters, 1 Dimes and 4 Pennies for a total of 10 coins. My program is giving me 13 coins and I am unsure how to get it to appear which ones. I've pondered on this and read online tutorials but it's not clicking in my head, sadly. I hope this is normal I am getting worried. Been changing the code around alot, also it has to be 'monetary'.

    import java.util.Scanner;
      public class Hw1 {
         public static void main(String [] args) {
            Scanner in = new Scanner(System.in);
     
            int cents; //input variables
            int dollars, quarters, dimes, nickels, pennies; //output   
     
     
            System.out.print("Enter an amount in cents:");
            amounts = in.nextInt();
     
            dollars = amounts/100;
            amounts = amounts%100;
     
            quarters = dollars/25;
            dollars = dollars%25;
     
            dimes = quarters/10;
            quarters = quarters%10;
     
            nickels = dimes/5;
            dimes = dimes%5;
     
            pennies = nickels/1;
            nickels = nickels%1;
     
     
            System.out.println("("+dollars+" Dollars, "+quarters+"       
            Quarters, "+dimes+" Dimes, "+nickels+" Nickels, "+pennies+"     
            Pennies )");
     
       }
     
    }
    Last edited by goodtanner; August 25th, 2014 at 06:45 PM. Reason: Highlighting and fixing format, changing a couple things.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Newb, working on Computer Science homework. Need ideas

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  3. #3
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Newb, working on Computer Science homework. Need ideas

    Hi,
    apart from posting unformatted code you've got the order wrong. Start with paper and pencil to find an algorythm. On paper you would certainly not start with trying to find the number of pennies to be returned.

  4. #4
    Junior Member
    Join Date
    Aug 2014
    Location
    Kentucky
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Newb, working on Computer Science homework. Need ideas

    Thanks,
    Sorry for the beginner mistakes, I am working hard to completely understand. Also my professor handed this assignment out at as the first one for homework with very little to no training. We do not have a text in this class either, so basically I used integer division to come up with the correct answer on paper before doing this. I dont know if you would call that an algorithm but I came up with the proper answer on paper. As far as the coding goes I didnt realize it would be in the same format as my math, if I have done what you said. Sorry for the newb mistakes, I am working on it.

  5. #5
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Newb, working on Computer Science homework. Need ideas

    Don't apologize for mistakes. They are part of the learning process. Regarding your problem: You have an amount of cents. First find out whether and how many dollars you can return. Next reduce the amount of cents by the dollars returned and find out whether and how many quarters you can return and so on. Division and the modulus operator are what you need:
    int amount = 234;
    int dollars = amount / 100;
    int remaining = amount % 100;
    I'm sure you'll get it.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Newb, working on Computer Science homework. Need ideas

    I dont know if you would call that an algorithm but I came up with the proper answer on paper.
    It's an approach to a solution, and it provides you with a basis for writing code that will get you to a solution. Review your paper solution and use that to outline in code comments, pseudo-code, actual code, or some combination of all of those how the paper solution can be translated to a program solution.

Similar Threads

  1. Can someone help me with my Computer Science homework :)
    By surfelijo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 1st, 2013, 08:18 PM
  2. Working on a phdgets Project for Computer Science Need some Help.
    By Tallonejosh in forum What's Wrong With My Code?
    Replies: 21
    Last Post: April 25th, 2012, 08:39 AM
  3. AP computer science questions
    By svo in forum Java Theory & Questions
    Replies: 1
    Last Post: April 7th, 2012, 02:24 PM
  4. need help in AP Computer Science!
    By eechord in forum Member Introductions
    Replies: 3
    Last Post: October 7th, 2011, 09:57 AM
  5. computer science help
    By hairyjewbear in forum Algorithms & Recursion
    Replies: 6
    Last Post: November 4th, 2010, 04:05 PM