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

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...
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++
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT