Я не знаю, как сделать массив для хранения деталей на каждый семестр. Я должен создать подкласс класса Student - PullRequest
0 голосов
/ 09 июля 2020

Вопрос требует, чтобы я сделал:

  • Массив Finance с именем financialRecord для хранения деталей платежей за каждый семестр.

Это мой код

package lab5;

class Student_U extends Student {

public String student_name;
private String studentID;
public int student_age;


private byte currentSemester;
private byte TotalFinanceRecord;
private String cohort;

public Student_U() {
    
    student_name = " ";
    studentID = " ";
    student_age = 0;
    currentSemester = 1;
    TotalFinanceRecord = 0;
    cohort = " ";
}


public Student_U(String student_name, String studentID, int student_age, 
        String course, String year,
        String section, String subject, String student_name2,
        String studentID2, int student_age2,
        byte currentSemester, byte totalFinanceRecord, String cohort) {
    super(student_name, studentID, student_age, course, year,
    section, subject);
    student_name = student_name2;
    studentID = studentID2;
    student_age = student_age2;
    this.currentSemester = currentSemester;
    TotalFinanceRecord = totalFinanceRecord;
    this.cohort = cohort;
}

public String getStudent_name() {
    return student_name;
}

public void setStudent_name(String student_name) {
    this.student_name = student_name;
}

public String getStudentID() {
    return studentID;
}

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

public int getStudent_age() {
    return student_age;
}

public void setStudent_age(int student_age) {
    this.student_age = student_age;
}

public byte getCurrentSemester() {
    return currentSemester;
}

public void setCurrentSemester(byte currentSemester) {
    this.currentSemester = currentSemester;
}

public byte getTotalFinanceRecord() {
    return TotalFinanceRecord;
}

public void setTotalFinanceRecord(byte totalFinanceRecord) {
    TotalFinanceRecord = totalFinanceRecord;
}

public String getCohort() {
    return cohort;
}

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

public void initStudent() {
        
}

public void print() {
    System.out.print("Student name: " + student_name + " ");
    System.out.print("\nMatric No: " + studentID + " ");
    System.out.print("\nAge: " + student_age + " ");
    System.out.print("\nCurrent Semester: " + currentSemester + " ");
    System.out.print("\nCohort: " + cohort + " ");
    
    System.out.println();
}
}

Пожалуйста, помогите мне исправить мой код, я был бы очень признателен. Это мое лабораторное задание, которое нужно отправить до завтра.

Ответы [ 2 ]

0 голосов
/ 09 июля 2020

насколько я понимаю, вы должны создать массив типа Finance

Finance []financeRecord = new Finance[totalFinanceRecord];

, а затем вы можете получить доступ к значениям класса Finance

financeRecord[indexNumber].methodName();
0 голосов
/ 09 июля 2020

Вы можете попробовать это, но лучше также просмотреть стандартные java концепции (массивы, классы и т. Д. c). После этого просто адаптируйте свой код как подходящий.

public class Finance  extends Student
{
    public static void main(String args[])
    {
        
        Finance f1 = new Finance("Student_1");
        System.out.println(f1);
        f1.setPayment(1, 10);
        System.out.println(f1);
        f1.setPayment(2, 10.77);
        System.out.println(f1);
        Student s2 = new Student("Student 2");
        Finance f2 = new Finance(s2);
        f2.setPayment(2, 88.77);
        System.out.println(f2);
    }
    Double finaceRecord[] = new Double[3];
    private void initPayment()
    {
        for(int i=0;i<finaceRecord.length;i++)
        {
            finaceRecord[i]=0.0;
        }
    }
    public Finance(Student s)
    {
        super(s.name);
        initPayment();
    }
    public Finance(String name)
    {
        super(name);
        initPayment();
    }
    //store first or second
    public void setPayment(int i, double d)
    {
        if(d<=0) return;
        if(i==1)
        {
            finaceRecord[i] = d;
        }
        else
        {
            finaceRecord[2] = d;
        }
        finaceRecord[0] = finaceRecord[2] + finaceRecord[1];
    }
    
    public String toString()
    {
        return "name="+super.name+", Total Paid="+finaceRecord[0]+","
                + " Sem1="+finaceRecord[1]+", Sem2="+finaceRecord[2];
    }
    
}

...

public class Student
{
    String name;
    int Semester;
    Student(String name)
    {
        this.name = name;
        this.Semester = 1;
    }
}

Ouptut

name=Student_1, Total Paid=0.0, Sem1=0.0, Sem2=0.0
name=Student_1, Total Paid=10.0, Sem1=10.0, Sem2=0.0
name=Student_1, Total Paid=20.77, Sem1=10.0, Sem2=10.77
name=Student 2, Total Paid=88.77, Sem1=0.0, Sem2=88.77
...