Question

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

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)

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

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

There are various possibilities of a non compliant argument:-

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

  2. 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.

  3. 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.

Add a comment
Know the answer?
Add Answer to:
8 What techniques can a method do when it receives a non-compliant argument? 9 What is...
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
  • I NEED HELP with this. please create a UML diagram. I need a simple code to...

    I NEED HELP with this. please create a UML diagram. I need a simple code to solve the problem.   The ADT Bag is a group of items, much like what you might have with a bag of groceries. In a software development cycle, specification, design, implementation, test/debug, and documentation are typical activities. The details are provided in the rest of the document. ADT Bag Specification: (Note: You should not change the names of the operations in your program. This should...

  • I tried to complete a Java application that must include at a minimum: Three classes minimum...

    I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...

  • 10.3 Example. When you first declare a new list, it is empty and its length is...

    10.3 Example. When you first declare a new list, it is empty and its length is zero. If you add three objects—a, b, and c—one at a time and in the order given, to the end of the list, the list will appear as a b c The object a is first, at position 1, b is at position 2, and c is last at position 3.1 To save space here, we will sometimes write a list’s contents on one...

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

  • QUESTION 13 A constructor is the same as any other method definition EXCEPT for what? A...

    QUESTION 13 A constructor is the same as any other method definition EXCEPT for what? A constructor includes an optional argument list. A constructor includes an optional access modifier, usually public. A constructor includes a return type. A constructor places arguments within parentheses (). 2 points    QUESTION 14 Using the asterisk wildcard (e.g. import com.packageName.*) will include all of the classes within packageName, even if they aren’t used. True False 2 points    QUESTION 15 The Random class gives...

  • code in java please:) Part I Various public transporation can be described as follows: A PublicTransportation...

    code in java please:) Part I Various public transporation can be described as follows: A PublicTransportation class with the following: ticket price (double type) and mumber of stops (int type). A CityBus is a PublicTransportation that in addition has the following: an route number (long type), an begin operation year (int type), a line name (String type), and driver(smame (String type) -A Tram is a CityBus that in addition has the following: maximum speed (int type), which indicates the maximum...

  • ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag...

    ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag collection using a linked list and, in order to focus on the linked list implementation details, we will implement the collection to store only one type of object of your choice (i.e., not generic). You can use the object you created for program #2 IMPORTANT: You may not use the LinkedList class from the java library. Instead, you must implement your own linked list...

  • /*hello everyone. my question is mostly related to the garagetest part of this assignment that i've...

    /*hello everyone. my question is mostly related to the garagetest part of this assignment that i've been working on. i am not sure how to read the file's contents AND put them into variables/an array. should it be in an arraylist? or maybe a regular array? or no arrays at all? i am also not sure how to call the factory method from garagec. i'm thinking something along the lines of [Garage garage = new GarageC.getInstance("GarageC", numFloors, areaofEachFloor);] but i...

  • SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what...

    SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what choices does one make when creating a class that is an example of Abstraction? Encapsulation: What is encapsulation? What is information hiding? What does a public interface to a class consist of (not in the sense of actual java interfaces but what the client uses to manipulate objects of the class) What is an object of a class? What is the constructor? How do...

  • This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing...

    This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing data structures. We will be using Entry.java objects which are key,value pairs, with key being an integer and value being a generic parameter. We will be sorting these Entries according to their key value. The file Entry.java is given to you and does not require any modification. As you should know by now, bucket sort works by placing items into buckets and each bucket...

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