Question

Create a method in Java that adds a task to a list and returns false if...

Create a method in Java that adds a task to a list and returns false if the task is already part of the list. Create another method to remove tasks from the list, and returns false if the task is not part of the list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;
import java.util.List;

public class Tasks {

    public static boolean addTask(List<String> list, String task) {
        if(list.contains(task)) {
            return false;
        } else {
            list.add(task);
            return true;
        }
    }

    public static boolean removeTask(List<String> list, String task) {
        if(list.contains(task)) {
            list.remove(task);
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        List<String> tasks = new ArrayList<>();
        addTask(tasks, "task1");
        addTask(tasks, "task2");
        addTask(tasks, "task3");
        addTask(tasks, "task1");
        System.out.println(tasks);
        removeTask(tasks, "task1");
        System.out.println(tasks);
    }

}

[taskl, task2, task3] [task2, task3] Process finished with e xit code 0

Add a comment
Know the answer?
Add Answer to:
Create a method in Java that adds a task to a list and returns false if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java: Create an addBefore and add method to the class IntArrayList. The method addBefore adds...

    In Java: Create an addBefore and add method to the class IntArrayList. The method addBefore adds an element to the beginning of an array, at index 0, causing all existing elements to move forward (their indexes increased by 1). The method add creates an element at the end of the array. There are several ways to accomplish this task. Perhaps the easiest: double the array when you hit the beginning or end, copying it to the beginning or end dependent...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • Java programming....Queues Create a method called "hasNodes". This returns true or false depending on if any...

    Java programming....Queues Create a method called "hasNodes". This returns true or false depending on if any nodes exist in the queue public class AQueue{ private AQueue head = null; private AQueue tail = null; public boolean hasNodes(){ ......}

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • PYTHON LANGUAGE Task 11 -Create a list with 5 items having 3 distinct data types -output...

    PYTHON LANGUAGE Task 11 -Create a list with 5 items having 3 distinct data types -output the second element of the list -output the last element of the list using -negative index -positive index -len method Task 12 -Create a list with 3 items -add an item to the end of the list -remove the first item of the list -determine if an item exists in the list -if it does, display its index -if it does not, give user...

  • In Java, create a list that represents a sentence. Then: Write a method that counts and...

    In Java, create a list that represents a sentence. Then: Write a method that counts and returns the number of punctuation in a given sentence. Express this as a higher-order function and implement it. Write a method that counts and returns the number of words in the sentence that have the letter ’z’ in them. Express this as a higher-order function and implement it.

  • Write a method called removeLast that removes and returns the last value from a list of...

    Write a method called removeLast that removes and returns the last value from a list of integers. For example, if a variable called list stores [8, 17, 42, 3, 8], a call of list.removeLast(); should return 8 and change the list’s state to [8, 17, 42, 3]. The next call would return 3 and remove 3 from the list, and so on. If the list is empty, throw a NoSuchElementException. The program needs to be in Java. Please also create...

  • In this problem, you will create a selectable “To Do” List. To add a task to...

    In this problem, you will create a selectable “To Do” List. To add a task to this list, the user clicks the Add Task button and enters a description of the task. To delete a task from the list, the user selects the task and then clicks the Delete Task button. Open the HTML and JavaScript files provided as start-up files (index.html from within todo_list_Q.zip file). Then, review the HTML in this file. Note, within the div element, there is...

  • FOR JAVA: Summary: Create a program that adds students to the class list (list below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (list below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • In Java: Let's create a method that has parameters. Write a method, called returnHours that returns...

    In Java: Let's create a method that has parameters. Write a method, called returnHours that returns a string. Make the string from a string representing your name and a number representing the hours you spend sleeping, both are values that you pass in from the main. Create the variables to pass into the method call in main. Call the method and print the return value in main. Run and test it.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT