Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8 What techniques can a method do when it receives a non-compliant argument? 9 W

ID: 3601787 • Letter: 8

Question

8 What techniques can a method do when it receives a non-compliant argument?

9 What is a collection class?

10 What capabilities or operations should/could all collection classes (in general) have?

11 You are given a Collection class called X (a “bag” class). You are not given the .java file, only the class file, as well as some documentation on the interface methods (which includes pre- and post-condition requirements, data types, and method names). You cannot read the class implementation. Is it possible to write a program that uses the X data type? Why or why not (explain fully)

Explanation / Answer

8) What techniques can a method do when it receives a non-compliant argument?

There are various possibilities of a non compliant argument:-

An argument can be null, in this case we should throw a new NullPointerException.

Various objects can be checked for empty status for ex. String, Collection, Array classes. In this case IllegalArgumentException can be thrown or any other related exception.

Another check is of instance type. If argument is of not of required type, IllegalArgumentException can be thrown.

9) What is a collection class?

A Collection is a group of individual objects represented as a single unit. A unit in which multiple objects can be store in a particular data structure. Generally objects should be of same type. All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Collection class.

A Collection can follow any data structure like Array, Linked List, Tree Data Structure depends upon the requirements. Other factors that needs attention are ordering, sorting and uniqueness. Some collection can store any element or duplicate element or other can store only unique elements.

A collection class includes multiple operations like add, remove, contains, size, isEmpty, iterator.

10 What capabilities or operations should/could all collection classes (in general) have?

Following are the operations:-

Boolean add(E e) -

Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.

If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.

Void clear() - Removes all of the elements from this collection.

Boolean contains(Object o) - Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).

Boolean isEmpty() - Returns true if this collection contains no elements.

Iterator<E> iterator() - Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).

remove(Object o) - Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).

int size() - Returns the number of elements in this collection.


11 You are given a Collection class called X (a “bag” class). You are not given the .java file, only the class file, as well as some documentation on the interface methods (which includes pre- and post-condition requirements, data types, and method names). You cannot read the class implementation. Is it possible to write a program that uses the X data type? Why or why not (explain fully)

Every java class is converted into the .class file or we can say bytecode to run by JVM. That means for every source code there should be byte code for that class to run properly by JVM. This byte code or .class file is generated by compiler after compiling .java file. We can add the .class file in our project class path and can use everywhere in our code.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote