请求修正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, andstudent_agein the subclass—sinceStudent_UextendsStudent, 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[] financeRecordarray 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
- Removed duplicate properties: Got rid of
student_name,studentID,student_ageinStudent_U—we inherit these from the parentStudentclass, so no need to redefine them. - Added the Finance array: Added
private Finance[] financeRecord;to store semester payment details, and initialized it in both constructors. - Fixed the constructor: Removed duplicate parameters and used
super()to call the parent class's constructor, which correctly initializes inherited properties. - Followed Java naming conventions: Renamed
TotalFinanceRecordtototalFinanceRecords(camelCase for instance variables). - Added utility methods: Included
addFinanceRecord()to populate the array and updated theprint()method to display both student details and finance records. - Included a sample Finance class: Since you didn't provide it, I added a basic
Financeclass with common payment properties—adjust this to match your actualFinanceclass 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
相关产品推荐
相关产品推荐

