Homework Help please asap: Exercise 7.11a Write an action method named clear for a subclass of ArrayList: The executor removes all of the values it contains. Exercise 7.11b Write a query method named contains for a subclass of ArrayList: The executor tells whether it contains a given Object parameter. Use the equals method for the test. Exercise 7.11c Write an action method named flip for a subclass of ArrayList: The executor swaps its first and second values, swaps its third and fourth values, swaps its fifth and six values, etc.
I'm having trouble on where to start in each exercise.
Set up each method, copy the assignment into it's method as a comment. Deal with each method in turn. Treat each as it's own problem. Decompose it until you're basically forced to write it as code to decompose it further.
I figured out how to do a and c (sort of) b is giving me a hard time.
for a and c I have a. public void Clear1() { // If we need to write this from scratch // to destroy all elements from the aray, i could make everything start over from the position 0. // this would work if i have an ArrayList<String> data; I could do data.clear int[] elementData; modCount++; // Thisis the NUmber of times an array List has been structurally modified //for loop , I set the size to 0 to start from position 0 //size = 0; for (int i = 0; i < size; i++) elementData[i] = null; } public void Clear2() { // I'm goingto create an empty array list with an initial capacity ArrayList<Integer> arrlist = new ArrayList<Integer>(5); // using the add() method to add elements in the list arrlist.add(10); arrlist.add(20); arrlist.add(30); arrlist.add(40); //arrlist.add(50); // printing all elements available for (Integer number : arrlist) { System.out.println("Number = " + number); } // we need to find the size of the list int find = arrlist.size(); System.out.println("The List consists of" + find +" elements"); arrlist.clear(); find = arrlist.size(); } c. // I didn't know if it was ok to do this public Exercises7_11() { super(); } public void flip() { // Swap arr[l] and arr[a] in array arr int temp = arr[l]; arr[l] = arr[a]; int[] list = {1, 2, 3, 4, 5}; swap(list,0, 4); for (int num : list) System.out.print(num + " "); }
@Mathexpert
It took time, but I tried my school videos and came up with something. Please check if it is right. I'm trying to do well so I can become a programmer. 7.11 b public void Contains1() { ArrayList<Integer> arrlist = new ArrayList<Integer>(5); // using the add() method to add elements in the list arrlist.add(15); arrlist.add(25); arrlist.add(35); arrlist.add(45); // list contains element 15 boolean check = arrlist.contains(15); if (check == true) { System.out.println("Element 15 is in the list"); } else { System.out.println("Element 15 is not in the list"); } } public boolean Contains2() { ArrayList<Integer> arrlist = new ArrayList<Integer>(5); //arrlist.add(15); //arrlist.add(25); //arrlist.add(35); //arrlist.add(45); List list = Arrays.asList(new String[] { "A", "B", "C", "D" }); List list2 = Arrays.asList(new String[] { "A", "B", "C" }); if (list.equals(list2)) { System.out.println("I have your elements in my list"); } else { System.out.println("Sorry, I don't have your elements in my list"); } }
For starters, the way I read the assignment, you're going to be extending ArrayList with a contains method. Does ArrayList have an equals method that you can use? Do the objects in the ArrayList have an equals method? Or do they have to provide a comparison method that your equals method calls?
I don't think so. I Have looked through my course textbook and it barely says anything about the equals method. I went to a java site and found something on the contains and clear methods. I was looking on the JavaSE7.
Testing primitives, ( int, char, bool, string ) is a fairly straightforward: if( a == b ) { // do something } Complex data types, like objects and collections are more difficult. An equals method would handle the comparison of your data types.
Thank You, I have reread my notes and study materials. my teacher gave me an A for parts B and A. For part c, the teacher cancelled it because it was not part of the school textbook.
Join our real-time social learning platform and learn together with your friends!