CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    78

    Question about function

    I'm trying to practice a little, but the book dosen't give me the solution. There's still one function missing in this piece of code and I'm wondering hwo I complete it.

    Code:
    public class Person
    {
    private String firstName;
    private String lastName;
    private String ID; // user ID
    private String password;
    private int userType; // 1 student 2 faculty
    
    
    public Person()
    {
    
    }
    public Person(String ID, String firstName, String lastName, String password, int userType)
    {
    this.ID = ID;
    this.firstName = firstName;
    this.lastName = lastName;
    this.password = password;
    this.userType = userType;//1 faculty 2 student
    }
    public String toString()
    {
    return ID+ " " + Application.addSpaces((firstName + " " + lastName),18);
    }
    public boolean equals(Object theOtherObject)
    {
    // YOU CAN CHANGE EVERYTHING IN THIS FUNCTION INCLUDING THE STAMENTS:
    // boolean result = false;
    // return result;
    
    boolean result = false;
    // INCOMPLETE FUNCTION
    // ooooooooooooooooooo
    // oooooooo ooooooooo
    // oooooooo ooooooooo
    // oooooooo ooooooooo
    // oooooooo ooooooooo
    // ooooooooooooooooooo
    // oooooooo ooooooooo
    // ooooooooooooooooooo
    
    return result;
    }
    public boolean validation(String ID, String password)
    {
    return ( this.ID.equals(ID)&& this.password.equals(password) );
    }
    public int getUserType()
    {
    return userType;
    }
    public String getUserID()
    {
    return ID;
    }
    }
    I suppose it's something like this...

    Code:
    public boolean equals(Object theOtherObject)
    {boolean result = false;
    if((theOtherObject!=null) && (theOtherObject instanceof Person))
    {
    
    Person otherPerson = (Person) theOtherObject;
    if((this.firstName.equals(otherPerson) *****&& (this.userType== otherPerson.userType))
    {
    result = true;
    }
    
    return result;
    }
    }
    Do I just have to compare those attributes?

    this.ID = ID;
    this.firstName = firstName;
    this.lastName = lastName;
    this.password = password;
    this.userType = userType

    So I would have to modify this line here:

    if((this.firstName.equals(otherPerson) *****&& (this.userType== otherPerson.userType))

    Do I just have to add them now? thank you!

  2. #2
    Join Date
    Oct 2008
    Posts
    50

    Re: Question about function

    It really depends on what you are using that method for and when you consider two "person" to be the same. For instance, if you know for a fact that the ID will ALWAYS be unique to each person, then that's really all you need to compare. So the method would be:
    Code:
    public boolean equals(Object theOtherObject)
    {
        boolean result = false;
        if((theOtherObject!=null) && (theOtherObject instanceof Person))
       {
            Person otherPerson = (Person) theOtherObject;
            if(ID == otherPerson.ID)
            {
                 result = true;
            }
       }
       return result;
    }
    However, if two different person can have the same ID, then you should compare more things. If you really want to be safe, you can compare everything. Do note that comparing everything would mean that two "person" with the same first and last name, same userType and same ID would be considered different person if they have different passwords.
    Last edited by Filobel; April 1st, 2011 at 02:24 PM.

  3. #3
    Join Date
    Mar 2009
    Posts
    78

    Re: Question about function

    Thanks! There's another piece of code, which I probably should have posted, too.

    public class Application
    {
    private Scanner keyboard;
    private Person[] userDatabase;
    private Course[] courseDatabase;
    private int numberOfUsers;
    private int numberOfCourses;
    private Person currentUserObject;
    public static String addSpaces(String input, int fullLength)
    {
    String output= input;
    int extraZeros = fullLength-input.length();
    for(int i=0; i<extraZeros; i++)
    {
    output = output +" ";
    }
    return output;

    }
    public Application()
    {

    currentUserObject = null;
    userDatabase = new Person[20];
    courseDatabase = new Course[10];
    numberOfUsers = 0;
    numberOfCourses = 0;
    keyboard = new Scanner(System.in);
    addRecords();

    }
    private void addRecords()
    {
    // Create Users
    userDatabase[numberOfUsers++] = new Student("92038201", "Ilyas", "Uyanik", "Computer Science", "1956", 2, 1);//0
    userDatabase[numberOfUsers++] = new Student("20483002", "Joseph", "Mathew", "Computer Science", "1234", 3, 1);//1
    userDatabase[numberOfUsers++] = new Student("26023923", "Suzan", "Pink", "Computer Science", "2342", 3, 1);//2
    userDatabase[numberOfUsers++] = new Student("34532343", "Amy", "Yellow", "Computer Science", "4533", 1, 1);//3
    userDatabase[numberOfUsers++] = new Student("78643546", "George", "Black", "Computer Science", "0495", 4, 1);//4
    userDatabase[numberOfUsers++] = new Student("45345753", "Fatih", "Haberdar", "Computer Science", "5478", 1, 1);//5
    userDatabase[numberOfUsers++] = new Student("23748967", "Murat", "Erdogan", "Computer Science", "9043", 2, 1);//6
    userDatabase[numberOfUsers++] = new Student("32946262", "Sam", "Blue", "Computer Science", "3241", 4, 1);//7


    userDatabase[numberOfUsers++] = new Faculty("52037101", "Hakan", "Haberdar", "Computer Science", "1267", 70000.0, 2);//8
    userDatabase[numberOfUsers++] = new Faculty("30293102", "Bill", "Brown", "Computer Science", "7810", 100000.0, 2);//9
    userDatabase[numberOfUsers++] = new Faculty("10132103", "John", "White", "Computer Science", "3724", 60000.0, 2);//10
    userDatabase[numberOfUsers++] = new Faculty("32546104", "Ayse", "Red", "Computer Science", "2938", 80000.0, 2);//11
    userDatabase[numberOfUsers++] = new Faculty("98232105", "Emre", "Charu", "Computer Science", "2302", 83000.0, 2);//12
    // Create Courses
    courseDatabase[numberOfCourses++] = new Course("1320", "Intro to Computer Science", "52037101", 4);
    courseDatabase[numberOfCourses++] = new Course("1410", "Data Structures", "10132103", 4);
    courseDatabase[numberOfCourses++] = new Course("2430", "Artificial Intelligence", "32546104", 3);
    // Enrollment
    ((Student)userDatabase[0]).addClass(courseDatabase[0]);
    ((Student)userDatabase[1]).addClass(courseDatabase[0]);
    ((Student)userDatabase[2]).addClass(courseDatabase[0]);
    ((Student)userDatabase[3]).addClass(courseDatabase[0]); // 1320, full

    ((Student)userDatabase[4]).addClass(courseDatabase[1]);
    ((Student)userDatabase[5]).addClass(courseDatabase[1]); // 1410, 2 available

    ((Student)userDatabase[6]).addClass(courseDatabase[2]);
    ((Student)userDatabase[7]).addClass(courseDatabase[2]); // 2430, 1 available
    }
    private int existingCourse(String courseNumber)
    {
    int result = -1;
    for(int i=0; i<numberOfCourses;i++)
    {
    if( courseDatabase[i].getNumber().equals(courseNumber) )
    {
    result = i;
    }
    }
    return result;

    }
    private boolean existingUser(String ID)
    {
    // RETURN FALSE IF THERE IS NO USER WITH THE GIVEN ID
    // RETURN TRUE OTHERWISE
    // YOU CAN CHANGE EVERYTHING IN THIS FUNCTION INCLUDING THE STAMENTS:
    // boolean result = false;
    // return result;
    boolean result = false;
    // INCOMPLETE FUNCTION
    // ooooooooooooooooooo
    // oooooooo ooooooooo
    // oooooooo ooooooooo
    // oooooooo ooooooooo
    // oooooooo ooooooooo
    // ooooooooooooooooooo
    // oooooooo ooooooooo
    // ooooooooooooooooooo
    return result;
    I suppose you only need to check for the students ID because it's a unique number. So, I think your posted code would work just fine. There's another function missing in this code. I don't see what I would do there? Do you know?

  4. #4
    Join Date
    Oct 2008
    Posts
    50

    Re: Question about function

    Note that in your case, although the IDs ARE all unique, there is nothing that actually assures you it will always be that way. I don't know if this is an assignment or what, but depending on what you want to do, it may be safer to compare everything.

    As for your other question, I don't think you have any other choice but to loop through all the values and check their IDs. You could have used a hashmap or treemap instead of an array for your "userdatabase", that would have made your method easier and faster.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Question about function

    Quote Originally Posted by Filobel
    You could have used a hashmap or treemap instead of an array for your "userdatabase", that would have made your method easier and faster.
    As the OP has provided their own implementation of the equals() method they will need to override the hashcode() method as well before using any collection that calls the object's hashcode() method.

    The API docs for equals() say: "Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes."
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Mar 2009
    Posts
    78

    Re: Question about function

    No, I think it sufficient.. according the the problem in the book. I still don't know what's wrong with the second code. It's an intro to computer science book. I don't think I have to override it.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Question about function

    There's another function missing in this code. I don't see what I would do there? Do you know?
    It tells you what you need to do ie:

    // RETURN FALSE IF THERE IS NO USER WITH THE GIVEN ID
    // RETURN TRUE OTHERWISE
    So what is your problem?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Mar 2009
    Posts
    78

    Re: Question about function

    Quote Originally Posted by keang View Post
    It tells you what you need to do ie:

    So what is your problem?
    Ok, didn't pay much attention to that part. Overlooked it. So, I can just do a loop here?

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Question about function

    Before you make guesses about the code constructs you need to use, think about what you would do if you were doing this with a pen and paper.

    So imagine I ring you up several times and give you some student details which you dutifully write down. Later I ring back and say do you have a student with ID XXX. What do you have to do to be able to answer me?

    Once you've worked out how you would answer the question from your written notes, convert that process to code.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured