Results 1 to 7 of 7

Thread: Passing Objects

  1. #1
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Question Passing Objects

    Hello dear developers

    I am desigining & implementing a system that follows these classes: News papers awhich they are exchanging news using news agencies. A news paper register itself at a news agency and sends their news to that agency. The news agency receives news and sends them to all registred news papers.
    I have four classes News,Newspaper,Main,Agency.

    I have send the newspaper and the news to the agency but I don't know how can I list of all newspapers and forward the news to other newspaper.

    Can anyone help me please?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The Agency would likely have a collection of Newspapers, perhaps an ArrayList or a HashMap, and I imagine that to broadcast to all the newspapers the agency would simply iterate through its collection, sending to each paper in the collection.

  3. #3
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Question

    Java Code:
             private ArrayList<NewsPaperD> list = new ArrayList<NewsPaperD>();
    	private NewsPaperD getNewsPaperData(NewsPaper np,News n)
    	{
    		String s = n.getNews(); 
    		
    		for(NewsPaperD nd:list)
    		{
    			if(nd.getNewsPaper()==np )
    				return nd; 
    			
    		}
    		
    		System.out.println("Newspaper: "+np);
    		NewsPaperData nd=new NewsPaperData(np,s);
    		for(int i=0;i<list.size();i++)
    		{
    			list.set(i, nd);
    		}
    		list.add(nd);
    		return nd;
    	}
    I have done like above and the problem is that right now it will only show the first NEWS related to each NewsPaper.

    I have added the result also:

    PHP Code:
    Newspaper: Local
    	Local: News = Title1  nNewsPaper: Local, tTitle = Title1
    	Local: News = Title2  nNewsPaper: Local, tTitle = Title1
    	Local: News = Title3  nNewsPaper: Local, tTitle = Title1
    	
    Newspaper: Times 
    	Times : News = News1  nNewsPaper: Times , tTitle = News1
    	Times : News = News2  nNewsPaper: Times , tTitle = News1
    	Times : News = News3  nNewsPaper: Times , tTitle = News1
    Can you please help me with that?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I don't really know what you're trying to do with this snippet of code or really what your problems are.

    Please have a look here as it may show you how to avoid making assumptions about what we know about your current problem, so that your questions may be more understandable: smart questions

  5. #5
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    Ok sorry for that.

    actually my problem is that I don't know how can I send an object to other objects. and you have guided me in this way.

    The Agency would likely have a collection of Newspapers, perhaps an ArrayList or a HashMap, and I imagine that to broadcast to all the newspapers the agency would simply iterate through its collection, sending to each paper in the collection.

    I have followed the steps you mentioned like this :
    Java Code:
    //collection of Newspapers
    private ArrayList<NewsPaperD> list = new ArrayList<NewsPaperD>();
    //broadcast to all the Newspaper from agency
    Java Code:
    	private NewsPaperD getNewsPaperData(NewsPaper np,News n)
    	{
    		String s = n.getNews(); 
    		
    		for(NewsPaperD nd:list)
    		{
    			if(nd.getNewsPaper()==np )
    				return nd; 
    			
    		}
    		
    		System.out.println("Newspaper: "+np);
    		NewsPaperData nd=new NewsPaperData(np,s);
    		for(int i=0;i<list.size();i++)
    		{
    			list.set(i, nd);
    		}
    		list.add(nd);
    		return nd;
    	}
    But as I have mentioned the result is just the first News that each Newspaper has which is broadcasted to the same Newspaper which is wrong I want to send all the News that each Newspaper have to all other NewsPapers. Can you please guide me?

    Here is the result that right now I got.
    PHP Code:
    Newspaper: Local 
        Local: News = Title1  NewsPaper: Local, News = Title1 
        Local: News = Title2  NewsPaper: Local, News = Title1 
        Local: News = Title3  NewsPaper: Local, News = Title1 
         
    Newspaper: Times  
        Times : News = News1  NewsPaper: Times , News = News1 
        Times : News = News2  NewsPaper: Times , News = News1 
        Times : News = News3  NewsPaper: Times , News = News1

  6. #6
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    What you want to do is have a method like getNewsPaperData, and have that method be public. Then, each NewsPaper should have a List of all the other newspapers it receives data from. To get data from all of its newspapers that it's subscribed to, create a method called getAllNewspaperData() which loops through the List and calls getNewsPaperData on each one.

    getNewsPaperData would simply return the news from each Newspaper.

    It may also help if you tell us what all of the classes you use are. You listed them in your first post but it would be better if we knew what each one actually did. I also see a NewsPaperData object, and I'm having trouble figuring out why you'd create a new NewsPaperData object inside of your method.
    Last edited by AlbertoPL; 10-29-2009 at 01:23 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your getNewsPaperData method confuses me again as it does not appear to be a method for broadcasting to all papers. First it takes a NewsPaper parameter, np (what is the difference by the way between the NewsPaper class and the NewsPaperD class?), and then iterates through the list looking for this paper. If it finds this newspaper, the method returns it and ends, end of story, and no broadcast occurs. That doesn't make sense. I'm also a little concerned that you're using == to check for equality rather than the equals method as most all your classes should have a decent equals override (and perhaps a hashcode override as well).

    If I would have a newspaper broadcast method, it would look something like:

    Java Code:
    private void broadcastNews(News news) {
       for (NewsPaperD nd : list) {
          nd.getNewsPaper().receiveNews(news); // or whatever method you use to send news to a newspaper
       }
    }

Similar Threads

  1. MVC passing objects
    By simo_mon in forum New To Java
    Replies: 1
    Last Post: 07-17-2009, 06:57 AM
  2. Passing objects into constructors
    By aaronfsimons in forum New To Java
    Replies: 8
    Last Post: 04-14-2009, 12:08 PM
  3. help with passing objects between classes
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-22-2009, 02:41 PM
  4. Passing objects in Java
    By jbostjr in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 10:15 PM
  5. Passing objects in Java
    By jbostjr in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2007, 05:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •