reset password
Author Message
cysun
Posts: 2935
Posted 13:10 Apr 12, 2009 |

Consider the following code for the coding exercise in Homework 1:

(1) Set<User> moderators = forum.getModerators();
(2) moderators.add( user );
(3) forum.setModerators( moderators );
(4) forumDao.saveForum( forum );

Is line (3) necessary? And why?

The first person who answer the questions correctly receive 5pt extra credit for Homework 1.

jadiagaurang
Posts: 53
Posted 13:24 Apr 12, 2009 |

Hello Dr. Sun,

According to my understanding line 3 is needed. I submitted like this,

1. I check that current user is Moderator by method "isModerator(User);". Is user is not then go into IF statement.

2. get current all moderator in to Set<User> of forum by method "getModerators();"

3. add corrent user in to that Set<User>

4. NOW, that local set of user should be set back by method setModerators(Set<User>). So, line 3 is needed...!!!

5. Update forum by using Data Access Object of forum.

This is my understanding...!!! Undecided

 

Last edited by jadiagaurang at 17:20 Apr 12, 2009.
rlim
Posts: 1
Posted 13:28 Apr 12, 2009 |

Yes the previous comment is correct.  When you get the moderators, it is a local copy, and you need line three to copy the modified set.  Then line four will commit to the database.

jadiagaurang
Posts: 53
Posted 13:36 Apr 12, 2009 |

I need to explain more to clean my understanding.

Well, we get all current moderators by method getModerators() in to one local Set of users which is Set<User> moderators. We add current user (user is always instructor for sure) into that Set<User> in second line. Now method setModerators(Set<User>) is add that updated Set<User> in to forum object. and Data Access Objec (ForumDao) will commit change to database.

abhijeet_ubale
Posts: 10
Posted 13:46 Apr 12, 2009 |
cysun wrote:

Consider the following code for the coding exercise in Homework 1:

(1) Set<User> moderators = forum.getModerators();
(2) moderators.add( user );
(3) forum.setModerators( moderators );
(4) forumDao.saveForum( forum );

Is line (3) necessary? And why?

The first person who answer the questions correctly receive 5pt extra credit for Homework 1.

Line #3 is not required. Here is something you can try:

Source code:

         java.util.Set<User> moderators = forum.getModerators();
         moderators.add( user );
         logger.info("moderators.size() 1 =" + moderators.size());
         logger.info("forum.getModerators() 1 =" + forum.getModerators().size());
         forum.setModerators( moderators );
         logger.info("moderators.size() 2 =" + moderators.size());
         logger.info("forum.getModerators() 2 =" + forum.getModerators().size());
         forumDao.saveForum( forum );

Log file:

2009-04-12 13:49:50,790  INFO AddSectionController: moderators.size() 1 =1
2009-04-12 13:49:50,800  INFO AddSectionController: forum.getModerators() 1 =1
2009-04-12 13:49:50,800  INFO AddSectionController: moderators.size() 2 =1
2009-04-12 13:49:50,800  INFO AddSectionController: forum.getModerators() 2 =1

Variable "moderators (AddSectionController.java)" is a reference to the "moderators (Forum.java)" variable, hence set isn't required. In the log you would notice that the count before the set and after the set of the variable is exactly the same.

Regards,

 

Last edited by abhijeet_ubale at 13:54 Apr 12, 2009.
PoonamPawar
Posts: 16
Posted 13:59 Apr 12, 2009 |
cysun wrote:

Consider the following code for the coding exercise in Homework 1:

(1) Set<User> moderators = forum.getModerators();
(2) moderators.add( user );
(3) forum.setModerators( moderators );
(4) forumDao.saveForum( forum );

Is line (3) necessary? And why?

The first person who answer the questions correctly receive 5pt extra credit for Homework 1.

I think, line number 3 is not required.

Here is my code:

      Set<User> oCurrentModerators = forum.getModerators();
        oCurrentModerators.add(user);
        forumDao.saveForum(forum);
        logger.info("User name: " + user.getName() + "is moderator of the Course: " + course.getName());

I also checked it in the forum_moderators table after clicking "add section" on the page.


       

migcov
Posts: 10
Posted 14:28 Apr 12, 2009 |

No it's not required. Method add will add that user to the set of moderatos, then method call to save will save that set back to the database.

Last edited by migcov at 14:30 Apr 12, 2009.
cysun
Posts: 2935
Posted 15:11 Apr 12, 2009 |

Abhijeet is right - Line (3) is not needed. The key word here is "reference". Understanding references in OO languages is important.

Let's try one more. Consider the following code:

if( ! forum.isModerator(user) )
{ 
    forum.getModerators().add(user);
    forumDao.saveForum(forum);
}

Is the if condition necessary? and why? This question worth 3pt.

Last edited by cysun at 15:12 Apr 12, 2009.
dhavalpatels
Posts: 9
Posted 15:35 Apr 12, 2009 |

I think the IF statement is not required because of add method of set.

Add method adds the element to the set if its not already present.


migcov
Posts: 10
Posted 15:47 Apr 12, 2009 |

The answer to this question is YES. The reason is because if user is moderator the code inside the if statement is not executed. If user is not moderator, then the code is executed. The code inside the if statement adds this user as a moderator to the moderator set. However, if the IF statement was removed, then a user already added as moderator could presumably be added again and again as many times as the two lines are executed, without the IF statement. So yes, if statement is needed. 

Regards,  

abhijeet_ubale
Posts: 10
Posted 15:47 Apr 12, 2009 |
dhavalpatels wrote:

I think the IF statement is not required because of add method of set.

Add method adds the element to the set if its not already present.


The IF statement is not required. Even if you add the moderator without checking, it does not add the record to the forum_moderators table with an additional entry. But I feel it is a good practice to check if the moderator exists before adding him to the collection, assuming the DB constraints may/may not be in place.

Regards,

Abhijeet

Last edited by abhijeet_ubale at 15:59 Apr 12, 2009.
jadiagaurang
Posts: 53
Posted 16:11 Apr 12, 2009 |

I check code without IF statement and it is working fine without error. I think that add method of class java.util.set adds if it is not already present in set. So, if user already exist in set then user does not add again. So, we don't need IF statement.

I am not sure but I think that table "forum_moderators" has primary key, which is combination of forum_id and user_id ( ...CONSTRAINT forum_moderators_pkey PRIMARY KEY (forum_id, user_id), ...). If table already has this conbination then PostgreSQL does not add it again.

ellen214
Posts: 11
Posted 17:13 Apr 12, 2009 |

my god, they are the same as my code

is it wrong?

my thought is like this

1 I declare  "moderators" to get the moderators. this set is individual (i declare it).

2. "add" function comes from "set", not from the model or forumDao so that it won't effect the real database and model.

3  "set" function comes from forum model so that it doesn't effect database. it effects forum model in order to call "save" from forumDao.

4 "save" function comes from forumDao interface, this is the real action to change back to the database. save function take forum model as the intput, so line 3 is necessary.

i don't really understand hibernate, so i am not sure if i did it right.

Last edited by ellen214 at 17:17 Apr 12, 2009.
cysun
Posts: 2935
Posted 17:35 Apr 12, 2009 |
dhavalpatels wrote:

I think the IF statement is not required because of add method of set.

Add method adds the element to the set if its not already present.

Dhaval is right.

It's important to understand the API that you are using.

And this is not a database issue.