4. write a Java application which contains a class called ArrayMethods. The Arra
ID: 3777837 • Letter: 4
Question
4. write a Java application which contains a class called ArrayMethods. The Array Methods class contains methods with the following headers: public int duplicate (char[] array1, char[] array2) This method copies each character from array array1 into the same location of array 2. ay2 must have already been instantiated and be of the same or greater array length as array1. The method retums the number of elements copied public boolean same(char[] array1. char[] array2) This method examines arrays array1 and array2 character by character and returns false if a character in array1 is different than a character at the corresponding location in array2 or if the arrays are of different length. The method returns true if every character of array.1 is the same as the comesponding character in array2. These are the only methods in the ArrayMethods class. Then write the test class called TestArrayMethods, within the main0 method, there should be statements as following,Explanation / Answer
//class ArrayMethods
public class ArrayMethods{
//variable to keep track of no.of elements copied
public int i;
//duplicate method to copy elements of array1 to array2
public int duplicate(char[] array1,char[] array2){
for( i=0;i<array1.length;i++)
array2[i]=array1[i];
return i; //return no.of elements copied
}
//same method to check whether both arrays are equal or not
public boolean same(char[] array1,char[] array2){
//length of array1 == length of array2
if(array1.length==array2.length){
for(int i=0;i<array1.length;i++){
if(array1[i]!=array2[i]) //element doesnot match
return false; //return false
}
}
else
return false; //return false when length of both array is different
return true; //return true if elements of both the arrays are equal
}
}
//end of class ArrayMethods
//class TestArrayMethods
public class TestArrayMethods{
public static void main(String []args){
//creating object of ArrayMethods class
ArrayMethods am=new ArrayMethods();
//intializing array1
char[] array1={'s','t','r','e','s','s'};
//declaring array2 with the same length of array1
char[] array2=new char[array1.length];
//declaring boolean variable
boolean ok;
//calling duplicate method of ArrayMethods
am.duplicate(array1,array2);
//calling same method of ArrayMethods
ok=am.same(array1,array2);
//printing elements of array1
System.out.println("Elements of the array1 : ");
for(int i=0;i<array1.length;i++)
System.out.print(array1[i]+" ");
System.out.println();
//printing elements of array2
System.out.println("Elements of the array2 : ");
for(int i=0;i<array2.length;i++)
System.out.print(array2[i]+" ");
System.out.println();
//printing value of ok
System.out.println("Value of ok variable : "+ok);
}
}
//end of class TestArrayMethods
/******OUTPUT***********
Elements of the array1 :
s t r e s s
Elements of the array2 :
s t r e s s
Value of ok variable : true
**********OUTPUT*********/
/* Note:This code has been tested on eclipse,Please ask in case of any doubt,Thanks. */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.