10. (4 marks) Define the headers of the following two methods. You need to assum
ID: 3709145 • Letter: 1
Question
10. (4 marks) Define the headers of the following two methods. You need to assume that they are defined in a class and that they are invoked through the class. a) A method that searches a value from an integer array. An integer value and an integer array are passed to this method, and this method returns the index of the value in the array A method that copies an integer array to a new array. A source array is passed to this method, and this method returns the new array that have the same elements in the source array. b)Explanation / Answer
import java.util.*;
class arnum{
public static void main(String[] args) {
int[] arr = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
System.out.println("Enter element to search");
Scanner sc=new Scanner(System.in);
int value=sc.nextInt();
int index=search(value,arr);
System.out.println("index is"+index);
System.out.println("enter 1 to copy arra");
int respond=sc.nextInt();
if(respond==1){
copy(arr);
}
else{
System.out.println("you hava declined to copy");
}
}
static int search(int value,int arr[]){
int len=arr.length;
for(int i=0;i<len;i++){
if(arr[i]==value){
return i;
}
}
return 0;
}
static void copy(int sarr[]){
int len=sarr.length;
int darr[]=new int[len];
for(int i=0;i<len;i++){
darr[i]=sarr[i];
}
//display darray
System.out.println("new arry is =");
for(int i=0;i<len;i++){
System.out.print(darr[i]+",");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.