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 3 of 3

Thread: Math/Programming HELP!

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Math/Programming HELP!

    I recently started learning java, and decided to test my programming capabilities. Have you ever heard of the "If you pick up a penny one day then twice the pennies as the day before on the next day, you'd have over a million dollars in a month" ? Well I decided to make a gui to demonstrate. How it works is you enter the amount of days, then it displays how many dollars you have.

    this is my code, please tell me what I'm doing wrong to not get the correct answer

     package gui;
     
     
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
     
     
     
    public class DoublePennyProject extends JFrame
     
    {
     
        private static final int WIDTH = 500;
     
        private static final int HEIGHT = 300;
     
     
     
        private JLabel DayL, DollarL;
     
        private JTextField DayTF, DollarTF;
     
        private JButton calculateB;
     
     
     
     
     
        private CalculateButtonHandler cbHandler;
     
        private ExitButtonHandler ebHandler;
     
     
     
        public DoublePennyProject()
     
        {
     
            DayL = new JLabel("Enter the amount of days: ", SwingConstants.RIGHT);
     
            DollarL = new JLabel("You have this many dollars: ", SwingConstants.RIGHT);
     
     
     
            DayTF = new JTextField(10);
     
            DollarTF = new JTextField(10);
     
     
     
     
     
            calculateB = new JButton("Calculate");
     
            cbHandler = new CalculateButtonHandler();
     
            calculateB.addActionListener(cbHandler);
     
     
     
     
     
            setTitle("Twice the Pennies");
     
            Container pane = getContentPane();
     
            pane.setLayout(new GridLayout(4, 2));
     
     
     
     
     
            pane.add(DayL);
     
            pane.add(DayTF);
     
            pane.add(DollarL);
     
            pane.add(DollarTF);
     
            pane.add(calculateB);
     
     
     
     
     
            setSize(WIDTH, HEIGHT);
     
            setVisible(true);
     
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
        }
     
     
     
        private class CalculateButtonHandler implements ActionListener
     
        {
     
            public void actionPerformed(ActionEvent e)
     
            {
            	double Day = 0;
                double  Dollar = 0;
               while(Day<63){
     
     
                	 Day=Day+1;
                     Dollar = (Day*2)/100;
               }
     
     
     
                Day = Double.parseDouble(DayTF.getText()); 
     
                 Dollar = Math.pow(Day, Dollar);
     
                DollarTF.setText(":: " +Dollar);
     
            }
     
        }
     
     
     
        public class ExitButtonHandler implements ActionListener
     
        {
     
            public void actionPerformed(ActionEvent e)
     
            {
     
                System.exit(0);
     
            }
     
        }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,002
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Math/Programming HELP!

    what I'm doing wrong to not get the correct answer
    Please explain. Post the program's output and add some comments saying what is wrong with the output.

    If needed add some println() statements that print out the values of variables as their values change.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: Math/Programming HELP!

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Do you have another class with a main() method that tests what you've posted? I added a main() method to what you posted and the GUI displays and does something, but I'm not sure if that's your problem. Provide the details Norm has asked for, and we can help.

    Is your code double spaced on purpose? I like appropriate white space in source code to improve readability, but the extra linefeeds in yours are unnecessary.

Similar Threads

  1. Question about math.pow and math.sqrt
    By ggx7 in forum Java Theory & Questions
    Replies: 7
    Last Post: March 7th, 2014, 11:51 AM
  2. Math Fromula y=x2+6x+5 how to convert to Java Programming code
    By FreeMan0001 in forum Algorithms & Recursion
    Replies: 10
    Last Post: October 18th, 2013, 10:14 AM
  3. [SOLVED] Help with Math.tan and Math.atan
    By Dr.Code in forum Algorithms & Recursion
    Replies: 6
    Last Post: July 2nd, 2012, 05:54 AM
  4. Replies: 0
    Last Post: December 12th, 2011, 02:17 PM
  5. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM