Home » Java » Implementing 2 Java Classes

Implementing 2 Java Classes

Posted by: admin June 30, 2018 Leave a comment

Questions:

I’m learning java and trying to implement two java classes.

  • Student: firstName, lastName, departmentIn, yearGraduation, an array of UAClass this student is taking, an array of integers corresponding to the grades received for these classes
  • UAClass: teacherFirstName, teacherLastName, semesterOffered, numCredits

In the Student class, implement a method that calculates GPA. In the Student’s main() method, initiate one Student object and print out her GPA.

In my student.java class I have:

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

 }
 }

And in my UAClass.java class I have:

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


}

What I am trying to do is to create an Array in ‘UAClass‘ and having it printed into ‘Student‘ but I can’t seem to get it working.


I’ve modified the code as Amit suggested. When I run it, I get this error.

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

It seems to be having an issue with String[] secondArray = cy1.getarrayClass();


I took out the line String[] secondArray = cy1.getarrayClass() and it seems to run fine but now all I get is [null, null, null, null]

Answers: