In: Computer Science
please write the java code so it can run on jGRASP Thanks!
CODE 1
1 /**
2 * SameArray2.java
3 * @author Sherri Vaseashta4 * @version1
5 * @see
6 */
7 import java.util.Scanner;8 public class SameArray29{
10 public static void main(String[] args) 11 {
12 int[] array1 = {2, 4, 6, 8, 10};
13 int[] array2 = new int[5]; //initializing array2
14
15 //copies the content of array1 and array2
16 for (int arrayCounter = 0; arrayCounter < 5;
arrayCounter++) 17 { 18 array2[arrayCounter] = array1[arrayCounter]; 19 } 20 21 System.out.println("Array 1" + " " + "Array2"); 22 23 for (int counter = 0; counter < 5; counter++) 24 { 25 System.out.println(array1[counter] + "\t\t\t" + array2[counter]); 26 } 27
28 //change one of the elements using array1
29 array1[0] = 200;
30
31 //change one of the elements using array2
32 array2[4] = 1000;
33 34 System.out.println("Array 1" + " " + "Array2"); 35 36 for (int counter = 0; counter < 5; counter++) 37 { 38 System.out.println(array1[counter] + "\t\t\t" + array2[counter]); 39 } 40 41 }//end of main 42 }//end of class
CODE 2
1 /**
2 * SameArray.java
3 *@author Sherri Vaseashta4 *@version 1
5 @see
6 */
7 import java.util.Scanner;8 public class SameArray9{
10 public static void main(String[] args) 11 { 12
13 int[] array1 = {2, 4, 6, 8, 10};
14 int[] array2 = array1; //creates a clone...not a
separate array
15
16 //change one of the elements using array1
17 array1[0] = 200;
18
19 //change one of the elements using array2
20 array2[4] = 1000;
21
22 //Display all the elements in array1
23 System.out.println("The contents of array1:");
24 25 for (int i = 0; i < 5; i++) 26 { 27 System.out.println(i + " " + array1[i]); 28 } 29
30 //Display all the elements in array2
31 System.out.println("The contents of array2:");
32 33 for (int counter = 0; counter < 5; counter++) 34 { 35 System.out.println(counter + " " + array2[counter]); 36 37 } 38 39 }//end of main() 40 }//end of class
---------------------------------------SameArray29.java--------------------------------------
import java.util.Scanner;
public class SameArray29{
public static void main(String[] args)
{
int[] array1 = {2, 4, 6, 8, 10};
int[] array2 = new int[5]; //initializing array2
//copies the content of array1 and array2
for (int arrayCounter = 0; arrayCounter < 5;
arrayCounter++)
{
array2[arrayCounter] = array1[arrayCounter];
}
System.out.println("Array 1" + " " + "Array2");
for (int counter = 0; counter < 5; counter++)
{
System.out.println(array1[counter] + "\t\t\t" +
array2[counter]);
}
//change one of the elements using array1
array1[0] = 200;
//change one of the elements using array2
array2[4] = 1000;
System.out.println("Array 1" + " " + "Array2");
for (int counter = 0; counter < 5; counter++)
{
System.out.println(array1[counter] + "\t\t\t" +
array2[counter]);
}
}//end of main
}//end of class
Above Program is a working program in JGRASP
Ouput:
------------------------------------------------SameArray9.java---------------------------------------
import java.util.Scanner;
class SameArray9 {
public static void main(String[] args) {
int[] array1 = {2, 4, 6, 8, 10};
int[] array2 = array1; //creates a clone...not a
//separate array
//change one of the elements using array1
array1[0] = 200;
//change one of the elements using array2
array2[4] = 1000;
//Display all the elements in array1
System.out.println("The contents of array1:");
for (int i = 0; i < 5; i++) {
System.out.println(i + " " + array1[i]);
}
//Display all the elements in array2
System.out.println("The contents of array2:");
for (int counter = 0; counter < 5; counter++) {
System.out.println(counter + " " + array2[counter]);
}
}//end of main()
}
Working program in JGRASP:
Output:
Summary:
Create java files with the same name as mentioned for class name, otherwise the program won't compile properly.