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

There is a class in the Java API called Vector. The objects in this class are si

ID: 3821953 • Letter: T

Question

There is a class in the Java API called Vector. The objects in this class are similar to an ArrayList, except for a detail that is irrelevant to this discussion. Suppose that methods with the following signatures were in the class. Should they be class methods or instance methods?

add(int item)

Add item to this Vector.

add(int item, Vector v)

Add item to Vector v.

c) sort(Vector v)

Place the elements in Vector v in ascending order.

d) sort()

Place the elements of this Vector in ascending order.

e) binarySearch(int target)

Search the elements of this Vector for the target value

Explanation / Answer

a)

add(int item) should be a class method. We are just adding a item to a variable, we donot require any instance of class.It is utility method and we donnot want it to change

b)add(int item, Vector v) should be a Class method.Similar reason as above.

c & d ) should be instance methods as name of methods is same but in one we have to sort in asc and in other desc. so they cann't be in one utility method

e)binarySearch() this can be class method too because it is acting as a utility method and doesnot depends on any instance creation

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