Jump to content

Help for dynamic memory allocation.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
Here are my codes
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;
    }
}

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;
        }
    }
}

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
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
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;
        }
    }


#3
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
bobdark , could you explain it ..?

i try another way which shown in my java book
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 ?

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

R3.RyozKidz said:

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.

R3.RyozKidz said:

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!

#5
Guest_R3.RyozKidz_*

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


#6
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
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,
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.

#7
Guest_R3.RyozKidz_*

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

#8
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
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.

#9
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
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?

#10
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
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.

#11
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
i thought i updated the last variable and assigned it with a new object will actually updated the object referenced by 'last'.
still need a little bit more of explanation ..~

#12
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
You had to do two different assignments:
1) last.next = new employee x
2) last = x
the first assignment would have had updated the referenced object.