Results 1 to 3 of 3
Thread: Help with clarify some code
- 01-26-2013, 05:35 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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; }
- 01-26-2013, 06:23 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-26-2013, 06:45 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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:
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 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 (); } }
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
-
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
Can someone clarify this piece of code for me.
By DavidPynes in forum New To JavaReplies: 2Last Post: 04-17-2011, 10:22 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks