Question

Need some help with this java problem, thanks!Lab 6 dock Word File Home Insert Draw Design Layout References Mailings Review View Q lell me what you want to do How to Submit Please submit your answers to the lab instructor once you have completed. Failure to submit will result in a ZERO FOR THIS LAB NOEXCEPTIONS. 1. Write a program that prints out the lists after the following methods are called. a. Method miruroagon akes an Ar 5L or integers as a parameler. The method novcs the minimum valuc in the list to the front, oth prcscrving thc ordcr crwisc of the elements. For example, if a variable called list stores the following values: 4. 7,9, 2. 7, 7, 5, 3, 5, 1, 7, 8, 6, 71 and you make Lhis call: list. it should store the following values after the call: Li,4, 7, 9, 2,7, 7, 5, 3, 5. 7, 8, 6. 71. You may assume that the list stores at least one value. After the call print the list, b. Method ti terEange: accepts an ArrayList of integers and two integer values min and max as parameters and removes all elements whose values are in lhe range min through max (inclusive) from the list. For example, if a variable called list stores the values: [1. 4, 7, 9. 2, 7, 7, 5, 3, 5, 7, 8, 6, 71 The call of hould remo ove all values between 5 and is 7, therefore it should change the list to store [1,4, 9, 2, 3, R]. You may assume that the list is not null. Aner Lhe call print the list. c. Method intersec accepts two sorted array lists of integers as parameters and relurns a new list that contains only the elements lhal are found in b lists. For olh example, if lists named li and list2 ally store: [1, 4, 8, 9. 11, 15, 17, 28, 41, 59 [4, 7, 11, 17, 19, 20, 23, 28, 37, 59, 81] en the ca eturns the lis L2 L4, 11, 17, 28, 59 Print the list that is returned by thi method Page 1 of 1 334 words Dx Paull ojo Share 89%

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


public class ListUtil {
  
   public List<Integer> minToFront(List<Integer> list) {
       List<Integer> copy = new ArrayList<Integer>(list);
       int min = Collections.min(copy);
       copy.remove(min);
       copy.add(0, min);
       return copy;
   }
  
   public List<Integer> filterRange(List<Integer> list, int min, int max) {
       List<Integer> copy = new ArrayList<Integer>(list);
       Iterator<Integer> itr = copy.iterator();
       while(itr.hasNext()) {
           int value = itr.next();
           if(value == min || value == max || (value > min && value < max))
               itr.remove();
       }
      
       return copy;
   }
  
   public List<Integer> intersect(List<Integer> list1, List<Integer> list2) {
       List<Integer> copy1 = new ArrayList<Integer>(list1);
       List<Integer> copy2 = new ArrayList<Integer>(list2);
       List<Integer> intersection = new ArrayList<Integer>();
       Collections.sort(copy1);
       Collections.sort(copy2);
      
       Iterator<Integer> itr1, itr2;
       if(copy1.size() > copy2.size()) {
           itr1 = copy1.iterator();
           itr2 = copy2.iterator();
       } else {
           itr1 = copy2.iterator();
           itr2 = copy1.iterator();
       }
      
       int i = itr1.next();
       int j = itr2.next();
      
       while(itr1.hasNext()) {
           if(i == j) {
               if(!intersection.contains(i)) {
                   intersection.add(i);
                   i = itr1.next();
                   j = itr2.next();
               }
           }
           else if(i < j)
               i = itr1.next();
           else
               j = itr2.next();
       }
       intersection.add(i);
      
       return intersection;
   }
  
   public static void main(String[] args) {
       ListUtil listUtil = new ListUtil();
       List<Integer> list = Arrays.asList(new Integer[] {4, 7, 8, 9, 1});
       List<Integer> list1 = Arrays.asList(new Integer[] {4,7,1,3,5});
      
       System.out.println("After minimum to Front:" + listUtil.minToFront(list));
       System.out.println("After filtered via range:" + listUtil.filterRange(list, 5, 7));
       System.out.println("After intersection:" + listUtil.intersect(list, list1));
   }
}

Add a comment
Know the answer?
Add Answer to:
Need some help with this java problem, thanks! Failure to submit will result in a ZERO...
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
  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • Create a program to help some local coffee shops manage their menus Requirements The class contai...

    program is C# Create a program to help some local coffee shops manage their menus Requirements The class contains: 1) A method called defaultMenu that returns a String list and has no parameters. When called, the method returns a string list containing the following elements: {"Coffee", "Americano", Cappuccino" A method called custom Menu that returns a String list and has an int parameter. When called, the method will create a new string list, then ask the user for items to...

  • write a completed JAVA program (Main included) and make it not interactive but straight to the...

    write a completed JAVA program (Main included) and make it not interactive but straight to the output Write a method called partition that accepts a list of integers and an integer value E as its parameter, and rearranges (partitions) the list so that all the elements with values less than E occur before all elements with values greater than E The exact order of the elements is unimportant, so long as all elements less than E appear before all elements...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • by netBeans C++ java by netBeans C++ java by netBeans C++ java 1. Answer the following...

    by netBeans C++ java by netBeans C++ java by netBeans C++ java 1. Answer the following question a) (4 marks) Write a Java method, which takes an integer array and two integers x and y as input parameters. The method should exchange the value in position x with the value in the position y in the array. Example: If x = 1, y=2 Input Array Output Array b) (4 marks) Write the main method to do the following: 1. Define...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • (intro to java help?) Write a method manyStrings that takes an ArrayList of Strings and an...

    (intro to java help?) Write a method manyStrings that takes an ArrayList of Strings and an integer n as parameters and that replaces every String in the original list with n of that String. For example, suppose that an ArrayList called "list" contains the following values: ("squid", "octopus") And you make the following call: manyStrings(list, 2); Then list should store the following values after the call: ("squid", "squid", "octopus", "octopus") As another example, suppose that list contains the following: ("a",...

  • Write a method called reverseFirstK that accepts an integer k and a queue of integers as...

    Write a method called reverseFirstK that accepts an integer k and a queue of integers as parameters and reverses the order of the first k elements of the queue, leaving the other elements in the same relative order. For example, if a queue named q stores [10, 20 30, 40, 50, 60, 70, 80, 90], the call of reverseFirstK (4, q):should change the queue to store [40, 30 20, 10, 50, 60, 70, 80, 90]. If k is 0 or...

  • Hello, I need help writing the two methods to print a triangle shape in java. An...

    Hello, I need help writing the two methods to print a triangle shape in java. An example of the shape is as follows: 9 8 7 6 5 4 3 8 7 6 5 4 3 7 6 5 4 3 6 5 4 3 5 4 3 4 3 3 One method should be defined as "public static void printPattern(int num1, int num2, Boolean ascending)". This method will print a upper-triangle matrix-like layout filled will a sequence of integers...

  • daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function....

    daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function. Description Create a project called Daily21. Add a source file called daily21.c into the project. In this exercise, you will practice working with an array of integers. In the main function, you first need to create an integer array with 10 elements in t. Use a loop to read 10 integers from the user and store the values to the array (in the order...

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