Advertisement
pro-themes

List:Remove Stuff

Jun 13th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.47 KB | None | 0 0
  1. /**
  2.  * Jessica Wong Pd.4
  3.  * Write the following methods and test them on the given lists. Once they work, create your own list and test the methods on them.
  4.  *
  5.  * 1) For a list of Strings, write a method, dupString, that will duplicate all elements that have 3 or more characters
  6.  *      Ex: [The, constant, Pi, is, Awesome] should become [The, The, constant, constant, Pi, is, Awesome, Awesome]
  7.  *
  8.  * 2) For a list of Integers, write a method, removeEvens, that will remove all even integers from the list.
  9.  *      Ex: [1, 2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10] should become [1, 3, 5, 7, 9]
  10.  *
  11.  * 3)  For a list of Doubles, write a method, setToFive. If the current element is greater than 5, then it should be set to 5.0
  12.  *      Ex: [1.2, 5.4, 7.2, 2.8, 9.8] should become [1.2, 5.0, 5.0, 2.8, 5.0]
  13.  *
  14.  * Challenge:
  15.  * 3) For a list of Doubles, write a method, setToAverage. If the current element is greater than 5, then it should be set to the average of the remaining elements in the list.
  16.  *      Ex: [1.2, 5.4, 7.2, 2.8, 9.8] should become [1.2, 6.6, 6.3, 2.8, 9.8]  where 6.6 is the average of 7.2, 2.8, and 9.8 and 6.3 is the average of 2.8 and 9.8. 9.8 doesn't change because
  17.  *          there are no elements remaining in the list.
  18.  *
  19.  */
  20. import java.util.*;
  21.  
  22. public class Main {
  23.  
  24.     public static void main(String[] args) {
  25.         //You can create a list and then add elements one at a time
  26.         List<String> lstStr = new ArrayList<>();
  27.         lstStr.add("Hi");
  28.         lstStr.add("Bye");
  29.         lstStr.add("Mr. Peterson");
  30.         lstStr.add("Comp Sci");
  31.         //or you can add elements using a loop
  32.         for (int count = 65; count < 91; count++) {
  33.             String s;
  34.             if(count%2==0)
  35.                 s = ""+(char)count+(char)(count)+(char)(count)+(char)(count)+(char)(count);
  36.             else
  37.                 s = ""+(char)count+(char)(count);
  38.             lstStr.add(s);
  39.         }
  40.        
  41.         //You can print the elements of your list using a loop
  42.         for(int i =0; i<lstStr.size(); i++) {
  43.             System.out.print(lstStr.get(i)+" ");
  44.         }
  45.         System.out.println();
  46.        
  47.         //or you can use the List interface's toString method and just print the list (with prettiness too!)
  48.         System.out.println(lstStr);
  49.        
  50.         //You can create a list like you would an array in the following way
  51.         List<Integer> lstInt = new ArrayList<>(Arrays.asList(1,2,2,3,3,4,4,5,6,7,8,9,10));
  52.         System.out.println(lstInt);
  53.        
  54.         List<Double> lstDoub = new ArrayList<>(Arrays.asList(1.2, 5.4, 7.2, 2.8, 9.8));
  55.         System.out.println(lstDoub);
  56.        
  57.         List<Double> lstDoub2 = new ArrayList<>(Arrays.asList(1.2, 5.4, 7.2, 2.8, 9.8));
  58.         System.out.println(lstDoub2);
  59.        
  60.         //Now you will test the methods. DO NOT ALTER THE LISTS ABOVE.
  61.         System.out.println();
  62.         System.out.println("Here are the results of my methods: ");
  63.         dupString(lstStr);
  64.         System.out.println(lstStr);
  65.        
  66.         removeEvens(lstInt);
  67.         System.out.println(lstInt);
  68.        
  69.         setToFive(lstDoub);
  70.         System.out.println(lstDoub);
  71.  
  72.         setToAverage(lstDoub2);
  73.         System.out.println(lstDoub2);
  74.        
  75.         //CREATE YOUR OWN LISTS AND TEST THE METHODS BELOW
  76.        
  77.         List<String> list1 = new ArrayList<>(Arrays.asList("The","mitochondria","is","the","powerhouse","of","the","cell")); //converts array 2 list
  78.         System.out.println(list1);
  79.        
  80.         List<Integer> list2 = new ArrayList<>(Arrays.asList(13,24,1,3,6,2,6,8,12,65,13,86,23,54,23,85,43,32,7));
  81.         System.out.println(list2);
  82.        
  83.         List<Double> list3 = new ArrayList<>(Arrays.asList(3.2,5.5,3.8,1.5,2.8,9.5,6.4,7.3,1.2,10.2));
  84.         System.out.println(list3);
  85.        
  86.         List<Double> list4 = new ArrayList<>(Arrays.asList(40.2,13.3,24.7,18.2,15.2,85.2,57.2));
  87.         System.out.println(list4);
  88.        
  89.         System.out.println();
  90.         System.out.println("Here are the results of my methods: ");
  91.        
  92.         dupString(list1);
  93.         System.out.println(list1);
  94.        
  95.         removeEvens(list2);
  96.         System.out.println(list2);
  97.        
  98.         setToFive(list3);
  99.         System.out.println(list3);
  100.        
  101.         setToAverage(list4);
  102.         System.out.println(list4);
  103.     }
  104.    
  105.     public static void dupString(List<String> lst) {
  106.        
  107.         for(int i=0; i<lst.size(); i++){
  108.             if(lst.get(i).length()>=3){
  109.                 lst.add(i,lst.get(i));
  110.                 i++;
  111.             }
  112.         }
  113.     }
  114.    
  115.     public static void removeEvens(List<Integer> lst) {
  116.        
  117.          for(int i=0; i<lst.size(); i++){
  118.              if(lst.get(i)%2==0){
  119.                  lst.remove(i);
  120.                  i--;
  121.              }
  122.         }
  123.     }
  124.    
  125.     public static void setToFive(List<Double> lst) {
  126.        
  127.         for(int i=0; i<lst.size(); i++){
  128.             if(lst.get(i)>5.0){
  129.             lst.set(i,5.0);
  130.             }
  131.         }
  132.     }
  133.    
  134.     public static void setToAverage(List<Double> lst) {
  135.        
  136.         double sum=0;
  137.         double count=-1;
  138.         for(int i=0; i<lst.size(); i++){
  139.             sum+=lst.get(i);
  140.             count++;
  141.         }
  142.        
  143.         for(int i=0; i<lst.size(); i++){
  144.                 sum = sum-lst.get(i);
  145.                 if(lst.get(i) > 5.0 && count>0){
  146.                 lst.set(i,sum/count);
  147.                 }
  148.                 count--;
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement