实现2个Java类

时间:2018-05-22 04:18:23

标签: java arrays class variable-assignment

我正在学习java并尝试实现两个java类。

  • 学生:firstNamelastNamedepartmentInyearGraduation,此学生正在参加的UAClass数组,与这些课程收到的成绩相对应的整数数组
  • UAClass:teacherFirstNameteacherLastNamesemesterOfferednumCredits

在Student类中,实现一个计算GPA的方法。在学生的main()方法中,启动一个学生对象并打印出她的GPA。

在我的student.java课程中,我有:

import java.util.*;

public class Student {

private String firstName;
private String lastName;
private String departmentIn;
private String yearGraduation;
private float [] grade;
private int counter = 0;
private String Student;

public Student(String my_firstName, String my_lastName, String my_deptIn, String my_yearGrad) {
firstName = my_firstName;
lastName = my_lastName;
departmentIn = my_deptIn;
yearGraduation = my_yearGrad;
grade = new float[5];                    
}

public String toString(){

    String value;
    value  = "First Name: " + firstName + "\n";
    value += "Last Name : " + lastName + "\n";
    value += "Department: " + departmentIn + "\n";
    value += "Grad. Year: " + yearGraduation + "\n";
    return value;
}


public static void main(String[] args) {
 Student my1 = new Student("Bob", "Hope", "MBA", "2018");
     Student my2 = new Student("John", "Smith", "MBA", "2020");
     Student my3 = new Student("Jane", "Doe", "MBA", "2021");

     UAClass cy1 = new UAClass[4];
     String[] secondArray = cy1.getarrayClass();


     System.out.println(my1);
     System.out.println(my2);
     System.out.println(my3);
     System.out.println(Arrays.toString(cy1));

 }
 }

在我的UAClass.java类中,我有:

import java.util.*;

public class UAClass {
private String teacherFirstName;
private String teacherLastName;
private String semesterOffered;
private String numCredits;  
private String[] arrayClass = {"MBA 501","MBA 505","MBA 513","MBA 545"};

public UAClass(String teacherF, String teacherL, String semesterO, String numC) {
    teacherFirstName = teacherF;
    teacherLastName = teacherL;
    semesterOffered = semesterO;
    numCredits = numC;
}    

public String[] getarrayClass(){
    return arrayClass.clone();
}     


}

我要做的是在' UAClass'中创建一个数组。并将其打印到' Student'但我似乎无法让它发挥作用。

我已按照Amit的建议修改了代码。当我运行它时,我收到此错误。

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Array.getarrayClass at Homework2.Student.main(Student.java:66)

似乎遇到了String [] secondArray = cy1的问题。 getarrayClass ();

我拿出了String[] secondArray = cy1.getarrayClass()行,似乎运行正常,但现在我得到的只是[null, null, null, null]

2 个答案:

答案 0 :(得分:0)

当您更改代码时,它会起作用,但我无法理解您在代码中尝试执行的操作。

UAClass cy1 = new UAClass("Bob", "", "", "");
String[] secondArray = cy1.getarrayClass();
System.out.println(my1);
System.out.println(my2);
System.out.println(my3);
System.out.println(cy1.getarrayClass());

答案 1 :(得分:0)

首先,您的UAClass有一个构造函数,它将String teacherF, String teacherL, String semesterO, String numC作为参数。

所以你需要像这样调用这个构造函数:

UAClass cy1 = new UAClass("Teacher F", "Teacher L", "Semester", "NumC");

其次,您使用String []类型。这是一种低级数组类型。您可以在Java中执行此操作,但通常人们使用List类型,而不是原始类型,但更像List<String>。 List实际上是一个接口,但您可以引用它,因为Arrays类返回List类的实现。

然后你应该使用:

private List<String> arrayClass = Arrays.asList("MBA 501","MBA 505","MBA 513","MBA 545");

然后返回数组的克隆。我认为你这样做是因为你不想改变阵列。我只会返回带有值的串联String。这是一个很好的流示例。

public String getClasses() {
    return arrayClass.stream().collect(Collectors.joining(","));
}

现在在Student类中,您可以打印如下所示的类列表:

System.out.println(cy1.getClasses());