Results 1 to 12 of 12
Thread: ArrayList
- 11-30-2007, 08:14 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
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 08:33 PM.
- 12-01-2007, 12:00 AM #2
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) + "]"; } }
- 12-01-2007, 07:46 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
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.
- 12-01-2007, 09:46 PM #4
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?Java Code:public void setDateRequired(int day, int month, int year) { Date = new Date(day, month, year); }
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:
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.Java Code:// Deprecated Date constructor: Date(int year, int month, int date) // You have: new Date(day, month, year)
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.
- 12-01-2007, 10:24 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
- 12-04-2007, 01:12 PM #6
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
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
- 12-04-2007, 04:38 PM #7
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.
- 12-04-2007, 07:06 PM #8
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
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
- 12-05-2007, 12:01 AM #9
[i]"cannot find symbol - variable getCost[i/]
Sounds like you were using getCost like you would a variable/field
Maybe you left off the "()" operator. TryJava Code:aJob.getCost
instead.Java Code:aJob.getCost();
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.
- 12-05-2007, 09:56 AM #10
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
- 12-05-2007, 07:04 PM #11
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-05-2007, 07:30 PM #12
Member
- Join Date
- Nov 2007
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
ArrayList
By ramitmehra123 in forum New To JavaReplies: 1Last Post: 02-07-2008, 12:47 AM -
ArrayList
By kizilbas1 in forum New To JavaReplies: 1Last Post: 01-12-2008, 08:48 PM -
New to arraylist
By kleave in forum New To JavaReplies: 2Last Post: 11-19-2007, 06:45 PM -
Help ArrayList.add()
By eNine in forum New To JavaReplies: 2Last Post: 08-06-2007, 01:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks