1. Write a method that converts all Strings in an array to upper case. The metho
ID: 3665962 • Letter: 1
Question
1. Write a method that converts all Strings in an array to upper case. The method header is:
public void convertToUpper(String[] words)
2. Write a method to create a new array that contains only the elements between specified indices (inclusive). The method header is:
public int[] subArray(int[] numbers, int start, int end)
For example, if the method was invoked with {1, 5, 2, 4, 7, 3}, start = 1, end = 4, the method would return {5, 2, 4, 7}.
Include appropriate error checking for the indices.
3. Write two methods that double array elements. The first doubles the elements in the array passed in as a parameter. The second creates a new array that contains double the elements of the array passed in as a parameter. In the second method, the array passed in as a parameter should not change. The method headers are:
public void doubleElementsInArray(int[] numbers)
public int[] createDoubleElementsArray(int[] numbers)
Explanation / Answer
import java.util.Arrays;
/**
* @author Srinivas Palli
*
*/
public class Question1 {
/**
* @param args
*/
public static void main(String[] args) {
String words[] = { "srinivas", "rajesh", "ravi", "pavan" };
Question1 arraysService = new Question1();
System.out.print(" convertToUpper():");
arraysService.convertToUpper(words);
}
/**
* method that converts all Strings in an array to upper case.
*
* @param words
*/
public void convertToUpper(String[] words) {
try {
for (int i = 0; i < words.length; i++) {
words[i] = words[i].toUpperCase();
System.out.print(words[i] + " ");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
import java.util.Arrays;
/**
* @author Srinivas Palli
*
*/
public class Question2 {
/**
* @param args
*/
public static void main(String[] args) {
String words[] = { "srinivas", "rajesh", "ravi", "pavan" };
Question2 arraysService = new Question2();
int numbers[] = { 1, 5, 2, 4, 7, 3 };
int newArr[] = arraysService.subArray(numbers, 1, 4);
System.out.println(" subArray():" + Arrays.toString(newArr));
}
/**
* method to create a new array that contains only the elements between
* start and end indices
*
* @param numbers
* @param start
* @param end
* @return
*/
public int[] subArray(int[] numbers, int start, int end) {
int newArr[] = new int[(end - start) + 1];
try {
int j = 0;
for (int i = start; i <= end; i++) {
newArr[j] = numbers[i];
j++;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return newArr;
}
}
import java.util.Arrays;
/**
* @author Srinivas Palli
*
*/
public class Question3 {
/**
* @param args
*/
public static void main(String[] args) {
String words[] = { "srinivas", "rajesh", "ravi", "pavan" };
Question3 arraysService = new Question3();
int numbers[] = { 1, 5, 2, 4, 7, 3 };
System.out.print(" doubleElementsInArray():");
arraysService.doubleElementsInArray(numbers);
int newArr1[] = arraysService.createDoubleElementsArray(numbers);
System.out.print(" createDoubleElementsArray():"
+ Arrays.toString(newArr1));
}
/**
* method to doubles the elements in the array
*
* @param numbers
*/
public void doubleElementsInArray(int[] numbers) {
try {
for (int i = 0; i < numbers.length; i++) {
numbers[i] = numbers[i] * numbers[i];
System.out.print(numbers[i] + " ");
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* method to creates a new array that contains double the elements of the
* array
*
* @param numbers
* @return
*/
public int[] createDoubleElementsArray(int[] numbers) {
int squareNumbers[] = new int[numbers.length];
try {
for (int i = 0; i < numbers.length; i++) {
squareNumbers[i] = numbers[i] * numbers[i];
}
} catch (Exception e) {
// TODO: handle exception
}
return squareNumbers;
}
}
OUTPUT:Question1
convertToUpper():SRINIVAS RAJESH RAVI PAVAN
OUTPUT:Question2
subArray():[5, 2, 4, 7]
OUTPUT:Question3
doubleElementsInArray():1 25 4 16 49 9
createDoubleElementsArray():[1, 625, 16, 256, 2401, 81]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.