Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-30-2007, 10:14 PM
Member
 
Join Date: Nov 2007
Posts: 15
kizilbas1 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-01-2007, 02:00 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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) + "]"; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-01-2007, 09:46 PM
Member
 
Join Date: Nov 2007
Posts: 15
kizilbas1 is on a distinguished road
Quote:
Originally Posted by hardwired View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-01-2007, 11:46 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-02-2007, 12:24 AM
Member
 
Join Date: Nov 2007
Posts: 15
kizilbas1 is on a distinguished road
Quote:
Originally Posted by hardwired View Post
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:
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-04-2007, 03:12 PM
Member
 
Join Date: Nov 2007
Posts: 15
kizilbas1 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-04-2007, 06:38 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-04-2007, 09:06 PM
Member
 
Join Date: Nov 2007
Posts: 15
kizilbas1 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-05-2007, 02:01 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
[i]"cannot find symbol - variable getCost[i/]
Sounds like you were using getCost like you would a variable/field
Code:
aJob.getCost
Maybe you left off the "()" operator. Try
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-05-2007, 11:56 AM
Member
 
Join Date: Nov 2007
Posts: 15
kizilbas1 is on a distinguished road
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
Code:
aJob.getCost
Maybe you left off the "()" operator. Try
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-05-2007, 09:04 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 12-05-2007, 09:30 PM
Member
 
Join Date: Nov 2007
Posts: 15
kizilbas1 is on a distinguished road
Thanks for your all replies, i could finally manage it.

Much appreciated
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 01:43 PM
ArrayList ramitmehra123 New To Java 1 02-07-2008 02:47 AM
ArrayList kizilbas1 New To Java 1 01-12-2008 10:48 PM
New to arraylist kleave New To Java 2 11-19-2007 08:45 PM
Help ArrayList.add() eNine New To Java 2 08-06-2007 03:13 PM


All times are GMT +3. The time now is 03:21 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org