Question

In: Computer Science

please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...

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    {
  1. 12        int[] array1 = {2, 4, 6, 8, 10};
    
  2. 13        int[] array2 = new int[5]; //initializing array2
    

14

  1. 15        //copies the content of array1 and array2
    
  2. 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
  1. 28        //change one of the elements using array1
    
  2. 29        array1[0] = 200;
    

30

  1. 31        //change one of the elements using array2
    
  2. 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
  1. 13        int[] array1 = {2, 4, 6, 8, 10};
    
  2. 14        int[] array2 = array1;  //creates a clone...not a
    
separate array

15

  1. 16        //change one of the elements using array1
    
  2. 17        array1[0] = 200;
    

18

  1. 19        //change one of the elements using array2
    
  2. 20        array2[4] = 1000;
    

21

  1. 22        //Display all the elements in array1
    
  2. 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
  1. 30        //Display all the elements in array2
    
  2. 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

Solutions

Expert Solution

---------------------------------------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.


Related Solutions

please write the java code so it can run on jGRASP Thanks! 1 /** 2 *...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 * PassArray 3 * @Sherri Vaseashta 4 * @Version 1 5 * @see 6 */ 7 import java.util.Scanner; 8 9 /** 10 This program demonstrates passing an array 11 as an argument to a method 12 */13 14 public class PassArray 15 { 16 public static void main(String[] args) 17 { 18 19 final int ARRAY_SIZE = 4; //Size of the array 20 // Create...
please right make it so that it can run on jGRASP and java code please thank...
please right make it so that it can run on jGRASP and java code please thank you Debug Problem 1: As an intern for NASA, you have been instructed to debug a java program that calculates the speed that sound travels in water. Details about the formulas and correct results appear in the comments area at the top of the program Here is the code to debug: importjava.util.Scanner; /**    This program demonstrates a solution to the    The Speed of Sound...
Please write this program in JGRASP thanks you so much This program assesses your ability to...
Please write this program in JGRASP thanks you so much This program assesses your ability to use functions, arrays, for loops, and if statements where needed.You are writing a program to track bird sightings in Prince William Countyfor aweekend event where bird watchers watch birds and record each unique species that they see. Over a weekend, Saturday and Sunday, the bird watchers (aka birders) are going to record how many birdspeciesthey sighteach day. You begin by asking the user how...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803 This assignment involves extension to the single source-single destination shortest path problem. The Program Your program should: 1. Read the name of a text file from the console. (Not the command line) 2. Read an undirected graph from the file. 3. Find the shortest path between the start and goal vertices specified in the file. 4. Print out the vertices on the path, in...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public static void main(String[] args) {        // TODO Auto-generated method stub        getupdates();    }...
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If inRun...
Write a short, cohesive java program in jGrasp that interacts with the user and does the...
Write a short, cohesive java program in jGrasp that interacts with the user and does the following: create at least 2 double variables create at least 1 constant get at least 1 string input from the user get at least 1 int/double input from the user include both, incorporating the input you got from the user: 1 multiway if-else with at least 3 "else if" 1 nested if Your program should combine all of these into one cohesive, interactive program...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT