Build a class Project with id(int) , desc (String), duration (int) , budget (double) and members (ArrayList of Workers). a. Provide a parameterized constructor Project (int id, String desc, double budget). The member array will be initialized as empty array and the initial duration of the project will be 0 b. Provide setters for description and budget and getters for all except members list c. Provide a method addMember(Worker m) that adds the worker instance to the members array list d. Provide a method Worker getMember(int wId) that searches for the worker with the given id in the members list and returns instance.
SOLUTION CODE
class Project {
int id;
String desc;
int duration;
double budget;
// Create an ArrayList object
ArrayList<String> workers = new ArrayList<String>();
//parameterised constructor
Project(int id, String desc, double budget) {
this.id = id;
this.desc = desc;
this.budget = budget;
}
//setter methods
public void setDuration()
{
//set duration to 0
duration = 0;
}
public void setBudget(double b)
{
budget = b;
}
//getters
public double getBudget() {
return budget;
}
public int getDuration()
{
return duration;
}
//add member function
public void AddMember(String m)
{
workers.add(m);
}
public String getMember(int n)
{
return workers.get(n);
}
}
Comments
Leave a comment