To build a Set from scratch, first we must create the universe. Luckily, we can build it in Java, which saves a lot of t

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

To build a Set from scratch, first we must create the universe. Luckily, we can build it in Java, which saves a lot of t

Post by answerhappygod »

To build a Set from scratch, first we must create the universe.
Luckily, we can build it in Java, which saves a lot of time. For
this part, we will build a SimpleSet class that implements the
basic operations necessary for a functioning data structure. To
keep things simple, we're only going to build sets that hold
ints.
To this end you need to create a class called SimpleSet with the
following:
- A private array of ints called data that will hold the actual
numbers.
It will also need the following methods:
- A no-parameter constructor that initialises the array, and any
other variables you might have.
- A method called add that takes a single int parameter, and
puts it in data if it's not there, but does nothing if it's already
there.
- A method called remove that takes a single int parameter that
removes that value if it's in data, but does nothing if it's not
there.
- A method call contains that takes a single int and returns
true if that element is in the set, and false otherwise.
- A method called size that returns the number of elements in
the set.
- A method called isEmpty that returns true if there are no
elements in the set, and false otherwise.
- A method called toArray that returns an array containing
exactly the elements of the set.
- A toString method that returns a String representation of the
SimpleSet in the format "{x, y, z, ...}" where x, y, z, ... are
replaced with the actual elements of the set.
When implementing these, you may want to think about the
following questions: How big should data be? What happens when data
is full and we add a new element? What happens to the elements
stored in data when we remove one? Is the size of the set the same
as the length of data? You have seen answers to these questions
throughout the workshops and lectures, so think about where we
might have done similar things before.
Note that there is more than one right answer, the point here is
for you to think about what you're trying to do, and pick a
consistent approach.
Note that there is a main method provided in the Runner class,
but this does not form part of the tests, it's just there so you
can run your code and test it manually.
Finally, but not least, as the point here is for you to
implement your own data structure an learn about how these things
actually work:
You are not allowed to import additional libraries,
including, but not limited to tools from the Java Collections
Framework.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply