Click to See Complete Forum and Search --> : Assistance Needed : Calculating Tax Program


carrieoke13
05-10-2006, 05:57 PM
Hi there,
I am extremely new at Java, and I'm working on a program that will calculate estimated tax. I think I have everything right, but I can't figure out how to get the information calculated in TaxReturn.java (see below) to print out. I'm going to post both Tax.java and TaxReturn.java for your review. Can you help? I'm sure this is not very well written, but I'm learning!

tax.java :

import javax.swing.*;

public class Tax
{ static final String SINGLE = "S";
static final String MARRIED = "M";


public static void main(String[] args) throws Exception
{

String inputIncome;
double inputNumber;
String status;
TaxReturn = aTaxReturn;

inputIncome = JOptionPane.showInputDialog(null, "Enter your annual income with no commas or decimals");
inputNumber = Double.parseDouble(inputIncome);

JOptionPane.showMessageDialog(null, "Your annual income is " + inputNumber);
status = JOptionPane.showInputDialog(null, "Enter your marital status: S for single, M for married");

JOptionPane.showMessageDialog(null, "Your status is " + status);


JOptionPane.showMessageDialog(null, "Your estimated tax is " + TaxReturn.getTaxRate());

System.exit(0);

}

}


TaxReturn.java

public class TaxReturn
{
private double inputIncome;
private double estimTaxRate;
private double taxRate;
private String status;



public TaxReturn(double annualIncome, String maritalStatus)

{ inputIncome = annualIncome;
status = maritalStatus;



if(maritalStatus.equals("S") && annualIncome<= 10000)
taxRate = .15;
if(maritalStatus.equals("S") && annualIncome > 10000)
taxRate = .30;
if(maritalStatus.equals("M") && annualIncome < 20000)
taxRate = .15;
if(maritalStatus.equals("M") && annualIncome >=20000)
taxRate = .30;


estimTaxRate = taxRate * annualIncome;

}
public double getTaxRate()
{
return estimTaxRate;
}

}

thanks for any help!
Carrie

Cytael
05-14-2006, 11:38 PM
Please put your code inside [ php ] brackets when posting it on the boards. It makes it much easier to read.

I'm going to warn you before you read further that understanding the concepts you're lacking in this example is key to many, many things in Java. If you want to become better, I suggest you try to figure out the problems without my help. However, if you still want someone to do your homework for you, scroll down ^_^.

To make your code work, you need to properly call the TaxReturn constructor. Tax.java should read like so:
import javax.swing.*;

public class Tax
{
//you don't need those two static String declarations, and you weren't using them anywhere, anyway

public static void main(String[] args) throws Exception
{

String inputIncome;
double inputNumber;
String status;
myTaxReturn = null; // <-- note this change

inputIncome = JOptionPane.showInputDialog(null, "Enter your annual income with no commas or decimals");
inputNumber = Double.parseDouble(inputIncome);

JOptionPane.showMessageDialog(null, "Your annual income is " + inputNumber);
status = JOptionPane.showInputDialog(null, "Enter your marital status: S for single, M for married");

JOptionPane.showMessageDialog(null, "Your status is " + status);

myTaxReturn = new TaxReturn(inputNumber, status); // <-- an addition here
double theTax = myTaxReturn.getTaxRate(); // <-- and here, for clarity

JOptionPane.showMessageDialog(null, "Your estimated tax is " + theTax); //<-- also changed here for clarity

System.exit(0);

}

}