Results 1 to 12 of 12

Thread: ArrayList

  1. #1
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Post ArrayList

    I have been trying to work on ArrayList, however the class does not collect the stored information in another class. so can someone please help me on that. here is the code below;

    public class DailyLog
    {
    private Date logDate;
    private job job;
    private int totalCost;
    private ArrayList<String>waitingList;
    private ArrayList<String>completedList;

    public DailyLog(int day, int month, int year, int totalcost)
    {
    day = 32;
    month = 13;
    year = 2000;
    totalcost = 0;

    waitingList = new ArrayList<String>();
    completedList = new ArrayList<String>();
    }

    public void addjob(String job, int d, int m, int y)
    {
    waitingList.add(new String(Job));



    and here is what i have been asked to do;

    Provide the mutator addJob, which has a formal parameter of type Job and three ints to
    represent the date the job is required. The method sets the job’s required date from formal parameters, and adds the job to the end of the collection of jobs waiting to be copied.
    Last edited by kizilbas1; 11-30-2007 at 09:33 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.text.*;
    import java.util.*;
    
    public class DailyLogRx
    {
        private int totalCost = 0;
        private ArrayList<Job> waitingList; 
        private ArrayList<Job> completedList;
    
        public DailyLogRx()
        {
            waitingList = new ArrayList<Job>();
            completedList = new ArrayList<Job>();
        }
    
        public void addJob(Job job, int d, int m, int y)
        {
            job.setDate(d, m, y);
            waitingList.add(job);
        }
    
        public Job[] getJobs()
        {
            int size = waitingList.size();
            return waitingList.toArray(new Job[size]);
        }
    
        public static void main(String[] argsd)
        {
            DailyLogRx test = new DailyLogRx();
            test.addJob(new Job("East Side"), 6, 9, 2008);
            test.addJob(new Job("Uptown"),   14, 6, 2010);
            test.addJob(new Job("Harbor"),   13, 5, 2009);
            Job[] jobs = test.getJobs();
            for(int j = 0; j < jobs.length; j++) {
                System.out.println(jobs[j]);
            }
        }
    }
    
    class Job
    {
        DateFormat df = new SimpleDateFormat("dd MMM yyyy");
        Date deadLine;
        String name;
    
        public Job(String name)
        {
            this.name = name;
        }
    
        public void setDate(int day, int month, int year)
        {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month, day);
            deadLine = calendar.getTime();
        }
    
        public String toString()
        {
            return "Job[name: " + name +
                     ", deadLine: " + df.format(deadLine) + "]";
        }
    }

  3. #3
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    Java Code:
    import java.text.*;
    import java.util.*;
    
    public class DailyLogRx
    {
        private int totalCost = 0;
        private ArrayList<Job> waitingList; 
        private ArrayList<Job> completedList;
    
        public DailyLogRx()
        {
            waitingList = new ArrayList<Job>();
            completedList = new ArrayList<Job>();
        }
    
        public void addJob(Job job, int d, int m, int y)
        {
            job.setDate(d, m, y);
            waitingList.add(job);
        }
    
        public Job[] getJobs()
        {
            int size = waitingList.size();
            return waitingList.toArray(new Job[size]);
        }
    
        public static void main(String[] argsd)
        {
            DailyLogRx test = new DailyLogRx();
            test.addJob(new Job("East Side"), 6, 9, 2008);
            test.addJob(new Job("Uptown"),   14, 6, 2010);
            test.addJob(new Job("Harbor"),   13, 5, 2009);
            Job[] jobs = test.getJobs();
            for(int j = 0; j < jobs.length; j++) {
                System.out.println(jobs[j]);
            }
        }
    }
    
    class Job
    {
        DateFormat df = new SimpleDateFormat("dd MMM yyyy");
        Date deadLine;
        String name;
    
        public Job(String name)
        {
            this.name = name;
        }
    
        public void setDate(int day, int month, int year)
        {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month, day);
            deadLine = calendar.getTime();
        }
    
        public String toString()
        {
            return "Job[name: " + name +
                     ", deadLine: " + df.format(deadLine) + "]";
        }
    }
    in order to set the date, i have written the following code;

    public void setDateRequired(int day, int month, int year)
    {
    Date = new Date(day, month, year);
    }



    and i think this method must be right in order to link DailyLog class. As a result, do you think, i have written the right code?


    below here what i have been asked to write;

    Write a mutator setDateRequired to set the date for which the job is required from suitable formal parameters of type int. The values of the formal parameters should be used to set the value of an appropriate field.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public void setDateRequired(int day, int month, int year)
    { 
        Date = new Date(day, month, year);
    }
    and i think this method must be right in order to link DailyLog class. As a result, do you think, i have written the right code?
    No. This constructor in the Date class has been deprecated and it takes a different order in its arguemnts than what you have used above:
    Java Code:
    // Deprecated Date constructor:
    Date(int year, int month, int date)
    // You have:
    new Date(day, month, year)
    If you look up the constructor in the Date class api it tells you that it has been deprecated and tells what to use in its place: Calendar.set(...) which is what I used in DailyLogRx above.

    below here what i have been asked to write;

    Write a mutator setDateRequired to set the date for which the job is required from suitable formal parameters of type int. The values of the formal parameters should be used to set the value of an appropriate field

    I saw this paragraph in your original post and used the information to make DailyLogRx.

  5. #5
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    Java Code:
    public void setDateRequired(int day, int month, int year)
    { 
        Date = new Date(day, month, year);
    }
    and i think this method must be right in order to link DailyLog class. As a result, do you think, i have written the right code?
    No. This constructor in the Date class has been deprecated and it takes a different order in its arguemnts than what you have used above:
    Java Code:
    // Deprecated Date constructor:
    Date(int year, int month, int date)
    // You have:
    new Date(day, month, year)
    If you look up the constructor in the Date class api it tells you that it has been deprecated and tells what to use in its place: Calendar.set(...) which is what I used in DailyLogRx above.

    below here what i have been asked to write;

    Write a mutator setDateRequired to set the date for which the job is required from suitable formal parameters of type int. The values of the formal parameters should be used to set the value of an appropriate field

    I saw this paragraph in your original post and used the information to make DailyLogRx.

    I get it now, thanks for your help mate :)

  6. #6
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Hi,

    As you know I have used the following code to add jobs to the waiting list.

    public void addJob(Job Job, int day, int month, int year)
    {
    // put your code here

    waitingList.add(Job);

    }

    and now i need to return the total costs of the completed jobs. I have tried few things but it hasn't worked. Do i need to add another method for the course in order to display the total cost.

    What would you recommend?


    Regards

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Add a method in the Job class to return the cost of the Job. Add a method in the DailyLog class that will return the total cost of completed jobs. You would go through the completedList to add up the cost of each Job using the new method in the Job class.

  8. #8
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by kizilbas1 View Post
    Hi,

    As you know I have used the following code to add jobs to the waiting list.

    public void addJob(Job Job, int day, int month, int year)
    {
    // put your code here

    waitingList.add(Job);

    }

    and now i need to return the total costs of the completed jobs. I have tried few things but it hasn't worked. Do i need to add another method for the course in order to display the total cost.

    What would you recommend?


    Regards
    Quote Originally Posted by hardwired View Post
    Add a method in the Job class to return the cost of the Job. Add a method in the DailyLog class that will return the total cost of completed jobs. You would go through the completedList to add up the cost of each Job using the new method in the Job class.

    here is what i use to calculate the cost in Job Class which is in constructor;

    public class Job
    {

    private int reference;
    private String name;
    private int pagno;
    private int copno;
    private Time setTime;
    private Date Date;
    private boolean doubleSided;
    private int cost;

    /**
    * Constructor for objects of class Job
    */
    public Job(int refere, String nam, int pgno, int cpno, int hour, int minute, boolean doubleSid)
    {

    reference = refere;
    name = nam;
    pagno = pgno;
    copno = cpno;
    doubleSided = doubleSid;


    setTime = new Time(hour, minute);
    Date = null;

    if (doubleSid == true)
    {

    cost = (pgno * cpno) * 2;

    }
    else
    {

    cost = (pgno * cpno) * 3;

    }
    }


    and i also use getCost method in job class, however when i call this method it says "cannot find symbol - variable getCost or when i call the job in constructor then this time says "cost has private access in job". so that's the problem that i am encountering now.

    Thanks

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    [i]"cannot find symbol - variable getCost[i/]
    Sounds like you were using getCost like you would a variable/field
    Java Code:
    aJob.getCost
    Maybe you left off the "()" operator. Try
    Java Code:
    aJob.getCost();
    instead.
    when i call the job in constructor then this time says "cost has private access in job".
    Change the access modifier of the getCost method from private to public so that it can be accessed from outside the class.

  10. #10
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    [i]"cannot find symbol - variable getCost[i/]
    Sounds like you were using getCost like you would a variable/field
    Java Code:
    aJob.getCost
    Maybe you left off the "()" operator. Try
    Java Code:
    aJob.getCost();
    instead.
    when i call the job in constructor then this time says "cost has private access in job".
    Change the access modifier of the getCost method from private to public so that it can be accessed from outside the class.

    I have used the empty bracket and tried every solution but still comes up with the same error and getCost is already public. :(

  11. #11
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.text.*;
    import java.util.*;
    
    public class DailyLogRx
    {
        private int totalCost = 0;
        private ArrayList<Job> waitingList; 
        private ArrayList<Job> completedList;
    
        public DailyLogRx()
        {
            waitingList = new ArrayList<Job>();
            completedList = new ArrayList<Job>();
        }
    
        public void addJob(Job job, int d, int m, int y)
        {
            job.setDate(d, m, y);
            waitingList.add(job);
        }
    
        public Job[] getJobs(List<Job> list)
        {
            int size = list.size();
            return list.toArray(new Job[size]);
        }
    
        private void allComplete()
        {
            completedList.addAll(waitingList);
        }
    
        public static void main(String[] argsd)
        {
            DailyLogRx test = new DailyLogRx();
            test.addJob(new Job("East Side"), 6, 9, 2008);
            test.addJob(new Job("Uptown"),   14, 6, 2010);
            test.addJob(new Job("Harbor"),   13, 5, 2009);
            Job[] jobs = test.getJobs(test.waitingList);
            for(int j = 0; j < jobs.length; j++)
            {
                System.out.println(jobs[j]);
            }
            test.allComplete();
            // total cost of all jobs in completedList:
            jobs = test.getJobs(test.completedList);
            int totalCost = 0;
            for(int j = 0; j < jobs.length; j++)
            {
                totalCost += jobs[j].getCost();
            }
            System.out.println("totalCost = " + totalCost);
        }
    }
    
    class Job
    {
        DateFormat df = new SimpleDateFormat("dd MMM yyyy");
        private int reference;
        private String name;
        private int pagno;
        private int copno;
        private Time setTime;
        private Date date;
        private boolean doubleSided;
        private int cost;
    
        public Job(int refere, String nam, int pgno, int cpno,
                   int hour, int minute, boolean doubleSid)
        {
            reference = refere;
            name = nam;
            pagno = pgno;
            copno = cpno;
            doubleSided = doubleSid;
    
            setTime = new Time(hour, minute);
            date = null;
    
            if (doubleSid)
            {
                cost = (pgno * cpno) * 2;
            }
            else
            { 
                cost = (pgno * cpno) * 3;
            }
        }
    
        public Job(String name)
        {
            this(0,name,0,0,0,0,false);
        }
    
        public int getCost() { return cost; }
    
        public void setDate(int day, int month, int year)
        {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month, day);
            date = calendar.getTime();
        }
    
        public String toString()
        {
            return "Job[name: " + name +
                     ", date: " + df.format(date) + "]";
        }
    }
    
    class Time
    {
        int hour;
        int minute;
    
        Time(int hour, int minute)
        {
            this.hour = hour;
            this.minute = minute;
        }
    }

  12. #12
    kizilbas1 is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default

    Thanks for your all replies, i could finally manage it.

    Much appreciated

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  2. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 01:47 AM
  3. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 09:48 PM
  4. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 07:45 PM
  5. Help ArrayList.add()
    By eNine in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 02:13 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
  •