如何在TournamentIncome.java的calculateGradeBonus()方法中访问income变量?

huangapple go评论64阅读模式
英文:

How can I access income variable in calculateGradeBonus() method of TournamentIncome.java?

问题

以下是已翻译的部分:

application consist of the following files.
1. Player.java (abstract base class)
2. Tax.java (interface)
3. PlayerIncome.java (extends player implements Tax) + income variable
4. GradeBonus.java (interface)
5. TournamentIncome.java (extends player implements Tax)
6. PlayerTest.java

Task:
Create a new instance of class TournamentIncome and store the details in the three parameters name, grade, and rate. Now, create a method calculateGradeBonus() to calculate the bonus of each player based on his grade. Create a method displayDetails() to display the details about each player. Here, calculate the income of the player using his income and the bonus amount he will be given as per his grade.

public class PlayerIncome extends Player implements Tax {
    double income;
    double tax;

    public PlayerIncome(String name, double income) {
        super(name);
        this.income = income;
    }

    @Override
    public void calculateTax() {
        tax = income * (TAX_PERCENT / 100);
    }

    @Override
    void displayDetails() {
        System.out.println("Player Name: " + name);
        System.out.println("Income: " + income);
        System.println("Tax: " + tax);
    }
}

Income is an instance variable of PlayerIncome Class.

public class TournamentIncome extends Player implements GradeBonus {
    String grade;
    int rate;

    public TournamentIncome(String name, String grade, int rate) {
        super(name);
        this.grade = grade;
        this.rate = rate;
    }

    @Override
    public void calculateGradeBonus() {

    }

    @Override
    void displayDetails() {


    }

I want to use that income variable in TournamentIncome Class for calculating Bonus

public abstract class Player {
    String name;

    public Player(String name) {
        this.name = name;
    }

    abstract void displayDetails();
}
英文:

application consist of the
following files.

  1. Player.java (abstract base class)
  2. Tax.java (interface)
  3. PlayerIncome.java (extends player implements Tax) + income variable
  4. GradeBonus.java (interface)
  5. TournamentIncome.java (extends player implements Tax)
  6. PlayerTest.java

Task:
Create a new instance of class TournamentIncome and store the details in the three parameters
name, grade and rate. Now, create a method calculateGradeBonus() to calculate the bonus of
each player based on his grade. Create a method displayDetails() to display the details about each
player. Here, calculate the income of the player using his income and the bonus amount he will
be given as per his grade.

public class PlayerIncome extends Player implements Tax {
double income;
double tax;
public PlayerIncome(String name, double income) {
super(name);
this.income = income;
}
@Override
public void calculateTax() {
tax = income * (TAX_PERCENT / 100);
}
@Override
void displayDetails() {
System.out.println("Player Name: " + name);
System.out.println("Income: " + income);
System.out.println("Tax: " + tax);
}

}

Income is an instance variable of PlayerIncome Class.

public class TournamentIncome extends Player implements GradeBonus {
String grade;
int rate;
public TournamentIncome(String name, String grade, int rate) {
super(name);
this.grade = grade;
this.rate = rate;
}
@Override
public void calculateGradeBonus() {
}
@Override
void displayDetails() {
}

I want to use that income variable in TournamentIncome Class for calculating Bonus

public abstract class Player {
String name;
public Player(String name) {
this.name = name;
}
abstract void displayDetails();

}

答案1

得分: 0

以下是您要翻译的内容:

  1. PlayerIncome doesn't belong to Player, so don't extend Player, also TournamentIncome.
  2. we can use return value of a method for further usage, like you don't have to keep a tax field(I think you have to initialize tax field before you access it, if you want to initialize tax field, then do it in constructor of PlayerIncome class)
public abstract class Player {
    String name;

    public Player(String name) {
        this.name = name;
    }

    abstract void displayDetails();
}
public class PlayerIncome implements Tax {

    Player player;
    double income;

    public PlayerIncome(Player player, double income) {
        this.player = player;
        this.income = income;
    }

    @Override
    public double calculateTax() {
        return income * (TAX_PERCENT / 100);
    }

    @Override
    void displayDetails() {
        System.out.println("Player Name: " + player.name);
        System.println("Income: " + income);
        System.println("Tax: " + calculateTax());
    }
}
public class TournamentIncome implements GradeBonus {

    PlayerIncome playerIncome;
    String grade;
    int rate;

    public TournamentIncome(PlayerIncome playerIncome, String grade, int rate) {
        this.playerIncome = playerIncome;
        this grade = grade;
        this.rate = rate;
    }

    @Override
    public int calculateGradeBonus() {
        if (this.grade == "A" && rate == 10) {
            return (int)this.playerIncome.income * 10 / 100;
        }
        return (int)this.playerIncome.income * 5 / 100;
    }

    @Override
    void displayDetails() {
        System.out.println("Player Name: " + playerIncome.player.name);
        System.println("FinalIncome: " + calculateGradeBonus());
        System.println("Tax: " + playerIncome.calculateTax());
        System.println("Grade: " + grade);
        System.println("rate: " + rate);
    }
}
英文:

I changed it, hope it helps:

Something I noticed:

  1. PlayerIncome doesn`t belong to Player, so don`t extend Player, also TournamentIncome.
  2. we can use return value of a method for further usage, like you don`t have to keep a tax field(I think you have to initialize tax field before you access it, if you want to initialize tax field, then do it in constructor of PlayerIncome class)
public abstract class Player {
String name;
public Player(String name) {
this.name = name;
}
abstract void displayDetails();
}
public class PlayerIncome implements Tax {
Player player;
double income;
public PlayerIncome(Player player, double income) {
this.player = player;
this.income = income;
}
@Override
public double calculateTax() {
return income * (TAX_PERCENT / 100);
}
@Override
void displayDetails() {
System.out.println("Player Name: " + player.name);
System.out.println("Income: " + income);
System.out.println("Tax: " + calculateTax());
}
}
public class TournamentIncome implements GradeBonus {
PlayerIncome playerIncome;
String grade;
int rate;
public TournamentIncome(PlayerIncome playerIncome, String grade, int rate) {
this.playerIncome = playerIncome;
this.grade = grade;
this.rate = rate;
}
@Override
public int calculateGradeBonus() {
if (this.grade == "A" && rate == 10) {
return (int)this.playerIncome.income * 10 / 100;
}
return (int)this.playerIncome.income * 5 / 100;;
}
@Override
void displayDetails() {
System.out.println("Player Name: " + playerIncome.player.name);
System.out.println("FinalIncome: " + calculateGradeBonus());
System.out.println("Tax: " + playerIncome.calculateTax());
System.out.println("Grade: " + grade);
System.out.println("rate: " + rate);
}
}

huangapple
  • 本文由 发表于 2023年3月9日 16:41:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定