[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

static get method...

Posted on 2011-10-26
5
Medium Priority
?
292 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Mike Eghtebas
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 37035548
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?
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 37035564
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

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 37035603
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.
0
 
LVL 11

Accepted Solution

by:
dougaug earned 2000 total points
ID: 37035612
See if this is what you are looking for:
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);
        
        // 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 static double taxableIncome;
    
    public Tax(){}
    
    public Tax(int status, int[][] bracket, double []rate){
        this.status=status;
        this.bracket=bracket;
        this.rates=rate;
    }
    
    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 static void setTaxableIncome(double taxableIncome){
        Tax.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

0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 37035737
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question