Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

static get method...

The following code calculates income tax using Tax() object:

run:
0- single
1- married jointly
2- married separately
3- head of household
Enter the filling status:
0
Enter the taxable income:
400000
Tax in 2001 is: 113122.5
Tax in 2009 is: 117683.5

-------------------
Question: How can I use only the first line (commented here) to replace lines 2 and 3?

//        Tax.setTaxableIncome(income);
        tax2001.setTaxableIncome(income);
        tax2009.setTaxableIncome(income);

I tried to change to

private static double taxableIncome;

    public static void setTaxableIncome(double taxableIncome){
        this.taxableIncome= taxableIncome;
    }

but it errors.

Thank you
import java.util.Scanner;

public class TestTaxClass {
    
    static int [][] brackets2001 = {
    {27050, 65550, 136750, 297350, 0},
    {45200, 109250, 166500, 297350, 0},
    {22600, 54625, 83250, 148675, 0},
    {36250, 93650, 151650, 297350, 0},
    };
    
    static int [][] brackets2009 = {
    {8350, 33950, 82250, 171550, 372950},
    {16700, 67900, 137050, 208850, 372950},
    {8350, 33950, 82250, 104425, 186475},
    {11950, 45500, 117450, 190200, 372950},
    };
    
    static double [] rates={0.10, 0.15, 0.25, 0.28, 0.33, 0.35};            
    
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
      
        // prompt user to enter filing status
        System.out.println(
                "0- single"
            + "\n1- married jointly"
            + "\n2- married separately"
            + "\n3- head of household"
            + "\nEnter the filling status:");
        int status=input.nextInt();
        
        // prompt user to enter taxable income
        System.out.println("Enter the taxable income:");
        double income=input.nextDouble();
        
        Tax tax2001=new Tax(status, brackets2001, rates);
        Tax tax2009=new Tax(status, brackets2009, rates);
        
//        Tax.setTaxableIncome(income);
        tax2001.setTaxableIncome(income);
        tax2009.setTaxableIncome(income);
        
        // display the result
        System.out.println("Tax in 2001 is: " + 
                (int)(tax2001.getTax()*100)/100.0+ "\n"+
                 "Tax in 2009 is: " + 
                (int)(tax2009.getTax()*100)/100.0+ "\n");

    }             
}

class Tax {
    private int status;
    private int[][] bracket;
    private double[] rates;
    private double taxableIncome;
    
    public Tax(){}
    
    public Tax(int status, int[][] bracket, double []rate){
        this.status=status;
        this.bracket=bracket;
        this.rates=rate;
//        this.taxableIncome=taxableIncome;
    }
    
    public int getFilingStatud(){
        return status;
    }
    
    public void setFilingStatud(int status){
        this.status= status;
    }
    
    public int[][] getBracket(){
        return bracket;
    }
    
    public void setBracket(int[][] bracket){
        this.bracket= bracket;
    }
    
    public double[] getRates(){
        return rates;
    }
    
    public void setRates(double[] rates){
        this.rates= rates;
    } 
    
    public double getTaxableIncome(){
        return taxableIncome;
    }
    
    public void setTaxableIncome(double taxableIncome){
        this.taxableIncome= taxableIncome;
    } 
    
    public double getTax(){
       return bracket[status][0]*rates[0]+
       ((taxableIncome>bracket[status][1])?
       (bracket[status][1]-bracket[status][0])*rates[1]:0)+
       
       ((taxableIncome>bracket[status][2])?
       (bracket[status][2]-bracket[status][1])*rates[2]:0)+
               
       ((taxableIncome>bracket[status][3])?        
       (bracket[status][3]-bracket[status][2])*rates[3]:0)+
               
       ((taxableIncome>bracket[status][4])?        
       (bracket[status][4]-bracket[status][3])*rates[4]:0)+
               
       ((taxableIncome>bracket[status][4])?     
       (taxableIncome-bracket[status][4])*rates[5]:0);
    }
}

Open in new window

Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

I could be wrong but I don't see how it could work.  You don't have an object called Tax.

What is your goal?

This appears to be for a class.  Is it?  If so, what's the assignment.  It really would help to have some context.  If not, context still helps.  What are you trying to accomplish?
Avatar of Mike Eghtebas

ASKER

line 53 is start of Tax(0 sub class:

class Tax {
    private int status;
    private int[][] bracket;
    private double[] rates;
    private double taxableIncome;

it works as it is. When you run, it gives:

run:
0- single
1- married jointly
2- married separately
3- head of household
Enter the filling status:
0                                 <-- from the menu above it you make a selection
Enter the taxable income:
400000                        <-- you enter income

Tax in 2001 is: 113122.5  <-- you get tax amount
Tax in 2009 is: 117683.5

Here, instead of having two lines like:

        tax2001.setTaxableIncome(income);
        tax2009.setTaxableIncome(income);

I can have only one line based on how a static method works:

      Tax.setTaxableIncome(income);

I wanted to accomplish just this.

Mike

I don't think you can do it that way because Tax isn't an object.

I could be wrong.  Let's wait around to see if anyone else joins in to help.
ASKER CERTIFIED SOLUTION
Avatar of dougaug
dougaug
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial