简体   繁体   中英

Create tasks[] an array of task

My current problem is that I am assigned to created a program that should within the private fields assign tasks[] an array of task. Then within the constructor, that creates the task[] array, giving it the capacity of INITIAL_CAPAITY, and setting numTasks to zero.

I am new and confused on I can tackle this problem

I have tried declaring it within the constructor but there has been no luck.

Task.java

public class Task {
    private String name;
    private int priority;
    private int estMinsToComplete;

    public Task(String name, int priority, int estMinsToComplete) {
        this.name=name;
        this.priority=priority;
        this.estMinsToComplete = estMinsToComplete;
    }

    public String getName() {
        return name;
    }

    public int getPriority() {
        return priority;
    }

    public int getEstMinsToComplete() {
        return estMinsToComplete;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEstMinsToComplete(int newestMinsToComplete) {
        this.estMinsToComplete = newestMinsToComplete;
    }

    public String toString() {
        return name+","+priority+","+estMinsToComplete;
    }


    public void increasePriority(int amount) {
        if(amount>0) {
            this.priority+=amount;
        }
    }

    public void decreasePriority(int amount) {
        if (amount>priority) {
            this.priority=0;
        }
            else {
                this.priority-=amount;
            }
        }
    }

HoneyDoList.java

public class HoneyDoList extends Task{
    private String[] tasks;
//this issue to my knowledge is the line of code above this

    private int numTasks;
    private int INITIAL_CAPACITY = 5;

    public HoneyDoList(String tasks, int numTasks, int INITIAL_CAPACITY,int estMinsToComplete, String name,int priority) {
        super(name,priority,estMinsToComplete);
        numTasks = 0;
        tasks = new String[]{name,priority,estMinsToComplete};
//as well as here^^^^^^^^
    }

My expected result is to be able to print out the list through honeydo class. I need to manipulate the code a bit more after adding a few other methods.

Your problem is that your constructor parameter tasks has the same name as that field of your class.

So you assign to the method parameter in your constructor, not to the field. And luckily those two different "tasks" entities have different types, otherwise you would not even notice that something is wrong.

Solution: use

this.tasks = new String... 

within the body of the constructor!

And the real answer: you have to pay a lot attention to such subtle details. And by using different names for different things you avoid a whole class of issues!

Also note: it sounds a bit strange that a class named Task contains a list of tasks, which are then strings. The overall design is a bit weird...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM