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 convert This java Code to C# (.cs) Please make sure the code can run and...
Please convert This java Code to C# (.cs) Please make sure the code can run and show the output Thank you! Let me know if you need more information. Intructions For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods...
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...
Please write code in java and comment. Thanks. I would appreciate that. Fitness Task: public interface...
Please write code in java and comment. Thanks. I would appreciate that. Fitness Task: public interface Fitness public Muscle [ ] muscleTargeted() A method that returns the muscle that is going to be affected by the fitness. Note that the return type of the method is Muscle. A human body has a finite number of muscle parts that can be affected by fitness exercises. Define an enum datatype called Muscle with the follwoing member values Abs,Back,Biceps,Chest,Arms,Glutes,Shoulders,Triceps,Legs,Cardio public String description() a...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
Please do in java with code available for copy and with comments so I can follow...
Please do in java with code available for copy and with comments so I can follow along :)\ Develop a program that prints out the sum of each column of a two-dimensional array. The program defines method sumColumn() takes a two-dimensional array of integers and returns a single-dimensional array that stores the sum of columns of the passed array. The program main method prompts the user to enter a 3-by-4 array, prints out the array, and then calls method sumColumns()....
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();    }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT