I have been trying to model a school in Java(no main method, just attributes). How do I assign 30 students and a teacher to a classroom?( is it possible to do without a main method?)
public class Person{
private String name;
private String surname;
private String age;
private String job;
}
public class Teacher extends Person{
private String job = "teacher";
}
public class Student extends Person{
private String job = "student";
}
private class Classroom{
// 1 teacher and max. 30 students per classroom
}
My best guess: Using Arrays?
private Student[] students = new Student[30]; // there can be less students so would this be okay?
private Teacher classroomTeacher;
One last question, is using "extends" to create teacher and student class a good practice? If not, how can I improve it? If so, should Person,Student,Teacher be in the same file or seperated?
public class School{
private enum schoolTypes{
ArtSchool,
ScienceSchool,
SportsSchool,
}
private Classroom classrooms;
}
If I were to create a School class and specified the number of students a class can have, would I use ArrayLists or would Arrays be enough?
CodePudding user response:
For clarity, change name
to givenName
. See Wikipedia.
private String givenName;
private String surname;
Age should be an integer, not text.
private int age;
If all the possible jobs are known at compile-time, use Java’s powerful flexible enum facility.
enum Job { TEACHER , ADMINISTRATOR , STUDENT }
But actually, you have no need for the job. You have subclasses for teacher and student. So noting their job is superfluous.
Choose which route you want to go:
Person
class with ajob
member field of typeJob
enum.- Subclasses
Teacher
&Student
.
Both routes are valid, each with pros and cons. But don’t mix the two.
You asked:
is using "extends" to create teacher and student class a good practice?
Yes, if you choose the second of the two bullets above.
Classroom
should be its own class, not private
as you marked it. Yes you could use arrays. But using the Java Collections framework is much more flexible and convenient.
public Classroom
{
private Teacher teacher ;
private Set< Student > students ;
… add constructor
… add getters, and possible setters
}
You asked:
would I use ArrayLists or would Arrays be enough?
Generally best to use the collections such as List
and Set
. When speed is crucial, or memory is limited, use arrays.
You asked:
should Person,Student,Teacher be in the same file or separated?
Yes, generally best to keep each class in its own .java
file. Using an IDE makes navigating between the files quite easy.
create a School class
public class School
{
String schoolName ;
Administrator principal ;
Set< Classroom > classrooms ;
…
}
You asked:
is it possible to do without a main method?
First define your model of the real world, as you are doing. Decide on the structure of your “problem domain” classes such as school, classroom, teacher, student, etc.
The main
method is the entry point for running your app. This is where you start to instantiate objects. You take input from the user, or from files, or from a database, and use that data to make objects from your classes.
How do I assign 30 students and a teacher to a classroom?
In your main method, or some method called later:
Set< Student > students = new HashSet<>() ;
students.add( new Student( … ) ) ;
students.add( new Student( … ) ) ;
…
Teacher teacher = new Teacher( … ) ;
Classroom classroom = new Classroom( teacher , students ) ;
You asked:
specified the number of students a class can have
Setting a limit on the number of students in a class is part of what is known as data validation. You’ll have to write code for that, checking the limit as you add students.
The Jakarta Bean Validation framework can help with that chore. (For real work, not for your simple schoolwork project.)
CodePudding user response:
You have the basic idea down, but I would consider the following:
You're mixing implementation patterns. I would either use an enum to mark a job (not a string) or use subclasses, but not both simultaneously. I would lean more towards the subclassing myself; just ditch the job
field, it's unnecessary.
As for the student list, I would subclass ArrayList and simply override add
and addAll
to check the size of itself and throw an Exception when trying to add more than the limit.
Your classroom then becomes something like:
public class Classroom {
private Teacher teacher;
private List<Student> students;
public Classroom(Teacher teacher, List<Student> students) {
this.teacher = teacher;
this.students = new LimitedList(students, 30); //class you should make
}
}