Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34
  1. #21
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Not getting correct output.

    getAverage() is a method! Did you understand my post? Do you know how to call a method? You really need to think about this stuff.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  2. #22
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    I understand, Im sorry for being so hardheaded...I've been doing this for a few weeks, trying my best! I now get the average. thank you. This is my new output:

    Java Code:
    nullBread.............	2.99
    Beans.............	3.5
    Jam.............	4.56
    Ham.............	4.67
    
    __________________________
    Total Price is.....15.72
    
    The number of items being purchased........4
    The average price per item is........3.93
    
    Amount tendered is......20.0
    Your change is.....4.279999999999999

  3. #23
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Not getting correct output.

    Great! And don't forget to check out the String.format() method. I recommend you try it on a few sample programs to get the hang of it before you incorporate it into your cashier program.

    Regards
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #24
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    I imported NumberFormat,
    Java Code:
    static void generateReceipt(Cashier c) {
            Date d = new Date(); 
            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
            NumberFormat nf = NumberFormat.getCurrencyInstance();
            
            JTextArea text = new JTextArea(c.receipt + "\n" + "__________________________" + "\n" + "Total Price is....." + nf.format(c.totalPrice) + 
                    "\n\n" + "The number of items being purchased........" + c.count + 
                    "\n" + "The average price per item is........" + nf.format(c.average) +  "\n\n" + 
                    "Amount tendered is......" + nf.format(c.tendered) + "\n" + "Your change is....." + nf.format(c.makeChange), 20, 30);
            
            JScrollPane pane = new JScrollPane(text);
    
            JOptionPane.showMessageDialog(null, pane,
                    "Today is: " + df.format(d), JOptionPane.INFORMATION_MESSAGE);
            // Write the necessary code that will generate a customer’s receipt.
            // The output must be displayed in a scrollable pane
        }
    
    }
    now all my numbers look all neat. Now all i got to work on is getting it to count change(# of quaters, dollars, dimes, etc) the customer gets in return. I have an idea, i wanna try it out. But, insight would be nice. My idea is to give the coins a value, such as
    Quarter = .25
    dine = .1
    nikel = .05.
    And implement it into the the makeChange.

  5. #25
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Not getting correct output.

    You can do that but be warned. Decimals, even doubles, can have subtle errors that creep in over time. You saw the precision errors in the original values. So it is possible that your quarters, dimes, and nickels may not sum up correctly. It is something to be aware of.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #26
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    This is my new output:
    Java Code:
    nullBread.............	3.99
    Cheese.............	2.5
    Meat.............	8.89
    Juice.............	4.67
    
    __________________________
    Total Price is.....$20.05
    
    The number of items being purchased........4
    The average price per item is........$5.01
    
    Amount tendered is......$25.00
    Your change is.....$4.95
    The amount of change given is.......
    Quarters:$4.95
    Dimes:$4.95
    Nickels:$4.95
    Pennies:$0.95
    Im confused why Nickels and Dimes are the same as Quarters? I understand this isn't what output im looking for, im still working on getting it to give quarters then dimes when you cant give quarters and so on.
    Last edited by sithclone3; 06-29-2014 at 12:41 AM.

  7. #27
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Not getting correct output.

    Well, that's not all that is wrong. You should only give at most 4 pennies (.04), 1 nickel, etc. I don't know how you are doing this but you need to see how many times the change is divided by .25. That will give the number of quarters. In this case, it might be better to work with integers. Let's say that you want to give someone $2.47 in change. 247 divided by 100 is 2 so you give them 2 dollars. Then 247 - 200 = 47 so you divide by 25 which is 1 and you give them 1 quarter. 47 - 1*25 = 22 so you divide 22 by 10 and give them 2 dimes. 22 - 2 * 10 = 2. No nickels obviously so 2 is left and that will be pennies. You can then convert back to decimals if you like. Check out the % operator. It should help. Like other things you should play around with this concept in a separate program before you add it to your main program.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #28
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    Everything is just about fixed, except the fact that it wont calculate the change. Im not sure why. Im pretty sure the code I wrote is correct, but its not implementing the makeChange into its equations.

    This is my output:
    Java Code:
    nullBread.............	3.99
    Cheese.............	2.45
    Juice.............	7.99
    Jello.............	4.45
    
    __________________________
    Total Price is.....$18.88
    
    The number of items being purchased........4
    The average price per item is........$4.72
    
    Amount tendered is......$25.00
    Your change is.....$6.12
    
    The amount of change given is:
    Quarters:$0.00
    Dimes:$0.00
    Nickels:$0.00
    Pennies:$0.00
    This is my new mainClass:

    Java Code:
    package assignment4;
    
    /**
     *
     * @author sprat009
     */
    import javax.swing.JOptionPane;
    
    public class Cashier {
        
        int dollars, cents;
        int quarter = 25;
        int dime = 10;
        int nickel = 5;
        int penny = 1;
        int totalQuarter, totalDime, totalNickel, totalPenny, remainingCents;
    
        String name, s;
        double price;
        double totalPrice;
        int count;
        String receipt;
        double tendered;
        double average;
        double makeChange;
    
        Cashier() {
            name = " ";
            s = " ";
            totalPrice = 0;
            count = 0;
    
            totalQuarter = cents / quarter;
            remainingCents = cents % quarter;
            totalDime = remainingCents / dime;
            remainingCents = remainingCents % dime;
            totalNickel = remainingCents / nickel;
            remainingCents = remainingCents % nickel;
            totalPenny = remainingCents / penny;
            
            makeChange = tendered - totalPrice;
            dollars = (int)makeChange;
            cents = (int)(makeChange * 100) - (dollars * 100);
        }
    
        public void add(String name, double price) {
            receipt += name + ".............\t" + price + "\r\n";
            totalPrice += price;
            count++;
            JOptionPane.showMessageDialog(null, "Product is " + name + "\tPrice $" + price,
                    "My Super Store", JOptionPane.INFORMATION_MESSAGE);
    
            System.out.println("Product is " + name + "\tPrice is: $" + price);
        }
    
        public void makeChange() {
            makeChange = tendered - totalPrice;
        }
    
        public void tendered(double amount) {
            tendered += amount;
        }
    
        public double getAverage() {
            return average = totalPrice / count;
        }
    }
    This is the part in the testClass i implement to get the change, but i still get $0.00:

    Java Code:
    Area text = new JTextArea(c.receipt + "\n" + "__________________________" + "\n" + "Total Price is....." + nf.format(c.totalPrice) + 
                    "\n\n" + "The number of items being purchased........" + c.count + 
                    "\n" + "The average price per item is........" + nf.format(c.average) +  "\n\n" + 
                    "Amount tendered is......" + nf.format(c.tendered) + "\n" + "Your change is....." + nf.format(c.makeChange) + "\n\n" + 
                    "The amount of change given is:" + "\n" + "Quarters:" + nf.format(c.totalQuarter) + "\n" + "Dimes:" + nf.format(c.totalDime) + 
                    "\n" + "Nickels:" + nf.format(c.totalNickel) + "\n" + "Pennies:" + nf.format(c.totalPenny), 20, 30);
    Last edited by sithclone3; 06-29-2014 at 04:48 AM. Reason: clarify

  9. #29
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default Re: Not getting correct output.

    check your System.out.println(c.getAverage);...

    its wrong syntatically ..its should be System.out.println(c.getAverage())
    Ramya:cool:

  10. #30
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    Alright, I fixed it syntatically. I still can't get what Im looking for, which is the change (I.E: # of quarter, # of nickels, # pennies, # of dollars...) they all come up as 0.00

    EX: Heres my output;
    Java Code:
    nullBread.............	3.99
    Cheese.............	2.95
    Juice.............	7.89
    Penuts.............	3.55
    
    __________________________
    Total Price is.....$18.38
    
    The number of items being purchased........4
    The average price per item is........$4.59
    
    Amount tendered is......$20.00
    Your change is.....$1.62
    
    The amount of change given is:
    Dollars:0
    Quarters:0
    Dimes:0
    Nickels:0
    Pennies:0
    Last edited by sithclone3; 06-29-2014 at 06:56 PM. Reason: give example of my output

  11. #31
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Not getting correct output.

    You are doing all your change calculations in your constructor before anything has been computed. That will never work. You should use a separate method to calculate the various change options. This method should be called after you have tendered a certain amount of money and the change has been computed. And remember, if you want to calculate just loose pocket change then you need to programmatically ignore any non-zero digit to the left of your change decimal point. So $2.87 in change would be 2 dollars(US) plus $.87 in various coinage. So essentially, ignore the 2. The coinage, I believe, is what you want. Although you could extend it to include $20 bills, tens, fives, etc.

    But you need to start playing computer by hand. Run the code sequentially in your head by reading the source code (and using a pencil to write down variable values). If something looks like it is computed before it should be then you have a problem. Doing this in this manner is
    almost mandatory for new programmers until they get a feel for the code they are writing or just coding in general.

    Regards,
    Jim
    Last edited by jim829; 06-29-2014 at 07:19 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #32
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    Dear Jim,

    Would you be able to give me an example of iimplementing makeChange into the coinage method? I have to have the equations in the MainClass in order to use them, but everytime I put something referencing them into the TestClass is get a syntax error.

    Please Ive been stuck on this part for like 15hrs and its the last thing I need finished...We never went over this in class, my professor never responds to emails. Im freaking out over this one part, I at least knew somewhat of what to do for the other parts of this program...

    UPDATE: I HAVE GOTTEN DOLLARS! IT GAVE ME THE AMOUNT OF DOLLARS! IM EXCITED! I THINK IM ON THE RIGHT TRACK!

    This is my new output:
    Java Code:
    nullBread.............	3.99
    Cheese.............	2.95
    Juice.............	6.79
    Penuts.............	0.99
    
    __________________________
    Total Price is.....$14.72
    
    The number of items being purchased........4
    The average price per item is........$3.68
    
    Amount tendered is......$20.00
    Your change is.....$5.28
    
    The amount of change given is:
    Dollars:5
    Quarters:0
    Dimes:0
    Nickels:0
    Pennies:0
    UPDATE TO MY UPDATE: I HAVE FINISHED IT! I COMPLETED IT! I GOT IT! IT DOES IT! IT WORKS! ITS ALIVE! ITS AAAALLLIVEEE!!!

    output:
    Java Code:
    nullBread.............	3.99
    Juice.............	6.95
    Cheese.............	2.95
    Penuts.............	1.95
    
    __________________________
    Total Price is.....$15.84
    
    The number of items being purchased........4
    The average price per item is........$3.96
    
    Amount tendered is......$20.00
    Your change is.....$4.16
    
    The amount of change given is:
    Dollars:4
    Quarters:0
    Dimes:1
    Nickels:1
    Pennies:1
    Last edited by sithclone3; 06-29-2014 at 08:23 PM.

  13. #33
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Not getting correct output.

    I added the calculate method:

    Java Code:
    package assignment4;
    
    /**
     *
     * @author sprat009
     */
    import javax.swing.JOptionPane;
    
    public class Cashier {
        
        int dollars, cents;
        int quarter = 25;
        int dime = 10;
        int nickel = 5;
        int penny = 1;
        int totalQuarter, totalDime, totalNickel, totalPenny, remainingCents;
    
        String name, s;
        double price;
        double totalPrice;
        int count;
        String receipt;
        double tendered;
        double average;
        double makeChange;
        
    
        Cashier() {
            name = " ";
            s = " ";
            totalPrice = 0;
            count = 0;
    
            
        }
    
        public void add(String name, double price) {
            receipt += name + ".............\t" + price + "\r\n";
            totalPrice += price;
            count++;
            JOptionPane.showMessageDialog(null, "Product is " + name + "\tPrice $" + price,
                    "My Super Store", JOptionPane.INFORMATION_MESSAGE);
    
            System.out.println("Product is " + name + "\tPrice is: $" + price);
        }
        public void calculate(){
    dollars = (int)makeChange;
            cents = (int)(makeChange*100) - (dollars*100);
    
        totalQuarter = cents/quarter;
            remainingCents = cents%quarter;
            totalDime = remainingCents/dime;
            remainingCents = remainingCents%dime;
            totalNickel = remainingCents/nickel;
            remainingCents = remainingCents%nickel;
            totalPenny = remainingCents/penny;
            
            
    }
    
        public void makeChange() {
            makeChange = tendered - totalPrice;
        }
    
        public void tendered(double amount) {
            tendered += amount;
        }
    
        public double getAverage() {
            return average = totalPrice / count;
        }
    }
    Last edited by sithclone3; 06-29-2014 at 08:25 PM. Reason: I moved dollars= (int)makeChange and cents = (int)(makeChange*100) - (dollars*100) to before totalQuarters.

  14. #34
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Not getting correct output.

    Congratulations! I was about to tell you to move that code but you figured it out yourself. Well done!

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. How to set layout to have correct output
    By Prajin in forum AWT / Swing
    Replies: 6
    Last Post: 07-07-2010, 03:40 PM
  2. Getting correct output
    By WarmRegards in forum New To Java
    Replies: 9
    Last Post: 11-01-2009, 04:41 PM
  3. Is this the correct Output?
    By Teny in forum New To Java
    Replies: 17
    Last Post: 04-13-2009, 12:52 PM
  4. Output correct grammar
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 12:22 AM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM

Posting Permissions

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