1. Write a method called twiceArray. This method takes an array of double type,
ID: 3816389 • Letter: 1
Question
1. Write a method called twiceArray. This method takes an array of double type, it then multiplies each number in the array by 2, stores the new number back in the array and returns the array back to main.
2. Write a method called xArray. This method takes an array of type double, and another variable of type double (I will call this variable x). It then multiplies each number in the array by x, stores the new number back in the array and returns the array back to main.
This is to be done in Java Oracle and if possible done in one class. Thank you!
Explanation / Answer
public class Demo{
public static void main(String[] args) {
double arr[]={45.56,67.78,89.98,12.23,34.45,56.67};
int x=3;
arr=twiceArray(arr);
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
arr=xArray(arr,3);
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
private static double[] xArray(double[] arr, int x) {
for(int i=0;i<arr.length;i++)
{
arr[i]=arr[i]*x;
}
return arr;
}
private static double[] twiceArray(double[] arr) {
for(int i=0;i<arr.length;i++)
{
arr[i]=arr[i]*2;
}
return arr;
}
}
_________________
Output:
91.12 135.56 179.96 24.46 68.9 113.34
273.36 406.68 539.88 73.38 206.70000000000002 340.02
______________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.