I'm not sure why it's not calculating right. When I enter a negative number, the if else statement or something does not register it's a negative number. There is a before and after, and you can see where it does not register negative numbers.

Thanks in advance for your help!

import javax.swing.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.applet.*;
import java.awt.*;
public class Macro extends Applet{

    double C = 6000;
    double Cchange;
    double I = 2000;
    double Ichange;
    double G = 2000;
    double Gchange=0;
    double exports = 1000;
    double Exchange;
    double imports = 1000;
    double Imchange;
    double NX = exports - imports;
    double NXchange;
    double T = 2000;
    double Tchange;
    double GDP;
    double GDPchange;
    double Y = 8000;

    DecimalFormat dpoint2 = new DecimalFormat("0.00");
    NumberFormat dollar = NumberFormat.getCurrencyInstance();

    public void init() {

    double a;
    double b;
    double c;
    double d;
    double e;
    double f;

    String aVal, bVal, cVal, dVal, eVal, fVal;

    //   Input values

    aVal = JOptionPane.showInputDialog("Enter the change in the interest rate (1 = 1% increase -1 = 1% decrease)");
    bVal = JOptionPane.showInputDialog("Enter the elasticy of Investment in response to a change in the interest (e,g, 10 = 10%)");
    cVal = JOptionPane.showInputDialog("Enter the change in government spending (100 = $100 increase -100 = $100 decrease)" );
    dVal = JOptionPane.showInputDialog("Enter the change in government taxes (100 = $100 increase -100 = $100 decrease)");
    eVal = JOptionPane.showInputDialog("Enter the change exports (100 = $100 increase -100 = $100 decrease)");
    fVal = JOptionPane.showInputDialog("Enter the change in imports (100 = $100 increase -100 = $100 decrease)");
    //   Convert from a String to a double

    a = Double.parseDouble(aVal);
    b = Double.parseDouble(bVal);
    c = Double.parseDouble(cVal);
    d = Double.parseDouble(dVal);
    e = Double.parseDouble(eVal);
    f = Double.parseDouble(fVal);

    GDP = C + I + G + NX;

    if(c < 0) {
      Gchange = (G - c);
    } else {
      Gchange = (G + c);
    }

    if(d < 0) {
      Tchange = (T - d);
    } else {
      Tchange = (T + d);
    }   

    if(e >= 0) {
      Exchange = (exports + e);
    } else {
      Exchange = (exports - e); 
        }

    if(f < 0) {
      Imchange = (imports - f);
    } else {
      Imchange = (imports + f); 
    }
    if(a > 0) {
     Ichange = (I - ((a*b/100)*I));
    }else {
     Ichange = (I + ((a*(-b)/100)*I));
    }

    Cchange = Y - Tchange;
    NXchange = Exchange - Imchange;

     GDPchange = Cchange + Ichange + Gchange + NXchange;

    }

    public void paint(Graphics g){
    Graphics2D g2 = (Graphics2D) g;

    super.paint(g);

    g.drawRect(15, 10, 530, 150);
    g.drawLine (280, 10, 280,160);

    g.drawString("Initial Consumption: " +dollar.format(C), 25, 25);
    g.drawString("Initial Income: " +dollar.format(Y), 35, 40);
    g.drawString("Initial Taxes: " +dollar.format(T), 35, 55);
    g.drawString("Initial Investment: " +dollar.format(I), 25, 70);
    g.drawString("Initial Government Spending:" +dollar.format(G), 25, 85);
    g.drawString("Initial Exports: " +dollar.format(exports), 25, 100);
    g.drawString("Initial Imports: " +dollar.format(imports), 25, 115);
    g.drawString("Initial GDP: " +dollar.format(GDP), 25, 150);

    g2.setColor(Color.BLUE);
    g.drawString("New Consumption: " +dollar.format(Cchange), 300, 25);
    g.drawString("New Income: " +dollar.format(Y), 310, 40);
    g.drawString("New Taxes: " +dollar.format(Tchange), 310, 55);
    g.drawString("New Investment: " +dollar.format(Ichange), 300, 70);
    g.drawString("New Government Spending: " +dollar.format(Gchange), 300, 85);
    g.drawString("New Exports: " +dollar.format(Exchange), 300, 100);
    g.drawString("New Imports: " +dollar.format(Imchange), 300, 115);
    g.drawString("New GDP: " +dollar.format(GDPchange), 300, 150);

    }
    }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

It does do the job, but your logic is flaw.

// example
if(e >= 0) {
Exchange = (exports + e);
} else {
Exchange = (exports - e);
}

Look at your code above. Let say Exchange is 0. Now when e is 10, the result for Exchange is 0+10 => 10. Now when e is -10, the result for Exchange is 0-(-10) => 10. Do you see what I mean? In other words, you do NOT need to check if the variable is positive or negative. You just do addition normally.

Exchange = exports + e;
This article has been dead for over six months. Start a new discussion instead.