Question

Design MyWeek class: (1) Data members: the class contains one private data field weekList. weekList has datatype of Array

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, here is the complete class in Java

==============================================================
import java.util.ArrayList;

public class MyWeek {

    private ArrayList<String> weekList;

    public MyWeek() {
        weekList = new ArrayList<>();

    }

    public MyWeek(String weekName) {
        if (weekName == null) {
            throw new IllegalArgumentException();
        }
        weekList = new ArrayList<>();
        weekList.add(weekName);
    }

    public MyWeek(ArrayList<String> weeks) {
        if (weeks == null) {
            throw new IllegalArgumentException();
        }
        weekList = new ArrayList<>();
        for (String name : weeks) {
            if (weekList.contains(name)) {
                continue;
            } else {
                weekList.add(name);
            }
        }
    }

    public void insert(String week) {
        weekList.add(week);
    }

    public void insert(ArrayList<String> weeks) {
        if (weeks == null) {
            throw new IllegalArgumentException();
        }
        for (String week : weeks) {
            if (weekList.contains(week)) {
                continue;
            } else {
                weekList.add(week);
            }
        }
    }

    public boolean search(String week) {
        if (week == null) {
            throw new IllegalArgumentException();
        }
        for (String weekName : weekList) {
            if (weekName.equalsIgnoreCase(week)) {
                return true;
            }
        }
        return false;
    }

    public void delete(String name) {
        if (name == null) {
            throw new IllegalArgumentException();
        }
        if (weekList.contains(name)) {
            weekList.remove(name);


        }
    }

    public void delete(ArrayList<String> names) {
        if (names == null) {
            throw new IllegalArgumentException();
        }
        for (String name : names) {
            if (weekList.contains(name)) {
                weekList.remove(name);
            }
        }
    }

    @Override
    public String toString() {
        return weekList.toString();
    }

    @Override
    public boolean equals(Object obj) {

        if (obj != null && obj instanceof MyWeek) {

            MyWeek anotherWeek = (MyWeek) obj;
            if (anotherWeek.weekList.size() != weekList.size()) {
                return false;
            }
            return weekList.containsAll(anotherWeek.weekList);

        }
        return false;
    }

}
Add a comment
Know the answer?
Add Answer to:
Design "MyWeek" class: (1) Data members: the class contains one private data field weekList. "wee...
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
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
ADVERTISEMENT