java code
level: beginner
write a method
sumList()
public static Integer sumList(ArrayList<Integer> list)
This method calculates the sum of all Integer values in a given ArrayList.
For example, if ArrayList<Integer> list contains {1, 2, 3}, a call to sumList(list) should return 1+2+3, which is 6 as an Integer.
strToList()
public static ArrayList<Character> strToList(String word)
This method transforms a String into an ArrayList<Character> containing each character of the string in order.
For example, strToWord("word") will return an ArrayList<Character> of four characters: {w,o,r,d} .
strToWord("hello world") will return an
ArrayList<Character>: {h,e,l,l,o, ,w,o,r,l,d}
findAverage()
public static double findAverage(ArrayList<Integer> list)
This method returns the average value in the given ArrayList.
For example, if ArrayList<Integer> list is {1, 2, 3, 4}, findAverage(list) will return 2.5.
import java.util.*;
public class Reccursive {
public static Integer sumList(ArrayList<Integer> list)
{
if(list.size()==0)
return null;
if(list.size()==1)
return list.get(0);
return list.remove(0)+sumList(list);
}
public static ArrayList<Character> strToList(String
word)
{
ArrayList<Character> clis=new
ArrayList<Character>();
if(word.length()==0)
return null;
if(word.length()==1)
{clis.add(word.charAt(0));
return clis;
}
clis.add(word.charAt(0));
clis.addAll(strToList(word.substring(1)));
return clis;
}
public static Double findAverage(ArrayList<Integer>
list)
{
int n=list.size();
if(n==0)
return null;
if(n==1)
return 1.0*list.get(0);
//System.out.println(list);
return
(list.remove(0)+findAverage(list)*(n-1))/(1.0*n);
}
public static void main(String[] args) {
ArrayList<Integer> list =new
ArrayList<Integer>();
System.out.println("sum of {1,2,3}=");
list.add(1);list.add(2);list.add(3);
System.out.println(Reccursive.sumList(list));
System.out.println("hello world to list:");
System.out.println(Reccursive.strToList("Hello
world"));
ArrayList<Integer> list2 =new
ArrayList<Integer>();
list2.add(1);list2.add(2);list2.add(3);list2.add(4);
System.out.println("Average of {1,2,3,4}=");
System.out.println(Reccursive.findAverage(list2));
}
}
java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates...
//Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.
Write a method called stutter that accepts an ArrayList of strings and an integer k as parameters and that replaces every string with k copies of that string. For example, if the list stores the values ["how", "are", "you?"] before the method is called and k is 4, it should store the values ["how", "how", "how", "how", "are", "are", "are", "are", "you?", "you?", "you?", "you?"] after the method finishes executing. If k is 0 or negative, the list should be...
(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 that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the method returns a new ArrayList containing the elements from the original ArrayList that come after the first occurrence of Integer parameter in the original ArrayList. If the original ArrayList does not contain an occurrence of the Integer parameter return an empty ArrayList . The original ArrayList must be unaffected by the method execution. Following are some examples of the invocation of the method using...
Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...
in Java please (a) Write a static method abbreviate( ) which is passed a String s and an int max. The method returns a new String which contains the content of s abbreviated to at most max characters, but where the last three characters are ellipses (i.e., the characters . . .). For example, if s is "Too bad Spongebob’s not here to enjoy Spongebob not being here. ---Squidward." and max is 10, the method returns the 10-character String "Too...
Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...
JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet { /** * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList * containing all the indexes of Strings in arr that match String s. For this method, two Strings, * p and q, match when p.equals(q) returns true or if both of the compared references are null. * * @param s The string...
Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...
Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...