You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求修正Student子类代码:添加Finance数组存储学期缴费信息

Fixing Your Student_U Class with Finance Record Array

Hey there! Let's get your Student_U class sorted out properly so it includes that Finance array for semester payment records, and fix a few issues in your current code along the way. Here's what we'll adjust and the final corrected code:

Key Issues in Your Original Code

  • You're redefining properties like student_name, studentID, and student_age in the subclass—since Student_U extends Student, these should already exist in the parent class, and redefining them breaks inheritance.
  • The constructor has duplicate parameters (like student_name2) which are unnecessary and confusing; we should use the parent constructor to initialize inherited properties.
  • The required Finance[] financeRecord array is missing entirely.

Corrected Code

package lab5;

// Assume the Finance class is defined elsewhere in your project
class Finance {
    // Example properties for Finance class (adjust based on your actual requirements)
    private byte semester;
    private double amountPaid;
    private String paymentDate;

    public Finance(byte semester, double amountPaid, String paymentDate) {
        this.semester = semester;
        this.amountPaid = amountPaid;
        this.paymentDate = paymentDate;
    }

    // Getters for Finance class (add setters if needed)
    public byte getSemester() { return semester; }
    public double getAmountPaid() { return amountPaid; }
    public String getPaymentDate() { return paymentDate; }

    public void printFinanceRecord() {
        System.out.printf("Semester: %d | Amount Paid: %.2f | Payment Date: %s%n", 
                          semester, amountPaid, paymentDate);
    }
}

class Student_U extends Student {
    private byte currentSemester;
    private byte totalFinanceRecords; // Renamed to follow Java naming conventions (camelCase)
    private String cohort;
    private Finance[] financeRecord; // The required array to store semester payment details

    // Default constructor
    public Student_U() {
        super(); // Call parent class's default constructor
        currentSemester = 1;
        totalFinanceRecords = 0;
        cohort = "";
        financeRecord = new Finance[0]; // Initialize empty array by default
    }

    // Parameterized constructor
    public Student_U(String studentName, String studentID, int studentAge, 
                     String course, String year, String section, String subject,
                     byte currentSemester, byte totalFinanceRecords, String cohort) {
        // Call parent class's parameterized constructor to initialize inherited properties
        super(studentName, studentID, studentAge, course, year, section, subject);
        this.currentSemester = currentSemester;
        this.totalFinanceRecords = totalFinanceRecords;
        this.cohort = cohort;
        // Initialize the financeRecord array with the specified size
        this.financeRecord = new Finance[totalFinanceRecords];
    }

    // Getters and Setters for subclass-specific properties
    public byte getCurrentSemester() { return currentSemester; }
    public void setCurrentSemester(byte currentSemester) { this.currentSemester = currentSemester; }

    public byte getTotalFinanceRecords() { return totalFinanceRecords; }
    public void setTotalFinanceRecords(byte totalFinanceRecords) {
        this.totalFinanceRecords = totalFinanceRecords;
        // Resize the array when total records count changes
        this.financeRecord = new Finance[totalFinanceRecords];
    }

    public String getCohort() { return cohort; }
    public void setCohort(String cohort) { this.cohort = cohort; }

    public Finance[] getFinanceRecord() { return financeRecord; }
    public void setFinanceRecord(Finance[] financeRecord) { this.financeRecord = financeRecord; }

    // Method to add a finance record to the array at a specific index
    public void addFinanceRecord(int index, Finance record) {
        if (index >= 0 && index < financeRecord.length) {
            financeRecord[index] = record;
        } else {
            System.out.println("Invalid index for finance record!");
        }
    }

    @Override
    public void print() {
        // Call parent class's print method first to show inherited details
        super.print();
        System.out.println("Current Semester: " + currentSemester);
        System.out.println("Cohort: " + cohort);
        System.out.println("Finance Records:");
        for (Finance record : financeRecord) {
            if (record != null) {
                record.printFinanceRecord();
            }
        }
        System.out.println();
    }
}

// Assume the parent Student class looks like this (adjust if your actual class differs)
class Student {
    protected String studentName;
    protected String studentID;
    protected int studentAge;
    protected String course;
    protected String year;
    protected String section;
    protected String subject;

    public Student() {
        studentName = "";
        studentID = "";
        studentAge = 0;
        course = "";
        year = "";
        section = "";
        subject = "";
    }

    public Student(String studentName, String studentID, int studentAge, 
                   String course, String year, String section, String subject) {
        this.studentName = studentName;
        this.studentID = studentID;
        this.studentAge = studentAge;
        this.course = course;
        this.year = year;
        this.section = section;
        this.subject = subject;
    }

    // Getters and Setters for Student class (add if missing)
    public String getStudentName() { return studentName; }
    public void setStudentName(String studentName) { this.studentName = studentName; }

    public String getStudentID() { return studentID; }
    public void setStudentID(String studentID) { this.studentID = studentID; }

    public int getStudentAge() { return studentAge; }
    public void setStudentAge(int studentAge) { this.studentAge = studentAge; }

    public void print() {
        System.out.println("Student Name: " + studentName);
        System.out.println("Matric No: " + studentID);
        System.out.println("Age: " + studentAge);
        System.out.println("Course: " + course);
        System.out.println("Year: " + year);
        System.out.println("Section: " + section);
        System.out.println("Subject: " + subject);
    }
}

What We Changed

  1. Removed duplicate properties: Got rid of student_name, studentID, student_age in Student_U—we inherit these from the parent Student class, so no need to redefine them.
  2. Added the Finance array: Added private Finance[] financeRecord; to store semester payment details, and initialized it in both constructors.
  3. Fixed the constructor: Removed duplicate parameters and used super() to call the parent class's constructor, which correctly initializes inherited properties.
  4. Followed Java naming conventions: Renamed TotalFinanceRecord to totalFinanceRecords (camelCase for instance variables).
  5. Added utility methods: Included addFinanceRecord() to populate the array and updated the print() method to display both student details and finance records.
  6. Included a sample Finance class: Since you didn't provide it, I added a basic Finance class with common payment properties—adjust this to match your actual Finance class structure.

This should meet your requirement of storing each semester's payment details in the array, and fixes the inheritance issues in your original code.

内容的提问来源于stack exchange,提问作者Aurora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 13:52:38