Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default [SOLVED] Java container class misunderstood

    hey guys, i recently learnt about arrays and all and now we supposed to make a container class that organizes objects...problem is i dont wanna copy and paste and still havent been able to understand the concept and the layout. Please help. here's an example code i got:

    Java Code:
    public class FriendList // a container class
    {
    	static final int MAX_SIZE = 5;
    	//attributes
    	private String[] names; // array of string for storing a collection of names
    	private int count;      // keeping track of elements stored
    	
    	
    	public FriendList (int size)
    	{
    		if (size > 1)
    			names = new String[size];
    		else
    			names = new String[MAX_SIZE];
    			
    			
    		count = 0;	
    	} 
    	
    	public boolean add(String aName)
    	{
    		// check for free space
    		if (count >= names.length)
    			return false;
    		names[count] = aName;
    		count++;
    		return true;
    	}
    	
    	public String longestName()
    	{
    		String longestName = names[0];
    		for (int i = 0; i < count; i++) //we dont use ".length" instead we use "count"
    			if (names[i].length() > longestName.length())
    				longestName = names[i];
    		return longestName;
    	}
    	
    	public String namesStartWith(String str)
    	{
    		String result = "";
    		for (int i = 1; i < count; i++)
    			if (names[i].startsWith(str))
    				result = result + names[i] + "\n";
    		return result;
    	}
    	
    	public int noOfFriends()
    	{
    		return count;
    	}
    }

  2. #2
    vijayabaskar is offline Member
    Join Date
    Apr 2009
    Posts
    37
    Rep Power
    0

    Default

    where is main

  3. #3
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by vijayabaskar View Post
    where is main
    this is a container class code....not a program...a program uses the methods in this one to manipulate the data stored in the array.

    Just for looks..the main is this:
    Java Code:
    import java.util.Scanner;
    public class FnMain
    {
    	public static void main(String[] args)
    	{
    		Scanner key = new Scanner(System.in);	
    		
    		FriendList list = new FriendList(5);
    		System.out.println("No of friends is : " + list.noOfFriends());
    	
    		for (int i = 1; i <= 5; i++)
    		{
    			System.out.print("Enter name of friend " + i + ": ");
    			String name = key.nextLine();
    			
    				if (list.add(name) == true )
    					System.out.println("Success");
    				else
    					System.out.println("Addition failed, no more free space");
    		}
    		
    		System.out.println("The longest name is : " + list.longestName());
    	
    		System.out.print("Enter prefix: ");
    		String prefix  = key.nextLine();
    		String str = list.namesStartWith(prefix);
    		if (str.equals(" "))
    			System.out.println("No friends start with '" +  prefix + " '");
    		else
    			System.out.println("Friends: \n" + str);
    	}
    	
    }

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    what in particular did you want help with? without running or reading too much into it, it looks like it works how it's intended.
    Last edited by emceenugget; 04-06-2009 at 06:56 PM. Reason: didn't read close enough...

  5. #5
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    what in particular did you want help with? without running or reading too much into it, it looks like it works how it's intended.
    It works okay....what i wanted was EXPLANATION of the container class. Can you do that??...i need more to understand it...like maybe the default format of a container class..etc.

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    the purpose of the 'container' is the same as having any class. it's the concept of encapsulation/modularity, where basically you have a black box (your class). you give it input and it gives you output, where you aren't concerned with what is occurring behind closed doors. you can say "add this", "give me the longest name" and "what name starts with this" and get the correct answer without concerning yourself with how to do it outside of the class.

  7. #7
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    the purpose of the 'container' is the same as having any class. it's the concept of encapsulation/modularity, where basically you have a black box (your class). you give it input and it gives you output, where you aren't concerned with what is occurring behind closed doors. you can say "add this", "give me the longest name" and "what name starts with this" and get the correct answer without concerning yourself with how to do it outside of the class.
    okay, can you gimme a format for these "container" classes? Thanks for the info.

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    there really isn't a format for anything. you just implement what you need (or what is required). you can read up on collections at the link below for ideas and more info. if you haven't dealt with maps, lists, etc., you can also gain experience using those as they're some of the most frequently used classes in java.

    Trail: Collections (The Java™ Tutorials)

  9. #9
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    there really isn't a format for anything. you just implement what you need (or what is required). you can read up on collections at the link below for ideas and more info. if you haven't dealt with maps, lists, etc., you can also gain experience using those as they're some of the most frequently used classes in java.

    Trail: Collections (The Java™ Tutorials)
    Thanks dude, thats a lot of help. Thanks

Similar Threads

  1. Applet as container .
    By pawankumarom in forum Java Applets
    Replies: 5
    Last Post: 04-23-2009, 07:17 AM
  2. AWT can we make a Tabbed container?
    By Panchitopro in forum AWT / Swing
    Replies: 0
    Last Post: 05-15-2008, 11:31 PM
  3. Container Managed Persistence
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-27-2007, 11:16 AM
  4. Butterfly Container 1.9.9-beta
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-17-2007, 03:02 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
  •