Question
in java

Part 1 In this section, we relook at the main method by examining passing array as parameters. Often we include options/flags
Part 3 Create a Java class (ArrayCompare.java). Declare, create and initialize two integer arrays (i.e., numbers and numbers)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

=================================

part 2)

Output:

hello

hillo

=========================

Explanation :

In java there is only pass by value.There is no pass by reference.

But when coming to the array when we pass the array to the method , its reference to the array is getting copied.

So inside the method if there is any modfications happened to the array then that reference will get affect the original array.

================================

part 3)

// ArrayCompare.java

public class ArrayCompare {

   public static void main(String[] args) {
  
       int numbers1[]={1,2,3,7,11};
       int numbers2[]={1,2,3,7,11};
      
       System.out.println(numbers1==numbers2);
       System.out.println(equals(numbers1,numbers2));      

   }

   private static boolean equals(int[] numbers1, int[] numbers2) {
       if(numbers1.length!=numbers2.length)
       {
           return false;
       }
       else
       {
           for(int i=0;i<numbers1.length;i++)
           {
               if(numbers1[i]!=numbers2[i])
               {
                   return false;
               }
           }
       }
return true;
   }

}


=========================================

Output:

false

true

========================================

Reason:

here we are trying to compare two arrays using == operator.

As the arrays are objects we cant compare them using == operator.Even If we try to compare using == we will get false.

Because only those array references are compared. But actually we have to compare the contents of the array.

So we are creating an equal merthod here to compare two arrays corresponding contents.

=================================Thank You

Add a comment
Know the answer?
Add Answer to:
in java Part 1 In this section, we relook at the main method by examining passing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values...

    Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayTwiceTheNumber(), displayNumberPlusFive(), and displayNumberSquared(). Create each method to perform the task its name implies. 2. Modify the NumbersDemo class to accept the values of the two integers from a user at the keyboard. This is the base code given: public class NumbersDemo { public static void main (String args[]) { // Write your...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • What display is produced by the execution of this JAVA program if we make the following...

    What display is produced by the execution of this JAVA program if we make the following call f(0,0). Please explain your answer public class function {    static void f(int x){        System.out.println(1);    }    static void f(double x){        System.out.println(2);    }    static void f(char x){        System.out.println(3);    }    static void f(long x){        System.out.println(4);    }    public static void main(String[] args) {       } }

  • JAVA Programming Produce a method that reads in a set of values (double) from the user...

    JAVA Programming Produce a method that reads in a set of values (double) from the user and returns them. Use this header: public static double[] getNumsFromUser(String msg1, String msg2) The implementation of the method should start with a message to input the total number of array elements, followed by a message to enter all the array elements. The method returns the array of elements. The two strings msg1 and msg2 that are passed to the method as parameters will be...

  • Java Method Resolution : Consider the following example: What is printed on the console if Main...

    Java Method Resolution : Consider the following example: What is printed on the console if Main is executed, and why? How the codes could be improved? public class Main {     public static void main(String[] args) throws Exception {         A obj = null;         obj.foo();     } } public class A {     public void foo() {         System.out.println(“foo in A”);     } }

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • Java please answer A to I please dont type the answer on paper please use the...

    Java please answer A to I please dont type the answer on paper please use the computer A.   Explain why alpha cannot be accessed by other members of its class. B.   In the program, how can you determine the type of access modifier? C.   Describe the differences and similarities of beta and gamma in program, within the context of access specifiers and class member accessibility. D.   Explain how objects, a and b, are passed to the method. E.    Why...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT