Create a class called \"Arrays1D\" with 5 methods (including the main method). T
ID: 3873903 • Letter: C
Question
Create a class called "Arrays1D" with 5 methods (including the main method). The class "Arrays1D" must be public. All methods must be public and static.
Method name: print
Input: 1 double array
Output: nothing
Purpose: prints the content of the given array to the console (standard out). The print method must use a specific format. If the array is {1.1, 2.2, 3.1, 4.8}, then the output should be "{1.1, 2.2, 3.1, 4.8} ". Be mindful of the brackets, commas, and spaces. Note that there is one newline in the output.
Method name: average
Input: 1 double array
Output: 1 double
Purpose: returns the average of numbers in an array
Method name: addOne
Input: 1 double array
Output: 1 double array
Purpose: returns a copy of the input array with 1 added to each value in the input array. For example, if you call addOne with input {1,2,3,4} it should return {2,3,4,5}.
Hint: the example above just copies an array. You should be able to modify the code in the example to write the "addOne" method.
Method name: concat
Input: 2 double arrays
Output: 1 double array
Purpose: returns an array containing the elements of the first parameter followed by the elements of the second parameter
Method name: main
Input: String[] args
Output: void
Purpose: test your code by printing the return of the following method calls in order. Each call should be printed on a newline. Your "print" method will be very useful here. Note: the following is NOT valid java code. It just gives you an idea of what to print.
print( {3.14, 2.24, 1.9, 6.21} )
average( {3.14, 2.24, 1.9, 6.21} )
addOne( {3.0, 5.0, 7.0, 1.0, 2.0} )
concat( {3.0, 5.0, 7.0, 1.0, 2.0}, {13.0, 15.0, 17.0, 11.0, 12.0} )
Output (newline characters are shown as ):
Explanation / Answer
import java.io.*;
public class Arrays1D{
public static void print(double[] a){
for(int i = 0; i<a.length; i++){
System.out.print(a[i] + " ");
}
System.out.println();
}
public static double average(double[] a){
double sum = 0;
for(int i = 0; i<a.length; i++){
sum = sum + a[i];
}
return sum/a.length;
}
public static double[] addOne(double[] a){
for(int i = 0; i<a.length; i++){
a[i] = a[i] + 1;
}
return a;
}
public static double[] concat(double[] a,double[] b ){
double[] c = new double[a.length + b.length];
for(int i = 0; i<a.length; i++){
c[i] = a[i];
}
for(int i = 0; i<b.length; i++){
c[a.length+i] = b[i];
}
return c;
}
public static void main(String[] args){
double[] a1 = new double[4];
a1[0] = 3.14;
a1[1] = 2.24;
a1[2] = 1.9;
a1[3] = 6.21;
print(a1);
System.out.println(average(a1));
double[] a2 = new double[5];
a2[0] = 3.0;
a2[1] = 5.0;
a2[2] = 7.0;
a2[3] = 1.0;
a2[4] = 2.0;
double[] b = addOne(a2);
double[] a3 = new double[5];
a3[0] = 13.0;
a3[1] = 15.0;
a3[2] = 17.0;
a3[3] = 11.0;
a3[4] = 12.0;
double[] c = concat( a2, a3 );
print(b);
print(c);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.