This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line!
See this thread for details.
Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Small help with the concept of composition  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm creating a program where I add teachers to the system,and assign student to them. I'm using composition to work things out. It works fine, if I add a teacher and assign 1 student to them.However, I don't know how to assign 2 or more students to one teacher! I tried to work with ArrayList,but it did not work well. Could you please help me out with this? Here's the code I wrote.

Person class:-




Student class:-



Teacher class:-



Main class:-

 
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please get rid of all of those blank lines in your code. They make for horrible reading.

A Teacher is-a Person :: so far so good
A Teacher has-a collection of Students :: what kind of collection do you want?
You can add a Student to a teacher :: "add" is a verb
 
Joel Drake
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Please get rid of all of those blank lines in your code. They make for horrible reading.

A Teacher is-a Person :: so far so good
A Teacher has-a collection of Students :: what kind of collection do you want?
You can add a Student to a teacher :: "add" is a verb



Sorry about the blank lines, I didn't notice them. I don't understand what you mean by the kind of collection that I want!
All I want is to display for example,

Teacher Jack(teacher information) teaches student1(student information) , student2 ( student2 information). I can do this with only one student,how do I add more?
 
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Carey said, you have to create some sort of data structure to hold those students. You don't want student1, student2, student3, and student4 as individual fields.
Have you come across arrays? Alternatively, have a look at this section in the Java™ Tutorials.
 
Carey Brown
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joel Drake wrote:I don't understand what you mean by the kind of collection that I want!
All I want is to display for example,

Teacher Jack(teacher information) teaches student1(student information) , student2 ( student2 information). I can do this with only one student,how do I add more?


Well, a Teacher can have multiple Students. "Multiple" implies a "Collection". There are a number of "Collections" available in Java. One of the simplest is an ArrayList. This might suit your needs. You shouldn't add a Student using the Teacher's constructor, that only allows you to add one. You could use var-args (ie variable number of arguments) to add more than one Student per Teacher but that limits where in your code you can add Students, and you can't change your mind later and decide to add another one.

In object oriented design, NOUNS tend to become objects (classes), and VERBS tend to become methods. Sometimes when you describe your intent out loud the noun/verb thing becomes a bit more obvious.
 
Joel Drake
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both,you have been helpful, I will try arraylist again, and see how it goes.
 
Saloon Keeper
Posts: 9986
206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A minor remark about terminology: The way you used the word 'composition' implies that the Teacher class instantiates the Student objects, and keeps them private. When the teacher ceases to exist, so do the students. I'm pretty sure this is not what you mean.

A teacher is composed of a collection of students. A teacher aggregates individual students. These two statements are compatible with each other, but mean something slightly different. The first statement means that a teacher instantiates a collection and keeps it private. When the teacher ceases to exist, so does the collection (not the students in the collection!). The second statement means that the teacher has references to students (either in individual fields, or in a collection), that were given to the teacher, and the teacher doesn't control the lifetime of those objects.
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joel Drake wrote:Hello everyone, I'm creating a program where I add teachers to the system,and assign student to them. I'm using composition to work things out. It works fine, if I add a teacher and assign 1 student to them.However, I don't know how to assign 2 or more students to one teacher! I tried to work with ArrayList,but it did not work well. Could you please help me out with this?


Putting my "modeller"s hat on, I think you may find that there's a bit more to it than you've described.

1. Your Teacher class has two fields called coursesTeaching and coursesNum, both of which sound like collections to me, and suggests another relationship:
"Teacher teaches Course",
where Course is a class containing (at least) a name and a number.

2. I suspect Teachers teach Students because they're enrolled in a Course they teach, so there's no direct "Teacher teaches Student" relationship.

3. There is, however, probably a "Student studies Course" relationship.

That in turn suggests to me that your "assignment" task has two parts:
1. Assigning (or adding) Courses to a Teacher.
2. Assigning (or adding) Students to a Course.

After that, you're on your own. :-)

HIH

Winston
 
Joel Drake
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Joel Drake wrote:Hello everyone, I'm creating a program where I add teachers to the system,and assign student to them. I'm using composition to work things out. It works fine, if I add a teacher and assign 1 student to them.However, I don't know how to assign 2 or more students to one teacher! I tried to work with ArrayList,but it did not work well. Could you please help me out with this?


Putting my "modeller"s hat on, I think you may find that there's a bit more to it than you've described.

1. Your Teacher class has two fields called coursesTeaching and coursesNum, both of which sound like collections to me, and suggests another relationship:
"Teacher teaches Course",
where Course is a class containing (at least) a name and a number.

2. I suspect Teachers teach Students because they're enrolled in a Course they teach, so there's no direct "Teacher teaches Student" relationship.

3. There is, however, probably a "Student studies Course" relationship.

That in turn suggests to me that your "assignment" task has two parts:
1. Assigning (or adding) Courses to a Teacher.
2. Assigning (or adding) Students to a Course.





After that, you're on your own. :-)

HIH

Winston




The problem with my instructors is that they teach us something, and give us a task to do that they have not yet explained -_-.... I have come to know about the terms "composition" and "aggregation" by coincidence!
Here's the expected about that I should display.

https://gyazo.com/e93ba7a7852777bd021bbb6d7f59cc86
 
Joel Drake
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have done so far....

Person class---------------







Student class--------------------






Teacher class-----------------






Main class---------------






I'm initializing the constructors in the main class because I want to test it out first. I'm supposed to use setters for that, I also have some queries here.

1- Am I applying composition?
2- Why does the output show students for first teacher,and not for the second one?
https://gyazo.com/1dc484c3d8093af9a3ac6518940e5c46

3- How do I specify certain students to certain teachers???
 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joel Drake wrote:I'm initializing the constructors in the main class because I want to test it out first.


OK.

1- Am I applying composition?

Difficult question, but IMO: No.
Why?: Because you don't have a Course class.
Therefore you're trying to force your model into something that doesn't reflect reality.

I'll say it again, maybe a bit more forcefully:
A Teacher does NOT teach a Student unless they are on one of the Courses they teach.
Therefore, there is no intrinsic "Teacher [has-a] Student" relationship, EXCEPT through their Course.

Part of the business of composition is to get classes to work together, rather than through hierarchies. And you're missing a biggie: Course.

2- Why does the output show students for first teacher,and not for the second one?

Simple answer: Dunno. And until you get your model right, it probably doesn't matter.

3- How do I specify certain students to certain teachers???

Don't. See Point 1.

I hope it helps, but I fear that you've locked yourself into one way of thinking about this.

That said, you might be able to force a "programmatic" solution with what you've got. But it will be exactly that: a forced solution.

Winston
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!