Results 1 to 3 of 3
  1. #1
    shinju is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Help with clarify some code

    hello,

    I am currently studying in class of Object Oriented Program were we are doing a simple program, however, I have some doubts that I could not clarify in classes.

    we have a menu for adding some information about cat. This menu leads to addcat method. the method addcat has only this part of the code, however, I can not understand where we are asking cat information to the user and exactly why we use the following code

    Java Code:
    private boolean addCat (Cat cat)
    
    {
    
    if (catExists (cat.getId ()))
    return false;
    
    catList.add (cat);
    return true;
    
    }
    
    private boolean catExists (String id)
    
    {
    for (Cat d: catlist)
    if (d.getId (). equalsIgnoreCase (id))
    return true;
    return false;
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default Re: Help with clarify some code

    That part of the code shouldn't ask information from the user; all the code does is check if a Cat is already in a list and if not, add it to the list. The second method does the checking for a Cat being present in a list. Another part of the code (which you should design and implement) should ask the user for the necessary information. This 'split' is called 'separtion of concerns' and it's a good thing.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    shinju is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help with clarify some code

    Thanks for you help. It was actually what I was thinking.

    So, I it should have a main method that ask the information from the user. However there is some restrictions in another separate program of how the id should be:
    Java Code:
     //PROGRAM A CATS
    private static String id;
    			public Cat(String id) throws Exception
    		{
    			
    			setId(id);
    			
    		}
    		
    	public String getId()
    	{
    		return id;
    	}
    	
    	public void setId(String id) throws Exception
    	{
    		if (id.length()==5 &&
    		   Character.isDigit(id.charAt(0)) && 
    		   Character.isLetter(id.charAt(1)) && 
    		   Character.isDigit(id.charAt(2)) && 
    		   Character.isDigit(id.charAt(3)) && 
    		   Character.isDigit(id.charAt(4)))
    						
    						{
    							this.id = id;
    						}
    		else
    		{			
    			throw new Exception ();
    		}						
    	 }
    And then we have the program that I show you. I create a new main method that ask for the information, but it does not do the validation. I cannot understand how it will go to the other program and do the validation of the restriction of the ID and then come back to this one and validate that this cat is not in the list. And if we have the variables in the Cats program (PROGRAM A) why eclipse tells me that I have to create again the variables.

    Java Code:
    //Program B 
    
     {
      String id;
    
      Scanner keyboard = new Scanner(System.in);
      
      System.out.println("Enter ID:");
      id = keyboard.nextLine();
       
     }
    
    private boolean addCat (Cat cat)
     
    {
     
    if (catExists (cat.getId ()))
    return false;
     
    catList.add (cat);
    return true;
     
    }
     
    private boolean catExists (String id)
     
    {
    for (Cat d: catlist)
    if (d.getId (). equalsIgnoreCase (id))
    return true;
    return false;
    }
    Last edited by shinju; 01-26-2013 at 06:53 PM.

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  2. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  3. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  4. Can someone clarify this piece of code for me.
    By DavidPynes in forum New To Java
    Replies: 2
    Last Post: 04-17-2011, 10:22 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •