R asked in 電腦與網際網路程式設計 · 1 decade ago

初級 Java: Inheritance

寫這幾個 java file: Employee.java Boss.java TestEmployee.java 以及 employee.txt 做為輸入

Employee class 有以下條件:

1. A constructor that takes the first name, last name, monthly salary (a double) as arguments.

2. A getFirstName() method that returns the first name of the employee.

3. A getLastName() method that returns the last name of the employee.

4. toString() method, overriding the version inherited from the Object class.

5. A getSalary() method that returns the employee's monthly salary.

6. An earnings() method that returns the employee's annual salary.

Boss Class 有以下條件:

1. A constructor that takes a first name, last name, monthly salary and annual bonus as arguments, and passes the first name, last name and monthly salary to the Employee's constructor to initialize its firstName, lastName, and monthly salary members.

2. An earnings method that returns the boss's annual income (annual salary + bonus). This method overrides the version inherited from the parent class, Employee.

3. A toString method that returns a String containing the type of employee (i.e. "Boss: ") followed by the boss's name and annual

並且 Employee & Boss 的 data member 需要是 private value

TestEmployee Class 拿來做測試這個程式,並從 employee.txt 讀取

format 會是:

<number indicating whether or not employee 1 is a boss -- 0 = no, 1= yes>

<employee 1's first name>

<employee 1's last name>

<employee 1's monthly salary>

<employee 1's annual bonus, if employee 1 is a boss>

<number indicating whether or not employee 2 is a boss

employee.txt 範例:

0

Sam

Smith

1200.56

1

Harry

Hacker

5800.21

3000.50

最後會印出:

Sam Smith

annual earnings: $25000

Boss: Harry Hacker

annual earnings: $70000

3 Answers

Rating
  • 1 decade ago
    Favorite Answer

    參考一下

    import java.util.Scanner;

    public class TestEmployee {

    public static void main(String[] args) {

    //載入資源檔

    Scanner scan = new Scanner(

    ClassLoader.getSystemClassLoader().getResourceAsStream("employee.txt") );

    while (scan.hasNextLine()) {

    Employee employee = null;

    int indicating = Integer.parseInt(scan.nextLine());

    if (indicating == 0) {//Employee

    employee = new Employee(

    scan.nextLine(),

    scan.nextLine(),

    Double.parseDouble(scan.nextLine())

    );

    }

    else {//Boss

    employee = new Boss(

    scan.nextLine(),

    scan.nextLine(),

    Double.parseDouble(scan.nextLine()),

    Double.parseDouble(scan.nextLine())

    );

    }

    System.out.println(employee.toString());

    System.out.println();

    }

    }

    }

    class Employee {

    private String firstName;

    private String lastName;

    private double salary;

    public Employee(String firstName, String lastName, double salary) {

    this.firstName = firstName;

    this.lastName = lastName;

    this.salary = salary;

    }

    public String getFirstName() {return firstName;}

    public String getLastName() {return lastName;}

    public String toString() {

    StringBuffer sb = new StringBuffer();

    sb.append(firstName)

    .append(" ")

    .append(lastName)

    .append("\n")

    .append("annual earnings: $")

    .append(earnings());

    return sb.toString();

    }

    public double getSalary() {return salary;}

    public double earnings() {

    return salary * 12;

    }

    }

    class Boss extends Employee {

    private double bonus;

    public Boss(String firstName, String lastName, double salary, double bonus) {

    super(firstName, lastName, salary);

    this.bonus = bonus;

    }

    public double earnings() {

    return super.earnings() + bonus;

    }

    public String toString() {

    StringBuffer sb = new StringBuffer();

    sb.append("Boss: ")

    .append(super.toString());

    return sb.toString();

    }

    }

    輸出的結果是

    Sam Smith

    annual earnings: $14406.72

    Boss: Harry Hacker

    annual earnings: $72603.02

    2007-09-29 15:04:42 補充:

    根據你的描述只能先求出這樣的答案, 若跟你要的結果不符, 請自行調整計算式 public double earnings() {return salary * 12;}

    做為輸入的 employee.txt 檔請置於 TestEmployee.class 的所在位置, 若 TestEmployee 有 package 請自行補上

    ClassLoader.getSystemClassLoader().getResourceAsStream("package.employee.txt") )

    Source(s): me
  • 1 decade ago

    哈哈.. 嗯... 因為想了幾個小時都想不出要怎麼把兩個檔案的東西互相存讀...

    所以跑到這邊來問....

    2007-10-01 05:32:49 補充:

    為什麼沒辦法選最佳解答... and....

    只有一個回答為什麼要投票?!

  • R-way
    Lv 4
    1 decade ago

    這是作業吧? ^^

Still have questions? Get your answers by asking now.