Question

3. (5 pts) Write code for a Java method that takes in as input parameter an...

3. (5 pts) Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all entries in the array initialized to 3.0.

public static double[] arrayInit (int num){ // You have to declare and create a result array to be returned

} //end arrayInit

4. (6 pts) Write code for a Java method that takes in two int arrays of the same size as input parameters, multiplies the corresponding entries, and returns the resulting int array. public static int[] product (int[] array1, int[] array2){ // You have to declare and create a result array to be returned

} // end product

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   int num;
       System.out.print("Enter num: ");
       num=sc.nextInt();
       double result[]=arrayInit(num);
       for(int i=0;i<2*num;i++)
       System.out.print(result[i]+" ");
      
      
   }
   public static double[] arrayInit (int num){
   double r[] =new double[2*num];
   for(int i=0;i<2*num;i++)
   r[i]=3.0;
  
   return r;
  
   }
}

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   int n;
       System.out.print("Enter size of array: ");
       n=sc.nextInt();
   int a[]=new int[n];
   int b[]=new int[n];
   System.out.print("Enter value in array1: ");
   for(int i=0;i<n;i++)
   a[i]=sc.nextInt();
     
   System.out.print("Enter value in array2: ");
   for(int i=0;i<n;i++)
   b[i]=sc.nextInt();
     
   int c[]=product (a, b);
   for(int i=0;i<n;i++)
   System.out.print(c[i]+" ");
     
     
      
      
   }
public static int[] product (int[] array1, int[] array2){
int result[] = new int[array1.length];
for(int i=0;i<array1.length;i++)
result[i]=array1[i]*array2[i];
  
  
return result;
  
}
}

Add a comment
Know the answer?
Add Answer to:
3. (5 pts) Write code for a Java method that takes in as input parameter an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT