简体   繁体   English

我不知道如何做一个数组来存储每个学期的详细信息。 我应该创建 class Student 的子类

[英]I don't know how to do an array to store for each semester the details. I am supposed to create a subclass of the class Student

The question wants me to do:这个问题要我做:

  • An array of Finance called financeRecord to store the details of the payments for each semester.一个名为 FinanceRecord 的财务数组,用于存储每个学期的付款详细信息。

This is my code这是我的代码

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();
}
}

Please help me fix my code I would appreciate it so much.请帮我修复我的代码,我将非常感激。 This is my lab assignment which needs to be submitted by tomorrow.这是我明天需要提交的实验室作业。

You could try this, but it's also better to review standard java concepts (arrays, classes, etc).你可以试试这个,但最好回顾一下标准的 java 概念(数组、类等)。 After, just adapt your code as suitable.之后,只需根据需要调整您的代码。

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

from what I understand you are supposed to create an array of Finance type据我了解,您应该创建一个 Finance 类型的数组

Finance []financeRecord = new Finance[totalFinanceRecord];

and then you can access the values of Finance class然后您可以访问 Finance class 的值

financeRecord[indexNumber].methodName();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在学习如何为控制器进行单元测试,但我不知道如何正确地进行 - I am learning how to do the unit tests for the controllers and i don't know how to do it correctly 如果我不知道必须执行多少次,如何多次调用子类的方法? - How to call a subclass' method more than once if I don't know how many times I'll have to do that? 如果我在编译时不知道该类,如何获取Enum的值? - How do I get the value of an Enum, if I don't know the class at compile time? 如何获得我不知道的数组的索引? - How do I get the index of an array which I don't know? 我是一个初学者程序员,我不知道 Java 中的参数是如何工作的。 那么它们是如何工作的呢? - I am a beginner programmer and I don't know how parameters work in Java. So how do they work? 如何使用存储在辅助类中主类中的Strings值? (有关详细信息,请参见描述。Java) - How can I use the Strings values stored in a main class in my secondary class? (see description for details. Java) 我不知道如何使用Scanner类的功能 - I don't know how to use a function of the Scanner class 我不知道如何使用类类型作为参数 - I don't know how to use class types as parameter 如果我不知道数组将有多少个元素? - if i don't know array will be have how many elements? 我不知道如何为空数组分配新值 - I don't know how assign new value to the empty array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM