Concept explainers
Add a method called checklndex to the MusicOrganizer class. It takes a single integer parameter and checks whether it is a valid index for the current state of the collection. To be valid, the parameter must lie in the range 0 to size
If the parameter is not valid, then it should print an error message saying what the valid range is. If the index is valid, then it prints nothing. Test your method on the object bench with both valid and invalid parameters. Does your method still work when you check an index if the collection is empty?
Want to see the full answer?
Check out a sample textbook solutionChapter 4 Solutions
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Additional Engineering Textbook Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out With Visual Basic (7th Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Absolute Java (6th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
- In the existing class UnsortedTableMap, the method put(k,v) is used to add an entry to a map. This method spends time to locate an existing item with the given key. Provide a non-static method named putOnlyIfAbsent(k,v) that adds the entry to the map only if there is no entry with a key k. In case an entry with key k already exists, then just return the existing value corresponding to the key. The new method should be provided in the class UnsortedTableMap. Write the testing code in the main method of the class UnsortedTableMap.arrow_forwardWhy not create a single Collection data type that includes methods to add items, remove most recently inserted items, remove least recently inserted items, remove random items, iterate, return the collection's item count, and any other actions we might need? Then we could build each one in a single class that many clients could use. ans in 10 minarrow_forwardCreate a method countSelectedOdd that returns the count of the all the odd numbers greater than threshold.arrow_forward
- Write the Java implementation of an instance method, called countRange, inside the IntArrayBag class such that the method takes two input integer parameters, called start and end. The method then counts and returns how many elements in the bag fall in the range between start and end inclusive. For example, if start is 5 and end is 8, then the method counts how many elements in the bag are equal to 5,6,7, or 8. The method returns zero if start > end. Make sure to include the method header.arrow_forwardFor the first part of this lab, copy your working ArrayStringList code into the GenericArrayList class.(already in the code) Then, modify the class so that it can store any type someone asks for, instead of only Strings. You shouldn't have to change any of the actual logic in your class to accomplish this, only type declarations (i.e. the types of parameters, return types, etc.) Note: In doing so, you may end up needing to write something like this (where T is a generic type): T[] newData = new T[capacity]; ...and you will find this causes a compiler error. This is because Java dislikes creating new objects of a generic type. In order to get around this error, you can write the line like this instead: T[] new Data = (T[]) new Object[capacity] This creates an array of regular Objects which are then cast to the generic type. It works and it doesn't anger the Java compiler. How amazing! Once you're done, screenshot or save your code for checkin later. For the second part of the lab,…arrow_forwardIn java, Write a method void popMult(int k) for the ArrayBoundedStack class which will pop k items from this. Throw a StackUnderflowException and don't pop anything if there are fewer than k items on this. This is a method of the ArrayBoundedStack class. DO NOT USE ANY OTHER METHODS OF THE ARRAYBOUNDEDSTACK CLASS.arrow_forward
- Implement a new class that implements Comparatorof GregorianCalendar class to be able sort the calendar in increasingorder based on day, month, and year, in that order. Write a method to displaythe GregorianCalendar instance in “dd-MMM-yyyy” format usingSimpleDateFormatclass. Write a test program with 10 GregorianCalendarinstances and display the results after the sort.arrow_forwardWrite a class MySortedArrayCollection that Inherit the SortedArrayCollection class show in screen shot and such that all the following methods are implemented in java, then write a driver program to test the methods public String toString() // Creates and returns a string that correctly represents the current collection. public T smallest() // Returns null if the collection is empty, otherwise returns the smallest element of the collection. public int greater(T element) // Returns a count of the number of elements e in the collection that are greater then element, that is such that e.compareTo(element) is > 0 public MySortedArrayCollection <T> combine (MySortedArrayCollection<T>other) // Creates and returns a new SortedArrayCollection object that is a combination of this object and the argument object. public T []toArray() // Returns an array containing all of the elements of the collection. public void clear() // Removes all elements. public boolean equals(Object o)…arrow_forwardCan you implement the Student class using the concepts of encapsulation? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first. You are given a Student class in the editor. Your task is to add two fields: ● String name ● String rollNumber and provide getter/setters for these fields: ● getName ● setName ● getRollNumber ● setRollNumber Implement this class according to the rules of encapsulation. Input # Checking all fields and getters/setters Output # Expecting perfectly defined fields and getter/setters. There is no need to add constructors in this class.arrow_forward
- Why don't we have a unique Collection data type that uses methods to add items, remove the most recently inserted item, remove the least recently inserted item, remove random, iterate, return the total number of items in the collection, and whatever other actions we may need? Then we could build each one in a singular class that many clients could use.arrow_forwardWrite a method called flipMap() that takes in a map from string keys to string values, and returns a "reverse" or "inverse" version of...arrow_forwardProvide a different implementation of ChoiceQuestion. Instead of storing the choices in an array list, the addChoice method should add the choice to the question text. For this purpose, an addLine method has been added to the Question class. Use the following files: Question.java /** A question with a text and an answer.*/public class Question{ private String text; private String answer; /** Constructs a question with empty text and empty answer. */ public Question() { text = ""; answer = ""; } /** Sets the answer for this question. @param correctResponse the answer */ public void setAnswer(String correctResponse) { answer = correctResponse; } /** Checks a given response for correctness. @param response the response to check @return true if the response was correct, false otherwise */ public boolean checkAnswer(String response) { return response.equals(answer); } /** Add a line of text to…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education