Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Help for dynamic memory allocation.

  1. #1
    R3.RyozKidz Guest

    Help for dynamic memory allocation.

    Here are my codes
    Code:
    public class Employee
    {
        protected String name;
        protected int idNumber;
        protected int age;
        protected double salary;
        protected String title;
        protected String department;
        private Employee nextEmployee;
        
        public Employee(String name , int idNumber , int age , double salary , String title , String department)
        {
            this(name , idNumber , age , salary , title , department , null);
        }
        
        public Employee(String name , int idNumber , int age , double salary , String title , String department , Employee next)
        {
            this.name = name;
            this.idNumber = idNumber;
            this.age = age;
            this.salary = salary;
            this.title = title;
            this.department = department;
            this.nextEmployee = next;
        }
        
        public String getName()
        {
            return name;
        }
        
        public int getID()
        {
            return idNumber;
        }
        
        public int getAge()
        {
            return age;
        }
        
        public double getSalary()
        {
            return salary;
        }
        
        public String getTitle()
        {
            return title;
        }
        
        public String getDepartment()
        {
            return department;
        }
        
        public String toString()
        {
            return String.format("%s: %s\n%s: %d\n%s: %d\n%s: %.2f\n%s: %s\n%s: %s\n" , "Name" , getName() , "ID" , getID() , "Age" , getAge() , "Salary" , getSalary() , "Title" , getTitle() , "Department" , getDepartment());
        }
        
        public void updateSalary(int amount)
        {
            this.salary += amount;
        }
        
        public void updateSalary(double rate)
        {
            double temp = this.salary * rate;
            this.salary += temp;
        }
        
        public void setNextEmployee(Employee next)
        {
            nextEmployee = next;
        }
        
        public Employee getNextEmployee()
        {
            return nextEmployee;
        }
        
        public void setSalary(double salary)
        {
            this.salary = salary;
        }
    }
    Code:
    public class ListOfEmployee
    {
        private Employee first;
        private Employee last;
        private static int i =0;
        
        public ListOfEmployee()
        {
            first = last = null;
        }
        
        public void addEmployee(String name , int idNumber , int age , double salary , String title , String department)
        {
            if(isEmpty())
            {
                first = last = new Employee(name , idNumber , age , salary , title , department);
                return;
            }
            else
            {
                last = last.getNextEmployee();
                last = new Employee(name , idNumber , age , salary , title , department);
            }
        }
        
        public void removeEmployee(String name , int id , String department)
        {
            if(isEmpty())
            {
                return ;
            }
            
            if(first == last)
            {
                first = last = null;
                return;
            }
            
            Employee firedEmployee = first;
            Employee previousEmployee = first;
            Employee lastEmployee = last;
            
            //to check whether the first employee is the one who is going to be fired
            if(name.equalsIgnoreCase(firedEmployee.getName()) && firedEmployee.getID() == id && department.equalsIgnoreCase(firedEmployee.getDepartment()))
            {
                first = firedEmployee.getNextEmployee();
                firedEmployee = null;
                return;
            }
            
            //to check whether the last employee is the one who is going to be fired
            if((name.equalsIgnoreCase(lastEmployee.getName())) && firedEmployee.getID() == id && department.equalsIgnoreCase(firedEmployee.getDepartment()))
            {
                while(firedEmployee.getNextEmployee() != lastEmployee)
                {
                    firedEmployee = firedEmployee.getNextEmployee();
                }
                last = firedEmployee;
                firedEmployee = firedEmployee.getNextEmployee();
                firedEmployee = null;
                return;
            }
            
            //since the first and the last employee are not the one who is being fired .
            firedEmployee = firedEmployee.getNextEmployee();
            previousEmployee = firedEmployee;
            
            while((name.equalsIgnoreCase(firedEmployee.getName()) == false )&& firedEmployee.getID() != id && (department.equalsIgnoreCase(firedEmployee.getDepartment()) == false))
            {
                firedEmployee = firedEmployee.getNextEmployee();
                if((name.equalsIgnoreCase(firedEmployee.getName()) == false) && firedEmployee.getID() != id && (department.equalsIgnoreCase(firedEmployee.getDepartment()) == false))
                {
                    previousEmployee = firedEmployee;
                }
            }
            
            Employee nextEmployee = firedEmployee.getNextEmployee();
            
            previousEmployee.setNextEmployee(nextEmployee);
            
            firedEmployee = null;
        }
        
        public boolean isEmpty()
        {
            return first == null;
        }
        
        public void printListOfEmployee()
        {
            Employee current = first;
            
            while( current != null)
            {
                System.out.printf("%s\n" , current.toString());
                current = current.getNextEmployee();
            }
        }
        
        public void printEmployeeList()
        {
            Employee current = first;
            
            while(current != null)
            {
                System.out.printf("%d\n" , i);
                System.out.printf("Name: %s , ID: %d , Age: %d , Department : %s" , current.getName() , current.getID() , current.getAge() , current.getDepartment());
                current = current.getNextEmployee();
            }
        }
        
        public void salary(String name , int idNumber , int amount)
        {
            Employee current = findEmployee(name , idNumber);
            if(current != null)
            {
                current.updateSalary(amount);
            }
            else
            {
                System.out.println("No such person");
            }
        }
        
        public void salary(String name , int idNumber  , double rate)
        {
            Employee current = findEmployee(name , idNumber);
            if(current != null)
            {
                current.updateSalary(rate);
            }
            else
            {
                System.out.println("No such person");
            }
        }
        
        public Employee findEmployee(String name , int idNumber )
        {
            if(isEmpty())
            {
                return null;
            }
            
            Employee current = first;
            
            while((current.getName() != "name") && (current.getID() != idNumber ) && (current != null))
            {
                current.getNextEmployee();
            }
            
            if(current == null)
            {
                return null;
            }
            else
            {
                return current;
            }
        }
    }
    Code:
    public class TestEmployee
    {
        public static void main(String args[])
        {
            ListOfEmployee employees = new ListOfEmployee();
            
            employees.addEmployee("wen hao" , 12323 , 12 , 2323.00 , "dfad" , "safsdf");
            employees.addEmployee("wen hao2" , 1233131 , 12 , 2323.00 ," adsfa " , "dfdfd");
            employees.addEmployee("wen hao3" , 1233 , 12 , 2323.00 , "dfsdf" , "fdfd");
            
            employees.printEmployeeList();
            System.out.println();
        }
    }
    i can't print all the three employees ...~ why ?

  2. CODECALL Circuit advertisement

     
  3. #2
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Help for dynamic memory allocation.

    Code:
    public void addEmployee(String name , int idNumber , int age , double salary , String title , String department)
        {
            if(isEmpty())
            {
                first = last = new Employee(name , idNumber , age , salary , title , department);
                return;
            }
            else
            {
                Employee temp = new Employee(name , idNumber , age , salary , title , department);
                last.setNextEmployee(temp);
                last = temp;
            }
        }

  4. #3
    R3.RyozKidz Guest

    Re: Help for dynamic memory allocation.

    bobdark , could you explain it ..?

    i try another way which shown in my java book
    Code:
    class Employee
    {
        String name;
        int idNumber;
        int age;
        double salary;
        String title;
        String department;
        Employee nextEmployee;
        
        public Employee(String name , int idNumber , int age , double salary , String title , String department)
        {
            this(name , idNumber , age , salary , title , department , null);
        }
        
        public Employee(String name , int idNumber , int age , double salary , String title , String department , Employee nextEmployee)
        {
            this.name = name;
            this.idNumber = idNumber;
            this.age = age;
            this.salary = salary;
            this.title = title;
            this.department = department;
            this.nextEmployee = nextEmployee;
        }
        
    }
    public class EmployeeList
    {
        private Employee first;
        private Employee last;
        
        public EmployeeList()
        {
            first = last = null;
        }
        
        public void addEmployee(String name , int idNumber , int age , double salary , String title , String department)
        {
            if(isEmpty())
            {
                first = last = new Employee(name , idNumber , age , salary , title , department);
            }
            else
            {
                last = last.nextEmployee = new Employee(name , idNumber , age , salary , title ,department);
            }
        }
        
        public void print()
        {
            Employee current = first;
            
            while(current != null)
            {
                System.out.printf("%s\n" , current.name);
                current = current.nextEmployee;
            }
        }
        
        public boolean isEmpty()
        {
            return first == null;
        }
    }
    this code works too ..
    but i'm not really know the details ..~
    why is a code that can contains two class ..? as i know , after i have compiled it , it will have 2 different .class file .
    why is the code fields are without access modifier (private , public ,protected) ? is java compiler will assume this fields are public ?

  5. #4
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Help for dynamic memory allocation.

    Quote Originally Posted by R3.RyozKidz
    why is a code that can contains two class ..? as i know , after i have compiled it , it will have 2 different .class file .
    Jav files may have as many classes as you want to place in them, however they may only have a single public class that is named the same as the file it is in.

    Quote Originally Posted by R3.RyozKidz
    why is the code fields are without access modifier (private , public ,protected) ? is java compiler will assume this fields are public ?
    When you omit the access specifier, that means it is a special type of access called "friendly" access. This means that all objects within the same package as the class have access to that class, however no other classes have access to it.
    Wow I changed my sig!

  6. #5
    R3.RyozKidz Guest

    Re: Help for dynamic memory allocation.

    Then how many types of accesses does java has ??
    what are the differences between this three codes?
    Code:
    last = last.nextEmployee = new Employee(name , idNumber , age , salary , title ,department);
    Code:
    Employee temp = new Employee(name , idNumber , age , salary , title , department);
                last.setNextEmployee(temp);
                last = temp;
    Code:
    last = last.getNextEmployee();
                last = new Employee(name , idNumber , age , salary , title , department);

  7. #6
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Help for dynamic memory allocation.

    Ok regarding what happens there...
    1)
    a)new employee is created, lets call him x.
    b)the 'next' field of the object pointed to by 'last' is set to point to x (everywhere I say points to, I mean holds reference to..., I'm saying 'points' for the sake of simplicity)
    c)'last' is set to point to the newly created x.
    There are two assignment operations, the right one first, if I'm not wrong it returns the value that the right operand was assigned, like in C. Next, 'last' is assigned the returned value.
    Why this code works? Exactly because java uses reference semantics (fix me if I'm wrong, I think this is the way it is called) - the trick is only at point when there is one single object in the list. Because 'first' and 'last' hold reference to the same object,
    Code:
    last.next = new employee
    updates the object pointed to by 'first' - that, by the way, what your code did not do, the 'next' field of the object pointed to by 'first' at your code was not updated when you added the second employee, but stayed null, this is why you had only the first employee printed. When your list has 2 or more elements, your code too should work fine.

    2)explicitly insert the new employee as last node -
    a) create new employee and set 'temp' to point to it.
    b) set the last node's 'next' field to point to the new employee.
    c) set the new employee as the last one

    3)
    a)overwrite 'last' with it's 'next' value (pointless anyway because in the next command you overwrite it again)
    b)set 'last' to point to the newly created employee

    this code didn't work because you didn't update here the 'next' field of 'last' to point to the new object. That is why after adding the second employee, the 'next' field of the first was still null and as I wrote above, you had only 1 employee printed.

    General advice - usually try to avoid multiple assignments in one line.

  8. #7
    R3.RyozKidz Guest

    Re: Help for dynamic memory allocation.

    ok ..~ isn't this statement is updating to the next employee ? last = last.getNextEmployee() ?

  9. #8
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Help for dynamic memory allocation.

    it updates the variable 'last' with a new value, but it doesn't update the object pointed to by 'last', which is the problem here.

  10. #9
    R3.RyozKidz Guest

    Re: Help for dynamic memory allocation.

    new value ..? after the last = last.getNextEmployee() statement , i continued it with last = new Employee( ... ) ? isn't it upgraded to the next employee then only being assigned for a new employee?

  11. #10
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Help for dynamic memory allocation.

    yes, 'last' - THE VARIABLE 'last', NOT THE OBJECT THAT 'last' holds reference to, is set first to hold the 'next' value and then the new object.
    The problem is that the OBJECT REFERENCED by 'last' is not updated, more precisely, it's 'next' field.

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 10
    Last Post: 10-17-2011, 05:32 PM
  2. why memory allocation is necessary?
    By swAn in forum C and C++
    Replies: 8
    Last Post: 04-07-2010, 10:22 AM
  3. Problem with dynamic memory allocation in C
    By Sinipull in forum C and C++
    Replies: 3
    Last Post: 11-12-2009, 03:42 AM
  4. [C++] dynamic allocation
    By armon in forum C and C++
    Replies: 4
    Last Post: 08-28-2009, 06:10 AM
  5. Assertion Error (Dynamic Memory Allocation)
    By LukeyJ in forum C and C++
    Replies: 3
    Last Post: 11-04-2008, 10:27 AM

Tags for this Thread

Bookmarks

Posting Permissions

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