|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

11-30-2007, 10:14 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
|
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 10:33 PM.
|
|

12-01-2007, 02:00 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
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, 09:46 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
Originally Posted by hardwired
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.
|
|

12-01-2007, 11:46 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
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:
// 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.
|
|

12-02-2007, 12:24 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
Originally Posted by hardwired
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:
// 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 
|
|

12-04-2007, 03:12 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
|
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, 06:38 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
|
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, 09:06 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
Originally Posted by kizilbas1
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
Originally Posted by hardwired
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;
i f (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, 02:01 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
[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. Try
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.
|
|

12-05-2007, 11:56 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
Originally Posted by hardwired
[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. Try
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. 
|
|

12-05-2007, 09:04 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
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, 09:30 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 15
|
|
|
Thanks for your all replies, i could finally manage it.
Much appreciated
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|