Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35
Like Tree1Likes

Thread: Retrieve a Student or Delete it from TreeMap issue.

  1. #21
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Quote Originally Posted by jim829 View Post
    Note: If you retrieve a student instance and not a copy, then you can simply make changes to the instance and it will be reflected in the map.

    ...

    If you change their name for some reason, then you should probably delete the old record and add the new one since names are used as keys.
    More generally, don't change any state that reflects in equals(...) and hashCode() of an object used as a key in a Map.

    The generally recommended best practice is to use only instances of immutable classes as keys in a Map. Then you or anyone else can't ever make a mistake that would lead to difficult-to-debug problems.

    db
    jim829 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  2. #22
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Okay, last bit and I swear its over.... boolean!

    I need a boolean, I did this (among 50 other ways) but always get "false".. I need it to be a "yes" or "no"...
    Try 1:

    Java Code:
    private boolean Tenured;
    
        public String Tenured(boolean yesOrNo)
        {
            if(Tenured == true)
            {
                return "yes";
            }
            
            else {
                return "no";
            }
        }
    Where am I messing up now?

    And regarding previous question, I'm just updating info through student instance, I'll double the headache if I try adding it to the Directory instance, o thanks, that answered my question.

  3. #23
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    First, don't name methods and variables the same. It is confusing (and both should start with lower case by convention). Second, you are not using the passed boolean in the comparison. You are using an instance field named Tenured. Change Tenured in your method to yesOrNo. And don't use == with booleans. Just do
    if (yesOrNo)...

    And finally, the if/else is not necessary. The value can only be true or false so if it isn't true, it must be false.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #24
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Jim,

    I know this is gonna smack me in the face, (also, I'll correct caps in the end once it all "works").

    I can see I'm calling it wrong, in the 'public Faculty' and the toString methods I'm calling the "Tenured" string, yet I don't see the connection to the boolean per se... I'm drawing a blank here... again.

    Also, if I remove the if/else, how am I gonna set it to print 'yes' or 'no' specifically?

    Sorry about the 50 dumb questions of the day, and as usual, thanks for your patience.


    Java Code:
    /**
    *
    */
    public class Faculty extends Persons
    {
            private String FacOffice;
            private boolean Tenured;
        public Faculty(String Lname, String Fname, String Email, String FacOffice, String Tenured)
        {
            super(Lname, Fname, Email);
            this.FacOffice = FacOffice;
            this.Tenured = false;
        }
        
        public String isTenured(boolean yesOrNo)
        {
           if (yesOrNo)
               {
                   return "yes";
                }
                else
                {
                    return "no";
                }
        }
    
        public String getName()
        {
            return Lastname;
        }
    
        /**
         * Set a new name for this Faculty member.
         */
        public void changeName(String replacementName)
        {
            Lastname = replacementName;
            
        }
    
        @Override public String toString()
        {
            return Lastname + " " + Firstname + "  - E-Mail: " + Email + "  - Faculty Office: " + FacOffice + "  - Tenured: " + Tenured;
        }
    }

  5. #25
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    It just takes a little getting used to:

    Java Code:
    public String isTenured(boolean yesOrNo)    {
        if (yesOrNo)  // if true
            return "yes"; // return yes
        return "no";  // only other possibility
    }
    
    // You could also do it like this:
    public String isTenured(boolean yesOrNo) {
        return (yesOrNo) ? "yes" : "no";
    }
    By convention when you have methods prefixed with is, it is expected that they return a boolean. A better practice would be in your code to do this:

    Java Code:
    public boolean isTenured() {
        return tenured; // some boolean state in class
    }
    
    Then in your code you could do the following
    Faculty prof;
    // construct prof somewhere along the way
    
    String hasTenure = prof.isTenured() ? "Yes" : "No";
    System.out.println(hasTenure);
    And as always I recommend you write a few code snippets to check these out for yourself and vary the parameters.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #26
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    ok Jim,

    But whatever I type in the "tenured" field it prints "false", so I'm not setting that field correctly to return the boolean.

    Java Code:
    /**
    *
    */
    public class Faculty extends Persons
    {
            private String FacOffice;
            private boolean isTenured;
            private String tenured;
            
        public Faculty(String Lname, String Fname, String Email, String FacOffice, boolean isTenured)
        {
            super(Lname, Fname, Email);
            this.FacOffice = FacOffice;
            this.tenured = tenured;
    
        }
        
        public String isTenured(boolean yesOrNo)    {
            if (yesOrNo)  // if true
                return "yes"; // return yes
                return "no";  // only other possibility
        }
    
        
        public String getName()
        {
            return Lastname;
        }
    
        /**
         * Set a new name for this Faculty member.
         */
        public void changeName(String replacementName)
        {
            Lastname = replacementName;
            
        }
    
        @Override public String toString()
        {
            return Lastname + " " + Firstname + "  - E-Mail: " + Email + "  - Faculty Office: " + FacOffice + "  - Tenured: " + isTenured;
        }
    }
    Last edited by Polett; 08-02-2013 at 09:58 PM. Reason: Pasted old code

  7. #27
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Ok, I have to annotate your code to explain.

    Java Code:
    public class Faculty extends Persons
    {
            private String FacOffice;
            private boolean isTenured; // remove this (will replace it below)
            private String tenured; // why is this here??  you don't need it.
            private boolean tenured; // add this
             
        public Faculty(String Lname, String Fname, String Email, String FacOffice, boolean isTenured)
        {
            super(Lname, Fname, Email);
            this.FacOffice = FacOffice;
            this.tenured = tenured; // what is this?? where is tenured coming from?? remove it
    this.tenured = isTenured;  // add this.  intialize the instance field from the constructor
     
        }
         
        public String isTenured(boolean yesOrNo)    {
            if (yesOrNo)  // if true
                return "yes"; // return yes
                return "no";  // only other possibility
        }
     
         
        public String getName()
        {
            return Lastname;
        }
     
        /**
         * Set a new name for this Faculty member.
         */
        public void changeName(String replacementName)
        {
            Lastname = replacementName;
             
        }
     /**
     * see what I did with the isTenured method below
     */
        @Override public String toString()
        {
            return Lastname + " " + Firstname + "  - E-Mail: " + Email + "  - Faculty Office: " + FacOffice + "  - Tenured: " + isTenured(tenured);
        }
    }
    As I suggested before, it helps to play around with these constructs outside of any project. Also, check out the tutorials in my signature for further re-enforcement. If this doesn't work, then let me know and we'll work thru it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #28
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    BTW, in your previous post you still named your method name the same as an instance field. Don't do that. If you had named them differently, then the compiler would have caught the error in your toString() method where you referred to the field and not the method (assuming you intended to use the method of course).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #29
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Dear Jim,

    I will most definitely go over your tutorials as I like the simple way of how you explain/show things. I'm not too sure how deep I'll venture into JAVA, but at least you are giving me a bit more hope than I had 3 days ago.

    Now, (and you CANNOT laugh), in my Directory, I needed to create a method that prints just student list (which I did) and one that prints the full Directory (here is where it gets funny); I did this for the full Directory, but it won't print alphabetically as I'm calling each TreeMap individually... But shouldn't there be a simple way of just calling all Directory entries to print, and have them auto-sort by the getName()?


    Java Code:
        public void printDirectory()
        {
            Set<String> students = studentList.keySet();
            Set<String> staffs = staffList.keySet();
            Set<String> facs = facList.keySet();
            
            Iterator<String> itrstud = students.iterator();
            Iterator<String> itrsta = staffs.iterator();
            Iterator<String> itrfac = facs.iterator();
    
           System.out.println("Full Directory List: ");
            
            while(itrstud.hasNext()) {
                Student student = studentList.get(itrstud.next());
                System.out.println(student.toString());
                //System.out.println();
            }
        
            while(itrsta.hasNext()) {
                Staff staff = staffList.get(itrsta.next());
                System.out.println(staff.toString());
                //System.out.println();
                }
    
            while(itrfac.hasNext()) {
                Faculty faculty = facList.get(itrfac.next());
                System.out.println(faculty.toString());
                //System.out.println();
                }
    
        }

  10. #30
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Pretty obvious answer: you want to apparently print ONE directory so you'd need ONE treeset.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #31
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Isn't a TreeMap somewhat (not in every way) to an ArrayList? If I have 3 separate classes, don't I need a TreeMap for each?

    Otherwise how do I tell it to print from where?

  12. #32
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Actually, they are not my tutorials. They are from Oracle. I just point to them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #33
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    It depends on how you implement it and what the requirements are of the assignment. Which class is "in charge" or "owns" the map. Is it the directory? You add values to the map. But not each map in each class would necessarily be updated. What are the roles and responsibilities of all the classes involved?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #34
    Polett is offline Member
    Join Date
    Aug 2013
    Posts
    15
    Rep Power
    0

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Lets see if I can explain it...

    I have a Directory, to that Directory I add Students, Staff & Teachers (for which I have individual classes as you've seen suffering through this with me). In the Directory Instance I add, remove and view people's info. I also need to print a Students list, as well as a full Directory List. What I showed u above is my code for the full list.

    Now, since I'm trying to gather everyone I added to the Directory (Students, Staff, Fac) how can I get a printout of their info without calling each TreeMap individually? I tried doing a Directory TreeMap, but that returned an empty list...

  15. #35
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Retrieve a Student or Delete it from TreeMap issue.

    Create a TreeMap of type String, Person (since Students, Faculty, and Staff are subtypes of Person) Then do a putAll of each existing TreeMap into this one.
    Then simply iterate thru the map like you normally do, printing out toString().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 1
    Last Post: 02-11-2013, 02:29 PM
  2. Treemap's
    By Russo in forum New To Java
    Replies: 6
    Last Post: 10-11-2009, 02:13 AM
  3. Replies: 2
    Last Post: 04-20-2009, 09:00 AM
  4. Delete Student from Array Help
    By kazit in forum New To Java
    Replies: 4
    Last Post: 02-25-2009, 03:42 AM
  5. Store retrieve and delete
    By on7june in forum New To Java
    Replies: 1
    Last Post: 03-08-2008, 06: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
  •