Java Question

Calling method in an element of a object array

I am working a simple program for storing the course information that each student takes and printing each course that the student takes.

The program consists of three class, one of the class called course which storing a course name of which a student takes and the score the student got in the course. One object for the class stores one course information.

public class Course
{
// instance variables - replace the example below with your own
private String courseName;
private int testMarks;
private String result;

/**
* Constructor for objects of class Course
* The constructor stores the information of a course
*/
public Course(String name,int marks)
{
courseName=name;
testMarks=marks;
}

public String toString(String name,int marks) //combine the course name with the mark to make a string and return the string.
{
// put your code here
String score=Integer.toString(marks);
result=name+", "+score;
return result;
}
}

the another class is UniversityStudent that stores the information of each student namely the name of a student, number of course the student takes and the course list of the student.

public class UniversityStudent
{
// instance variables - replace the example below with your own
private String studentName;
private int courseNumber;
private Course[] list;

/**
* Constructor for objects of class UniversityStudent
*/
public UniversityStudent(String name,int number,Course[] a)
{
// initialise instance variables
studentName=name;
courseNumber=number;
list=a; //the course list of each student
}

public void print() //print out the course that each student takes.
{
// put your code here
System.out.println("Student Name:"+studentName);
for(int i=0;i<courseNumber;i++)
{
System.out.println(list[i].toString());
}
}
}


Also, the main method. The main method creates two object array. One object array stores the courses that each student takes.

public class Test1Q2A
{
public static void main(String[] args)
{
Course[] listA=new Course[10];
listA[0]=new Course("EIE3320",60);
listA[1]=new Course("EIE3105",40);
UniversityStudent studentA=new UniversityStudent("John",2,listA);
studentA.print();
Course[] listB=new Course[10];
listB[0]=new Course("COMP1001",84);
listB[1]=new Course("EIE3105",68);
listB[2]=new Course("EIE3320",52);
UniversityStudent studentB=new UniversityStudent("Mary",3,listB);
studentB.print();
}
}


I would like to call the method toString() in class Course to return the saved String in the class Course but when I print out the string using the method print() in class University student, I cannot get the desired result. What is the problem?

Answer Source

It's because you aren't actually overriding the toString() method. Your method has a set of parameters and thus is a completely different method. So you either need to put parameters where you call the Course.toString(String name,int marks) method or you need to change your toString method to one without parameters. I would suggest doing something like so:

public String toString() {
    String score = Integer.toString(testMarks);
    result = courseName + ", " + score;
    return result;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download