fork download
  1. // Employee.java
  2. class Employee {
  3. private String empID;
  4. private String empName;
  5. private double tax;
  6.  
  7. public void setID(String id) {
  8. this.empID = id;
  9. }
  10.  
  11. public void setName(String name) {
  12. this.empName = name;
  13. }
  14.  
  15. public void calTax(double salary, double bonus) {
  16. this.tax = (salary + bonus) * 0.09;
  17. }
  18.  
  19. public void showDetails() {
  20. System.out.println("Employee ID: " + empID);
  21. System.out.println("Employee Name: " + empName);
  22. System.out.println("Tax: " + tax);
  23. }
  24. }
  25.  
  26. // Student.java
  27. class Student {
  28. private String name;
  29. private int score;
  30.  
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34.  
  35. public void setScore(int score) {
  36. this.score = score;
  37. }
  38.  
  39. public String getName() {
  40. return name;
  41. }
  42.  
  43. public int getScore() {
  44. return score;
  45. }
  46. }
  47.  
  48. // GradStudent.java (Inheritance from Student)
  49. class GradStudent extends Student {
  50. private String advisor;
  51.  
  52. public void setAdvisor(String advisor) {
  53. this.advisor = advisor;
  54. }
  55.  
  56. public void showDetail() {
  57. System.out.println("Name: " + getName());
  58. System.out.println("Score: " + getScore());
  59. System.out.println("Advisor: " + advisor);
  60. }
  61. }
  62.  
  63. // Main class to test
  64. public class Test {
  65. public static void main(String[] args) {
  66. // Test Employee
  67. Employee emp = new Employee();
  68. emp.setID("E001");
  69. emp.setName("Alice");
  70. emp.calTax(50000, 10000);
  71. emp.showDetails();
  72.  
  73. System.out.println("======================");
  74.  
  75. // Test Student
  76. Student stu = new Student();
  77. stu.setName("Bob");
  78. stu.setScore(85);
  79. System.out.println("Student Name: " + stu.getName());
  80. System.out.println("Student Score: " + stu.getScore());
  81.  
  82. System.out.println("======================");
  83.  
  84. // Test GradStudent
  85. GradStudent grad = new GradStudent();
  86. grad.setName("Charlie");
  87. grad.setScore(90);
  88. grad.setAdvisor("Dr. Smith");
  89. grad.showDetail();
  90. }
  91. }
  92.  
Success #stdin #stdout 0.02s 25764KB
stdin
Standard input is empty
stdout
// Employee.java
class Employee {
    private String empID;
    private String empName;
    private double tax;

    public void setID(String id) {
        this.empID = id;
    }

    public void setName(String name) {
        this.empName = name;
    }

    public void calTax(double salary, double bonus) {
        this.tax = (salary + bonus) * 0.09;
    }

    public void showDetails() {
        System.out.println("Employee ID: " + empID);
        System.out.println("Employee Name: " + empName);
        System.out.println("Tax: " + tax);
    }
}

// Student.java
class Student {
    private String name;
    private int score;

    public void setName(String name) {
        this.name = name;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }
}

// GradStudent.java (Inheritance from Student)
class GradStudent extends Student {
    private String advisor;

    public void setAdvisor(String advisor) {
        this.advisor = advisor;
    }

    public void showDetail() {
        System.out.println("Name: " + getName());
        System.out.println("Score: " + getScore());
        System.out.println("Advisor: " + advisor);
    }
}

// Main class to test
public class Test {
    public static void main(String[] args) {
        // Test Employee
        Employee emp = new Employee();
        emp.setID("E001");
        emp.setName("Alice");
        emp.calTax(50000, 10000);
        emp.showDetails();
        
        System.out.println("======================");

        // Test Student
        Student stu = new Student();
        stu.setName("Bob");
        stu.setScore(85);
        System.out.println("Student Name: " + stu.getName());
        System.out.println("Student Score: " + stu.getScore());
        
        System.out.println("======================");

        // Test GradStudent
        GradStudent grad = new GradStudent();
        grad.setName("Charlie");
        grad.setScore(90);
        grad.setAdvisor("Dr. Smith");
        grad.showDetail();
    }
}