Please complete the java code below. public class Lab11 { //This function conver
ID: 3567718 • Letter: P
Question
Please complete the java code below. public class Lab11 { //This function converts the contents of the array of Strings into a single String with //appropriate spaces and returns it. //For example if String[] s = {"I", "am", "doing", "good."}; is an input to the function //The result is a String, I am doing good. public static String concatenateStrings(String[] strArray){ //Create a String variable <concatenatedStr> and initialize it to ""; //--> //Implement a for loop where the loop variable <i> goes from the 0 to the length of <strArray> (not included) for(int i=0; i < strArray.length;i++) { //Concatenate the string at index <i> of <strArray> to <concatenatedStr> concatenatedStr+= " " + ???; } //return <concatenatedStr> //--> } //This function calculates the sum of the array of integers and returns it. //For example if int[] s = {1, 2, 3}; is an input to the function //The result is an Integer, 6 public static double findSumOfArray(int[] intArray) { //Create an integer variable <sum> and initialize it to 0; //--> //Implement a for loop where the loop variable <i> goes from the 0 to the length of <intArray> (not included) for(int i=???; i < ???; i++) { //Add the integer at index <i> of <intArray> to <sum> //--> } //return <sum> //--> } //The main method begins here. The methods defined above will be used inside the main method. public static void main(String[] args) { //Create an array of Strings String[] brokenString = {"I", " was", "broken", "and", "you", "fixed", "me."}; //Create an array of first ten Natural numbers. int[] tenNaturalNos = ??? ???[???]; //Write a loop to initialize the array //Implement a for loop where the loop variable i goes from 0 to tenNaturalNos.length-1 for(???;???;???) { //Set the value of tenNaturalNos at index i to i+1, tenNaturalNos[i] = i+1; //--> } //Print the <brokenString> array System.out.println("The array of Strings : "); for(int i=0; i<brokenString.length; i++){ // print the string at i index of brokenString System.out.println(???[???]); } System.out.println("The array of Integers : "); //Print the array <tenNaturalNos> in the same manner using a for loop //--> //Invoke the concatenateStrings method using the array <brokenString> as input System.out.println("The array of Strings now becomes : "+ ???); //Similarly invoke the findSumOfArray method using the <tenNaturalNos> as input System.out.println("The sum of the integers in the array is : " + ???); } }
Explanation / Answer
Here you go :)
//Lab11 class
public class Lab11 {
public static String concatenateStrings(String[] strArray) {
String concatenatedStr = "";
for (int i = 0; i < strArray.length; i++) {
concatenatedStr += " " + strArray[i];
}
return concatenatedStr;
}
public static double findSumOfArray(int[] intArray) {
int sum = 0;
for (int i = 0; i < intArray.length; i++) {
sum += intArray[i];
}
return sum;
}
public static void main(String[] args) {
String[] brokenString = { "I", " was", "broken", "and", "you", "fixed",
"me." };
int[] tenNaturalNos = new int[10];
for (int i = 0; i < 10; i++) {
tenNaturalNos[i] = i + 1;
}
System.out.println("The array of Strings : ");
for (int i = 0; i < brokenString.length; i++) {
System.out.println(brokenString[i]);
}
System.out.println("The array of Integers : ");
System.out.println("The array of Natural numbers : ");
for (int i = 0; i < tenNaturalNos.length; i++) {
System.out.println(tenNaturalNos[i]);
}
System.out.println("The array of Strings now becomes : "
+ concatenateStrings(brokenString));
System.out.println("The sum of the integers in the array is : "
+ findSumOfArray(tenNaturalNos));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.