11. 1. Create a Fruit class. The class must contain an int weight field, a constructor with initialization of this field, and the printWeight () method, which prints the weight value.
In the main method, add several Fruit objects to the List container. Call printWeight () for each of the objects in the following ways:
A) using the get () method;
B) using an iterator.
11.2. The Fruit class from 11.1 is placed in the map container. Let the name of the owner be specified as the key, the value of the object of the type Fruit. Implement iteration of each Fruit object by key and calling the printWeight () method for the found object: using foreach; using an iterator.
AnswerThanks
quarantino for a compact version of the solution with an iterator.
Fruit class - see task 11.2
import java.util.*; import java.util.Map.Entry; public class Apply { public static void main(String[] args) {
11.3. Using the Comparator for PriorityQueue, ensure that string variables are sorted by size.
AnswerNote: The example is taken from the Internet.
Translation of exercises from the original book:11.4. Create a new Gerbil class with an int gerbilNumber field. Let the field be initialized by the constructor. Also create a hop () method that prints Gerbil's number which is hopping is: and the value of the gerbilNumber variable. Place the Gerbil objects in the ArrayList container. Implement the get () method to traverse the list with a call to the hop () method for each of these objects.
11.5. Modify SimpleCollection.java to use the Set method for the variable "c". Modify innerclasses / Sequence.java so that any number of elements can be added there.
11.6. Write a class Generator, which has a list of the names of the characters of your favorite movies (objects of type String). Let each object in this list be returned using the next () method. When the end of the created list is reached, it is necessary to go back to the top of the list.
Use the Generator class to populate containers like ArrayList, LinkedList, HashSet, LinkedHashSet, TreeSet. Write a universal method that would output the contents of each of the arrays.
11.7. Modify ListFeatures.java so that it uses objects of type Integers instead of objects of type Pets. Explain the differences in the results of work arising from this modification.
11.8. Repeat exercise 11.7 for ListFeatures.java, but replace Pets with Strings.
11.9. Create a class that generates an initialized array of objects of another class. Using this array, fill the container of type List. Create another container using subList () for the List. Then remove the elements that you extracted using subList () from the container from which you extracted them.
11.10. Humorous exercise on the use of various containers and their methods (designed by yourself)
Suppose we have a list of expenses, initially consisting of 3 elements: “food”, “medicine”, “entertainment”. It is necessary to write these elements into the List container, and their order should not change.
What happened next?1) We suddenly realized that we had forgotten to add credit costs to the list. It is necessary to place the “taxes” entry between “food” and “medicine” without using record indexes directly.
2) The wife dragged an additional separate list of expenses with entries: "coat", "restaurant". There is nothing to do: add costs to the end of the list (so as not to be deceived).
3) However, the mother-in-law dragged us a couple more points: “theater”, “home decoration” and “medicine”. Maybe you already had one of these items. Just in case, copy the contents of the first container to the new container of Set type and then, without hesitation, add these fad to the end of the list.
4) Without hesitation, we have created an associative dynamic list of expenses map <String, Integer>, where Integer is the planned costs for each of the items of expenses (choose the amounts yourself).
5) We became extremely wealthy and decided to increase the amount of all costs by 30%. Write the method that does it for you.
6) Stop! Enough with us "home decoration!" And, perhaps, on the coat goes too much. Add these items to the new List and pass to the method, which, having considered this sheet, in the Map container will find the corresponding items of expenses and reduce them by ... 80%!
7) Svalim-ka part of the cost of the mother in law. Moreover, we will make her a surprise! We will generate two random numbers that will determine the starting and ending indices, all costs between which will be recorded in a separate List (which later will be slipped by the mother-in-law). Do not forget to update the Map, throwing out the corresponding expenditure items from it.
8) We will write a method that determines what the smallest amount of expenses goes on, we print the name of these expenses and proudly remove this item from the list.
9) However, additional consumables added to us: “food” and “restaurant”, decorated in the form of another array! Check if these items are on our list? And if at least one expense is, then in no case will we add them! If there is nothing of this, then (nothing can be done), we add both of these points at once (as I got hungry!).
10) We are crazy! From the Map list, we will read all the Integer sums we have left, write them into “some” container, which will sort them out ascending, then mix them up with a rage and delete them in the reverse order, furiously applying a special iterator for this.