英文:
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.
- Player.java (abstract base class)
- Tax.java (interface)
- PlayerIncome.java (extends player implements Tax) + income variable
- GradeBonus.java (interface)
- TournamentIncome.java (extends player implements Tax)
- 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
以下是您要翻译的内容:
- PlayerIncome doesn't belong to Player, so don't extend Player, also TournamentIncome.
- 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:
- PlayerIncome doesn`t belong to Player, so don`t extend Player, also TournamentIncome.
- 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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论