DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    May 2006
    Posts
    1

    Question Easy question: public void paint (Graphics g) not reading variable changes

    I'm brand new to java (college class), and have a quick question. Instead of outputting the correct numbers to the Applet, it just lists each one as "0.0". What am I doing wrong? Thank you.

    P.S. I know I need to clean the code up a lot and make some other major changes, but I just need help on this particular problem at the moment.

    import javax.swing.*;
    import java.awt.Graphics;
    import java.applet.*;

    public class GDPmodel extends Applet {

    double GDP,GDPVal, TaxChange, MPC1, RChange, GChange1, NXC;
    double initialGDP;


    // Contructor

    public GDPmodel()
    {

    }

    public GDPmodel(double initGDP)
    {
    GDP = initGDP;
    initialGDP = initGDP;
    }


    // Methods
    public void NX (double NXchange)
    {
    NXC = NXchange;
    GDP = GDP + NXchange;
    }

    public void G (double Gchange)
    {
    GChange1 = Gchange;
    GDP = GDP + Gchange;
    }

    public void I (double R)
    {
    RChange = R;
    GDP = GDP - ((initialGDP/4)*(R/100));
    }

    public void C (double T, double MPC)
    {
    TaxChange = T;
    MPC1 = MPC;
    GDP = GDP+ ((MPC*initialGDP) * (initialGDP-(T*initialGDP)));
    }


    public void init ()
    //public static void main(String[] args)
    {

    double GDPVal, TaxChange, MPC, RChange, GChange, Import, Export = 0;
    String InitGDPVal, TaxVal, MPCVal, RVal, GVal, ImportVal, ExportVal;

    // Input values

    InitGDPVal = JOptionPane.showInputDialog("Enter initial GDP value in billions of dollars (to default to actual 2005 GDP, enter 0):" );
    TaxVal = JOptionPane.showInputDialog("Enter percentage change in tax rate:" );
    MPCVal = JOptionPane.showInputDialog("Enter the marginal propensity to consume:" );
    RVal = JOptionPane.showInputDialog("Enter the change in interest rate in percentage terms:" );
    GVal = JOptionPane.showInputDialog("Enter change in government spending in dollars:" );
    ImportVal = JOptionPane.showInputDialog("Enter change in dollar value of imports:" );
    ExportVal = JOptionPane.showInputDialog("Enter change in dollar value of exports:" );

    //Convert from a String to a double

    GDPVal = Double.parseDouble(InitGDPVal);
    TaxChange = Double.parseDouble(TaxVal);
    MPC = Double.parseDouble(MPCVal);
    RChange = Double.parseDouble(RVal);
    GChange = Double.parseDouble(GVal);
    Import = Double.parseDouble(ImportVal);
    Export = Double.parseDouble(ExportVal);

    if(GDPVal == 0.0)
    {
    GDPVal = 12370; //12.37 trillion is 2005 estimate from CIA factbook using PPP
    } else {}

    GDPmodel model = new GDPmodel(GDPVal);

    model.NX(Export-Import);
    model.G(GChange);
    model.I(RChange);
    model.C(TaxChange, MPC);

    }

    public void paint (Graphics g){

    super.paint(g);

    g.drawRect(15, 10, 400, 155);

    g.drawString("You entered an initial GDP of $ " + initialGDP + " billion.", 25, 25);
    g.drawString("You entered a tax percentage change of " + TaxChange + "%.", 25, 45);
    g.drawString("You entered a marginal propensity to consume of " + MPC1 + ".", 25, 65);
    g.drawString("You entered an interest rate change of " + RChange, 25, 85);
    g.drawString("You entered a change in government spending of $" + GChange1 + " billion.", 25, 105);
    g.drawString("You entered a net export change of $ " + NXC + " billion.", 25, 125);
    g.drawString("Your new GDP is $ " + GDP + " billion.", 25, 145);

    }

    }

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    In the future, use code tags.

    Your variables are all doubles, but JOptionPane.showInputDialog() returns a String. I'm not even sure how it lets you compile that. You need to parse the input string as a double.

    taxVal = Double.parseDouble(JOptionPane.showInputDialog("input some number"));

    Also, you shouldn't start variable names with a capital.

Similar Threads

  1. Replies: 3
    Last Post: 04-21-2006, 03:46 PM
  2. Image Mastering API Headache
    By Willy in forum .NET
    Replies: 2
    Last Post: 07-23-2003, 04:46 PM
  3. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  4. files
    By langevin in forum .NET
    Replies: 1
    Last Post: 09-13-2002, 08:57 PM
  5. GUI Refresh problem?
    By Lim Wing Hoe in forum Java
    Replies: 7
    Last Post: 11-17-2000, 02:12 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
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links