Reputation: 119
I have to make 2 classes (Course.java and UseCourse.java). I have to have methods like get course, courseName, addStudent, dropStudent, getStudent and getNumberOfStudent.
When I tried to run these files I keep getting:
3 students are in Java Programming : [Ljava.lang.String; @6bc7c054
Can somebody explain what this means and how I can fix it?
UseCourse.java
public class UseCourse{
public static void main(String[] args){
Course c1 = new Course("Java Programming", "Bob Brown");
c1.getCourseName();
c1.getStudents();
c1.getNumberOfStudents();
System.out.println(c1.numberOfStudents + " students are in "+
c1.courseName + " : " + c1.students);
//c1.dropStudent("Andrew Drew");
c1.getStudents();
c1.getNumberOfStudents();
System.out.println(c1.numberOfStudents + " students are in "+
c1.courseName + " : " + c1.students);
}
}
Course.java
import java.util.Arrays;
import java.lang.String;
public class Course{
String courseName;
String[] students = {"Berry Cherry", "Lia Lee", "Andrew Drew"};
int numberOfStudents;
Course(String newCourseName, String newAddStudent){
courseName = newCourseName;
// Not sure if I'm doing this right but I'm trying to
// add the new student to the end of the list/array
students[students.length -1] = newAddStudent;
}
/*public void dropStudent{
}*/
public String getCourseName(){
return courseName;
}
public String getStudents(){
return Arrays.toString(students);
}
public int getNumberOfStudents(){
numberOfStudents = students.length;
return numberOfStudents;
}
}
Upvotes: 2
Views: 1142
Reputation: 3324
because this is the default textual representation of an Array (toString
)
So use this : Arrays.toString(students)
System.out.println(c1.numberOfStudents + " students are in "+
c1.courseName + " : " + Arrays.toString(c1.students));
Upvotes: 2