It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
Koen Ursem wrote:Your current code when the commented line is not commented will try to add your student into the list of courses. Now a list of Courses and Students mixed together would indeed not be what you want and if Clist is of type Course would even give you an error.
There are multiple ways of solving this, you can either give the Student an empty list of Courses where you add the courses to the list in the Student object. Or you can add an empty list of students to your Course and add the student to a students list in the Course object.
You can also let both objects have a list where you add persons to courses and courses to persons. But: then you have double data which would be redundant and can result in inconsistent data. Since person has its own list, and course has its own list.
You may want to think first about what approach you would like to make, and write it down to get an idea.
Daniel Demesmaecker wrote:It sounds like homework... So I won't post a full solition.
I don't know if you just created the array's or also maded classes for the courses and the students.
If you did the lather, you should have a instance variable which is a list of students(let's call it enrolledStudents) and make a enrollStudent method that takes a student as a parameter in the course class.
After you created the students to enroll and the course to enroll to you can enroll a student by calling the enrollStudent method of the to enroll to course and pass the to enroll student as parameter.
In the enrollStudent method you add the parameter to enrolledStudents. Make sure your enrolledStudents has a getterMethod too...
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
Daniel Demesmaecker wrote:I you have a seperate class for your courses who have enrolled students in them it's not enough to pass only the student. You have to create an array who keeps track of the course and the enrolled students (I would use a hashmap).
so you have to pass both student and course, then in the function looping the hasmap searching for the course and adding the student, but the way I descriped before is way easier.
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
Daniel Demesmaecker wrote:I you have a seperate class for your courses who have enrolled students in them it's not enough to pass only the student. You have to create an array who keeps track of the course and the enrolled students (I would use a hashmap).
so you have to pass both student and course, then in the function looping the hasmap searching for the course and adding the student, but the way I descriped before is way easier.
Koen Ursem wrote:
Daniel Demesmaecker wrote:I you have a seperate class for your courses who have enrolled students in them it's not enough to pass only the student. You have to create an array who keeps track of the course and the enrolled students (I would use a hashmap).
so you have to pass both student and course, then in the function looping the hasmap searching for the course and adding the student, but the way I descriped before is way easier.
A Hashmap would be a neat solution, but considering this is an assignment I think OP is more looking for a simple list<Student> implementation in the Course object.
@OP
If you want to add a student to your course, you would need a list/array in your course object to save this in.
Then when you want to add the student you want to call this course object and run a method that adds the student you have given to this list/array.
Considering the code from your first code, something like this 'c.addStudent(yourStudent);' in the for loop. Be aware that your for loop is running over all courses in your Clist, so now the student would be enrolled to all courses if you implement the suggested method. I will let you try to figure that part out for yourself for now, let us know if you can figure it out or not
Koen Ursem wrote:Only thing I see wrong right now is that you print this.student in the displaycourseinfo instead of this.enrolledstudents (You may have to find a way to print a list correctly).
your course class is now holding both a student object and a enrolledstudents list right now, but it could be this was your intention to have that student be the teacher of the course or something?
And yes, when you have more students and courses to work with you may want to have a way of selecting the right ones. Something in the trend of if student.name = ?? then do this. Same for course.
Unfortunately many people are reluctant to look at other sites like that. I did, and couldn't read it all which shows why we discourage screenshots.Joel Drake wrote: . . . and this what I got
https://gyazo.com/243cbc5af333f48383e1873f6a305549
Junilu Lacar wrote:Let's see, you have a list of courses. You have a student that you want to enroll in a course. You already have a way to enroll a student in a course through the Course.enrollStudent() method.
So, isn't your only problem now really to figure out a way to find the course in the list of courses?
So Google for "how to iterate over a list in Java"
Then, as you iterate through each course in the list of courses, how do you know you've found the right course that the student wants to enroll in?
When you find the right course, then wouldn't you just go ahead and call the appropriate method on that course?
Campbell Ritchie wrote:Welcome to the Ranch
I don't know what the bug is causing HTML tags to appear, but they don't appear if you write instead of leading spaces. We are working on it.Unfortunately many people are reluctant to look at other sites like that. I did, and couldn't read it all which shows why we discourage screenshots.Joel Drake wrote: . . . and this what I got
https://gyazo.com/243cbc5af333f48383e1873f6a305549
I think you are going to have to start from the beginning. You have got some code working and some not; that is normal. I think many people design from centre→periphery and code from periphery→centre, so you would expect to have some code working. But I think you need to go back to your design. What does the structure of your program look like? Do you have some sort of class in the middle, possibly called College? Presumably you have multiple Course objects and multiple Student objects. And the College object will store them all somehow. Let's imagine you have a method whereby you can add a course, like this:- add(new Course("EN123", "Basic English Literature"); Get that working, with the most basic Course class you can write, and it will look like this:-That is about as rudimentary as you can get and still pass into your add() method. Once you get add() working, and you find nothing else will work because I have removed 90% of your Course class, start putting the Course class back together, bit by bit, and make sure it continues to work. Compile and run your code after every new method you write.
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
Joel Drake wrote:
No I eventually have to look for a specific course by course code then display all enrolled students in that course. I tried this.enrolledstudents
and this what I got
https://gyazo.com/243cbc5af333f48383e1873f6a305549
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
Daniel Demesmaecker wrote:this could be your main method:
Liutauras Vilda wrote:
Class names usually are spelt as singular. Not Students, but Student. Not Courses, but Course.
This main method is too long. Ideally, main method supposed to start an application and nothing else, i.e.:
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
Daniel Demesmaecker wrote:Normally the main would start an userinterface of some kind, which would launch the needed methods, since this isn't an option here...
Daniel Demesmaecker wrote:
Daniel Demesmaecker wrote:
Liutauras Vilda wrote:
Class names usually are spelt as singular. Not Students, but Student. Not Courses, but Course.
This main method is too long. Ideally, main method supposed to start an application and nothing else, i.e.:
I give you the classnames, but for the main method you have to take in account the level of experience of the op.
Normally the main would start an userinterface of some kind, which would launch the needed methods, since this isn't an option here...
If you wanted you could split up the code in seperated method and call thoose from the main, but would that make such a big difference?
Liutauras Vilda wrote:That reads like an interesting, fairly small, meant to be object oriented project.
Would recommend to start from the first bullet point, one step at a time.
Joel Drake wrote:
Daniel Demesmaecker wrote:
As you can see in the pic. It needs to be text based or a menu if you will.Would you recommend me to have my inputs in the other class which contains then method?Regardless of all that, I still haven't figured out how to add students to courses
I thought I got the hang of it,but I'm still having an issue.Look at the output above. I used this.enrolledstudents, but it didn't work.
The answer to how to add a student to a course is already bin given several times...
Look at the code examples that have bin posted.
you have a Student.class, a Course.class and a Main.class.
In your Course.class you have an arrayList of type Student, a getter for that array and a enrollStudent method.
In the enrollStudent method you add the student (list.add(Student);)
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
Joel Drake wrote:
This is actually what I'm trying to do, I'm trying to to use multiple classes+inheritance+polymorphism, and all other possible stuff I'm having 8 classes including the main class.I don't know now if I should put my input in other classes or keep them in the main one
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
Daniel Demesmaecker wrote:
Joel Drake wrote:
Daniel Demesmaecker wrote:
As you can see in the pic. It needs to be text based or a menu if you will.Would you recommend me to have my inputs in the other class which contains then method?Regardless of all that, I still haven't figured out how to add students to courses
I thought I got the hang of it,but I'm still having an issue.Look at the output above. I used this.enrolledstudents, but it didn't work.
The answer to how to add a student to a course is already bin given several times...
Look at the code examples that have bin posted.
you have a Student.class, a Course.class and a Main.class.
In your Course.class you have an arrayList of type Student, a getter for that array and a enrollStudent method.
In the enrollStudent method you add the student (list.add(Student);)
Sry I didn't see the long main class you posted.I think I did the same thing!
Have a look at this plssss.
Main class
Course class
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
Joel Drake wrote:Oh man why does it look like this?
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
It doesn't any more because I got rid of all your spaces.Joel Drake wrote:Oh man why does it look like this?
Campbell Ritchie wrote:
It doesn't any more because I got rid of all your spaces.Joel Drake wrote:Oh man why does it look like this?
That also gave me a chance to see what you are writing, but I am afraid it isn't good. You should have a College object (or similar) which holds lists of students and courses; that is what happens in real life too. You have got those Lists floating in some sort of limbo in the main() method, which is not what the main() method is intended for. Also, give those lists proper names; you followed slist with a comment that it is the student List, when you could have called it studentList (or similar) and made its meaning obvious. (By the way: note where the Capital Letter belongs.)
You didn't show us the student class, but I think you need to think again about the Course class. The version I reformatted had a constructor taking a student (yes, just one) and one taking a name etc. That isn't how things work in real life. You have a course with a name, and you publicise it, and then students turn up to enrol. It is possible to have a course without any students but every Course a name and ID. Once you have got that sorted out, you can consider how you are going to regiester all the students on a particular course. Don't try to do it all at once; divide your task into small bits and it will work a lot better.
I found a beautiful pie. And a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|