public class Tax { int numberOfDepends; double grossIncome; String state; public double calcTax(){ // отобразить результат этого метода if(grossIncome < 50000){ return grossIncome * 0.06; } else { return grossIncome * 0.08; } } } public class NjTax extends Tax { public double adjustForStudents(double tax){ return tax - 500; } public double calcTax(){ super.calcTax(); if(grossIncome < 50000){ return grossIncome * 0.10; } else { return grossIncome * 0.13; } } } public class TestTax { public static void main(String[] args) { NjTax t = new NjTax(); t.grossIncome = 40000; t.numberOfDepends = 2; t.state = "NY"; double yourTax = t.calcTax(); double njt = t.adjustForStudents(yourTax); System.out.println("Your tax is " + njt); } } - Print to console? - Yuriy SPb ♦
- onedouble result = super.calcTax (); System.out.println (result); - Artem Konovalov
- Yes, output to the console - khuAn
- double result = super.calcTax (); System.out.println (result); doesn't work in main method - "Cannot use super in a static context" - khuAn
|
1 answer
Your code can be implemented, but as I understand it, you are trying to call the result from the static context of the main method, which is the problem. Initializing everything correctly, the problem will go away. (By the way, my code is not much different from yours.)
public class Main { public static void main( String[] args ) { Main main = new Main(); NjTax tax = main.createNjTax(); double res = tax.calcTax(); System.out.println( "Not super calc=" + res ); } public NjTax createNjTax() { NjTax tax = new NjTax(); tax.numberOfDepends = 1; tax.grossIncome = 2; return tax; } } class Tax { int numberOfDepends; double grossIncome; String state; public double calcTax(){ return grossIncome < 50000 ? grossIncome * 0.06 : grossIncome * 0.08; } } class NjTax extends Tax { public double adjustForStudents(double tax){ return tax - 500; } public double calcTax(){ double res = super.calcTax(); System.out.println( "Super calc=" + res ); return grossIncome < 50000 ? grossIncome * 0.10 : grossIncome * 0.13; } } - Thanks for the example. I understand that the example of my code cannot be implemented. I would like to understand whether it is possible to present it more gracefully and correctly? - khuAn
- Yes, I call through the main. The problem is really that the implementation seems to be there, but there is no output. I will parse your code. I want to reach an understanding of the realization of my ... - khuAn
|