0
How can write a school members, and print all of students of each teacher?
think we have and school with teachers and each teacher has some students, help me to write a code to add some teachers and their students and then print all of them. input of app is : add teachers, then select a teacher and add student for that. output of app is: print all teachers and their students. important: each members(teachers and students) have a specific code, that can call their name with code. or call code with name.
12 Answers
+ 2
thank you Jmse for code
+ 1
very good, thanks, very helpful, now my next question: in gui mode, we cant define t1 or t2 and more, how can write this? think we have jtextfield and a button to add teachers, and a jcombobox to add all teachers name to it, also have a jtextfield2 to add students name, and a button2 to add students, we select a teacher and write student name and click on button2, this student add for selected teacher.how about this, I know this question get more time to write, im sorry for that and thank you about your help.
+ 1
@hamid, here are a complete example that can be useful for you: https://code.sololearn.com/cewcAHq6r5eZ/#java
+ 1
how can ask my questions? do you have skype?
0
You need to create at least 2 class, one for teachers and other for students.
Teacher.java
Student.java
Every teacher needs an attribute to store a list/array of objects of Student type.
Teacher.java:
public class Teacher {
private String name;
private List<Student> students;
public Teacher() {
this.name = "";
this.students = new ArrayList<Student>();
}
public void addStudent(Student s) {
this.students.add(s);
}
}
I ommited getters and setters but that's the idea...
Students class it's similar but doesn't need a list, only name and maybe and id. All it's up to you and what you want to deep into the application design.
Good luck
0
how about print output?
0
I'm sorry, I forgot that part.
For the output you need a third class that contains the main method.
In this method you have to instantiate as many Teachers and Students as you need and then, asign every teacher with his Students. Something like this:
public class myApplication {
public static void main(String [] args) {
Teacher t1 = new Teacher ();
t1.setName("William");
Student s1 = new Student();
s1.setName("Robert");
// Add Robert to William
t1.addStudent(s1);
// Show students of William
for (Student s : t1.getStudents()) {
System.out.println(s.getName());
}
}
}
0
this code dose not work.
0
who can help?
0
Hi hamid
The code I posted was not complete, I suggested you to use something like that, but it is not a complete solution.
0
You welcome @hamid,
You can ask your questions opening new SoloLearn questions.
There is a good practice to ask only one thing per question as they said in their blog.
In addition there are lot of kind programmers that can help you. Anyway I'm glad you want to ask my opinion. In that case you can post a link of your question in this thread so I will be warn by the app.
Thank you and good luck in your programmer path!