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

x
Solved

# static get method...

Posted on 2011-10-26
Medium Priority
292 Views
The following code calculates income tax using Tax() object:

run:
0- single
1- married jointly
2- married separately
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"
+ "\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);
}
}
``````
0
Question by:Mike Eghtebas
• 2
• 2

LVL 13

Expert Comment

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

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

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
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

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

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"
+ "\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);
}
}
``````
0

LVL 34

Author Comment

ID: 37035737
0

## Featured Post

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…