Question

In: Computer Science

Complete the following method in java: public static List<Integer> runningMedianOfThree(List items) Create and return a new...

Complete the following method in java:

public static List<Integer> runningMedianOfThree(List items)

Create and return a new List<Integer> instance (of any subtype of List of your choice) whose first two elements are the same as that of original items, after which each element equals the median of the three elements in the original list ending in that position. For example, when called with a list that prints out as [5, 2, 9, 1, 7, 4, 6, 3, 8], this method would return an object of type List<Integer> that prints out as [5, 2, 5, 2, 7, 4, 6, 4, 6].

Solutions

Expert Solution

Java Program:

import java.util.*;

class MedianList
{
   //Main method
   public static void main(String args[])
   {
       //Creating original list
       List<Integer> orgLst = new ArrayList<Integer>();
      
       //Adding elements
       orgLst.add(5);
       orgLst.add(2);
       orgLst.add(9);
       orgLst.add(1);
       orgLst.add(7);
       orgLst.add(4);
       orgLst.add(6);
       orgLst.add(3);
       orgLst.add(8);
      
       System.out.print("\nOriginal List: ");
          
       //ITerating and printing elements
       for(Integer a:orgLst)
       {
           System.out.print(a + " ");
       }
      
       //Calling function
       List<Integer> retLst = runningMedianOfThree(orgLst);
      
       System.out.print("\n\nList after calculating median: ");
          
       //ITerating and printing elements
       for(Integer a:retLst)
       {
           System.out.print(a + " ");
       }
   }
  
   //Method that finds the median of three values
   public static List<Integer> runningMedianOfThree(List<Integer> items)
   {
       //Creating a new list
       List<Integer> retLst = new ArrayList<Integer>();
      
       //Adding first two elements
       retLst.add(items.get(0));
       retLst.add(items.get(1));
      
       int median;
      
       //Iterating from third elements
       for(int i=2; i<items.size(); i++)
       {
           //Calculating median
           if ((items.get(i-2) > items.get(i-1)) != (items.get(i-2) > items.get(i)))
           {
               median = items.get(i-2);
           }
           else if ((items.get(i-1) > items.get(i-2)) != (items.get(i-1) > items.get(i)))
           {
               median = items.get(i-1);
           }
           else
           {
               median = items.get(i);
           }
          
           //Adding median value
           retLst.add(median);
       }
      
       //Returning new list
       return retLst;
   }

}

___________________________________________________________________________________________________________________

Sample Run:


Related Solutions

In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
Write a complete static method named addUp that accepts, in order, an integer number and an...
Write a complete static method named addUp that accepts, in order, an integer number and an integer thisMany as parameters and that prints a complete line of output reporting all the numbers that result from adding number to itself thisMany number of times (starting at 1 and ending at thisMany). For example, the following calls: addUp(3, 5); addUp(7, 3); should produce this output: Adding up 3 for 5 times gives: 3, 6, 9, 12, 15 Adding up 7 for 3...
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome...
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: You'll find your isPalindrome method helpful here. This method calls for an optimization loop.)
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer,...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer, String>(); ssnMap.put (8675309,"Jenney"); ssnMap.put (42, "Answer to Everything"); ssnMap.put (8675309, "Stacy"); ssnMap.put (1006, "Peter"); System.out.println(ssnMap.get (8675309)); } } What is the output of the above code. Why?
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Create a ValueGet() method that takes a linked list as input and an integer index and...
Create a ValueGet() method that takes a linked list as input and an integer index and returns the value stored in the node at that index position. Sample Input: 01->100->300->214, index = 2 Output: 300 At index 2 the node has a value of 300 give me the full code. Give the full code with c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT