Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class MyNewSet { public MyNewSet(){ super(); } public static void main(String[] args) { } } In Java: 1. Add implements Set to

public class MyNewSet { public MyNewSet(){ super(); } public static void main(String[] args) { } }

In Java:

1. Add "implements Set" to the class declaration. You will see that eclipse doesn't like this and has a red line under your class name. Move your cursor over the class name and click on "Add unimplemented methods". Your file will now contain all the method stubs required to implement the Set interface. Scroll through the list to show how many there are. Now click on edit -> undo to remove all the methods (notice the red line is back). Now add "extends ArrayList" before implements in the class declaration. Notice that the red line went away, why (write your answer as a comment in the java file)? Your class header should be: public class MyNewSet extends ArrayList implements Set

2. We want this class to use an ArrayList to implement a Set (as noted above). To do that we will need to override the add method in the ArrayList class to match the Set definition. Write the add method so that we can't add duplicates. Don't forget to use the key word super if you need a super classes method.

3. In the main method create an instance of MyNewSet and then include the following code:

String h = "Hello"; String n = "Hey"; String i = "Hello";

Now try to add all 3 Strings to the MyNewSet object and verify that your add method is working the way that you would expect by printing the output to console (leave this part in your code).

4. Next create a new ArrayList object and add the following String to it: "Goodbye" and "Hey". Now take your MyNewSet object and call the addAll() method to add in your ArrayList object. Print out your MyNewSet object. Did the addAll do as you expected? Is this working like a Set should? (Answer in a comment)

5. Finally, override the addAll method to work as you would expect (hint you should use your add method, google the api to make sure you know what to return and when!). Run your code from 4 above to verify that you got what you expected. (If you don't get this step completed make sure to comment on what you were working on and going to do next).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions

Question

4. How has e-commerce affected business-to-business transactions?

Answered: 1 week ago