Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Jessica Wong Pd.4
- * Write the following methods and test them on the given lists. Once they work, create your own list and test the methods on them.
- *
- * 1) For a list of Strings, write a method, dupString, that will duplicate all elements that have 3 or more characters
- * Ex: [The, constant, Pi, is, Awesome] should become [The, The, constant, constant, Pi, is, Awesome, Awesome]
- *
- * 2) For a list of Integers, write a method, removeEvens, that will remove all even integers from the list.
- * Ex: [1, 2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10] should become [1, 3, 5, 7, 9]
- *
- * 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
- * Ex: [1.2, 5.4, 7.2, 2.8, 9.8] should become [1.2, 5.0, 5.0, 2.8, 5.0]
- *
- * Challenge:
- * 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.
- * 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
- * there are no elements remaining in the list.
- *
- */
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- //You can create a list and then add elements one at a time
- List<String> lstStr = new ArrayList<>();
- lstStr.add("Hi");
- lstStr.add("Bye");
- lstStr.add("Mr. Peterson");
- lstStr.add("Comp Sci");
- //or you can add elements using a loop
- for (int count = 65; count < 91; count++) {
- String s;
- if(count%2==0)
- s = ""+(char)count+(char)(count)+(char)(count)+(char)(count)+(char)(count);
- else
- s = ""+(char)count+(char)(count);
- lstStr.add(s);
- }
- //You can print the elements of your list using a loop
- for(int i =0; i<lstStr.size(); i++) {
- System.out.print(lstStr.get(i)+" ");
- }
- System.out.println();
- //or you can use the List interface's toString method and just print the list (with prettiness too!)
- System.out.println(lstStr);
- //You can create a list like you would an array in the following way
- List<Integer> lstInt = new ArrayList<>(Arrays.asList(1,2,2,3,3,4,4,5,6,7,8,9,10));
- System.out.println(lstInt);
- List<Double> lstDoub = new ArrayList<>(Arrays.asList(1.2, 5.4, 7.2, 2.8, 9.8));
- System.out.println(lstDoub);
- List<Double> lstDoub2 = new ArrayList<>(Arrays.asList(1.2, 5.4, 7.2, 2.8, 9.8));
- System.out.println(lstDoub2);
- //Now you will test the methods. DO NOT ALTER THE LISTS ABOVE.
- System.out.println();
- System.out.println("Here are the results of my methods: ");
- dupString(lstStr);
- System.out.println(lstStr);
- removeEvens(lstInt);
- System.out.println(lstInt);
- setToFive(lstDoub);
- System.out.println(lstDoub);
- setToAverage(lstDoub2);
- System.out.println(lstDoub2);
- //CREATE YOUR OWN LISTS AND TEST THE METHODS BELOW
- List<String> list1 = new ArrayList<>(Arrays.asList("The","mitochondria","is","the","powerhouse","of","the","cell")); //converts array 2 list
- System.out.println(list1);
- 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));
- System.out.println(list2);
- 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));
- System.out.println(list3);
- List<Double> list4 = new ArrayList<>(Arrays.asList(40.2,13.3,24.7,18.2,15.2,85.2,57.2));
- System.out.println(list4);
- System.out.println();
- System.out.println("Here are the results of my methods: ");
- dupString(list1);
- System.out.println(list1);
- removeEvens(list2);
- System.out.println(list2);
- setToFive(list3);
- System.out.println(list3);
- setToAverage(list4);
- System.out.println(list4);
- }
- public static void dupString(List<String> lst) {
- for(int i=0; i<lst.size(); i++){
- if(lst.get(i).length()>=3){
- lst.add(i,lst.get(i));
- i++;
- }
- }
- }
- public static void removeEvens(List<Integer> lst) {
- for(int i=0; i<lst.size(); i++){
- if(lst.get(i)%2==0){
- lst.remove(i);
- i--;
- }
- }
- }
- public static void setToFive(List<Double> lst) {
- for(int i=0; i<lst.size(); i++){
- if(lst.get(i)>5.0){
- lst.set(i,5.0);
- }
- }
- }
- public static void setToAverage(List<Double> lst) {
- double sum=0;
- double count=-1;
- for(int i=0; i<lst.size(); i++){
- sum+=lst.get(i);
- count++;
- }
- for(int i=0; i<lst.size(); i++){
- sum = sum-lst.get(i);
- if(lst.get(i) > 5.0 && count>0){
- lst.set(i,sum/count);
- }
- count--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement