问题得以执行,但它未计算净工资。

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

The problem is got executed but it doesn't calculate the netpay

问题

import java.util.Scanner;

class Employee {
    public String Staffname;
    public int StaffID;
    public int BasicSalary;
    public int Allowances;

    public void Staff() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Staff name=");
        this.Staffname = sc.nextLine();
        System.out.println("Enter the Staff ID=");
        this.StaffID = sc.nextInt();
        System.out.println("Enter the Basic Salary");
        this.BasicSalary = sc.nextInt();
        System.out.println("Enter the Allowances");
        this.Allowances = sc.nextInt();
        System.out.println("***Employee Details Registered Successfully***");
        System.out.println("");
    }
}

class Marketing extends Employee {
    public int sales_incentives;
    public int net_pay;

    public void Staff() {
        System.out.println(super.BasicSalary);
        System.out.println(super.Allowances);
        this.net_pay = super.BasicSalary + super.Allowances + this.sales_incentives;
        System.out.println("Net pay=" + net_pay);
    }

    public static void main(String[] args) {
        Marketing m = new Marketing();
        Employee e = new Employee();
        Scanner sc = new Scanner(System.in);
        e.Staff();
        System.out.println("Enter the incentives=");
        m.sales_incentives = sc.nextInt();
        m.Staff();
    }
}

这是一个用于方法重写的程序。程序已经执行,但未计算净薪水。如何从父类中调用基本工资和津贴以计算净薪水。找不到错误。

英文:
import java.util.Scanner;
class Employee{
public String Staffname;
public int StaffID;
public int BasicSalary;
public int Allowances;	
public void Staff()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Staff name=");
this.Staffname=sc.nextLine();
System.out.println("Enter the Staff ID=");
this.StaffID=sc.nextInt();
System.out.println("Enter the Basic Salary");
this.BasicSalary=sc.nextInt();
System.out.println("Enter the Allowances");
this.Allowances=sc.nextInt();
System.out.println("***Employee Details Registered Successfully***");
System.out.println("");
}
}
class Marketing extends Employee{
public int sales_incentives;
public int net_pay;
public void Staff()
{
System.out.println(super.BasicSalary);
System.out.println(super.Allowances);
this.net_pay=super.BasicSalary+super.Allowances+this.sales_incentives;
System.out.println("Net pay="+net_pay);
}
public static void main(String[] args){
Marketing m=new Marketing();
Employee e=new Employee();
Scanner sc=new Scanner(System.in);
e.Staff();
System.out.println("Enter the incentives=");
m.sales_incentives= sc.nextInt();
m.Staff();
}
}

This is a program for overriding method.The program got Executed but isnt calculating the netpay.The output of the program How to call the Basic salary and allowance from the super to calculate the Net pay. Can't able to find the mistake.

答案1

得分: 0

以下是翻译好的内容:

如果您想调用父类方法,只需按照以下方式进行:

public void Staff()
{
    super.Staff();

    System.out.println(super.BasicSalary);
    System.out.println(super.Allowances);
    this.net_pay = super.BasicSalary + super.Allowances + this.sales_incentives;
    System.out.println("Net pay=" + net_pay);
}
英文:

If you want to call the super method, just do it this way:

public void Staff()
{
super.Staff();
System.out.println(super.BasicSalary);
System.out.println(super.Allowances);
this.net_pay=super.BasicSalary+super.Allowances+this.sales_incentives;
System.out.println("Net pay="+net_pay);
}

答案2

得分: 0

m对象未运行Employee::Staff(),因此BasicSalaryAllowances未定义。
Marketing::Staff()方法顶部添加super.Staff()。最好将激励查询也移到该方法中,这样您的抽象不会泄漏到main()中。

英文:

The m object did not run Employee::Staff(), so BasicSalary and Allowances are undefined.

Add super.Staff() to the top of Marketing::Staff() method. Better yet, move the incentive query into that method, too, so your abstraction doesn't leak into main().

huangapple
  • 本文由 发表于 2020年10月8日 13:32:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64256356.html
匿名

发表评论

匿名网友

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

确定