java - How to display an object that with multiple parameters without a getter? -


in homework of mine, in main have line:

lec.addstudent( "james" , "a1" , "bict" ); 

in class called lectureroom:

import java.util.arraylist;  public class lectureroom{      private string coursename;     private string roomnumber;     private string lecturer;         private arraylist <student> studentlist;      public lectureroom(string roomnumber , string coursename , string lecturer)     {         this.coursename=coursename;         this.roomnumber=roomnumber;         this.lecturer = lecturer;         this.studentlist = new arraylist<student>();     }      public void printstudents(){          system.out.println(studentlist);      }      public void addstudent(string name, string id, string major)     {         student s = new student(name, id , major);         studentlist.add(s);     }                  public arraylist<student> getstudentsbymajor(string major)     {         arraylist<student> students = new arraylist<>();          (student student : studentlist) {             if (student.getmajor().equals(major))                 students.add(student);         }          return students;     } 

the outcome be:

adding:james, a1, bict

normally, getter would:

system.out.println("adding:" + getstudentname() + ", " + getid() + ", " + getmajor() ); 

however in case in method addstudent, have created object called "s" stores name of student, id , major.

suppose want print these 3 in line, how can so?

i tried these in printstudents() method

1) system.out.println(studentlist);

2) for(student studentdetails : studentlist) { system.out.println(studentdetails); }

but both returned-

[student@bf5743]

what error called , how can solve it? thanks!

you need override tostring method in class, ,

@override public string tostring() {    return string.format();// print desired format. } 

using system.out.println(obj) directory print use default tostring method returns:

object.getclass().getname() + "@" + integer.tohexstring(object.hashcode()) 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -