Question

Create a class named HW that has the main method. Leave the main method empty for...

Create a class named HW that has the main method. Leave the main method empty for now.
• Create a method named allPairs that takes an integer array parameter named a that contains only positive integers and does not return anything. The method should print out all pairs of numbers that have a sum that is prime. If there are no pairs that have a sum that is prime, print out "No pairs". Note that in the first example, (2, 1) is not the same as (1, 2) because there are two 2’s in the array.
Test your allPairs method in the main method using the sample usages.
Sample Method Usage Sample Output

int[] a1 = { 2, 1, 2, 9, 10, 4 };

allPairs(a1); returns (2, 1) (2, 9) (1, 2) (1, 10) (1, 4) (2, 9) (9, 10) (9, 4)

int[] a2 = { 20, 1, 19, 5 };

allPairs(a2);    returns    No pairs

int[] a3 = { 17, 12, 3 };

allPairs(a3);     returns (17, 12)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// HW.java

public class HW {

      // required method

      public static void allPairs(int array[]) {

            // boolean flag denoting if at least one target pair is found

            boolean found = false;

            // looping through each possible unique pair

            for (int i = 0; i < array.length; i++) {

                  for (int j = i + 1; j < array.length; j++) {

                        // finding sum of elements at i and j

                        int sum = array[i] + array[j];

                        // checking if sum is prime

                        if (isPrime(sum)) {

                              // found

                              found = true;

                              // displaying pair

                              System.out.printf("(%d, %d) ", array[i], array[j]);

                        }

                  }

            }

            // at the end, if not found, displaying No pairs

            if (!found) {

                  System.out.println("No pairs");

            } else

                  System.out.println(); // printing line break

      }

      // private helper method implemented to check if a number is prime or not

      // designed to improve neatness of the code

      private static boolean isPrime(int n) {

            if (n <= 1) {

                  // any number less than 2 = not prime

                  return false;

            }

            // looping from 2 to n/2

            for (int i = 2; i <= n / 2; i++) {

                  // if any number divides n evenly, then n is not prime

                  if (n % i == 0) {

                        return false;

                  }

            }

            return true; // prime

      }

      public static void main(String[] args) {

            //testing

            int[] a1 = { 2, 1, 2, 9, 10, 4 };

            allPairs(a1); // displays (2, 1) (2, 9) (1, 2) (1, 10) (1, 4) (2, 9) (9,

                                    // 10) (9, 4)

            int[] a2 = { 20, 1, 19, 5 };

            allPairs(a2); // displays No pairs

            int[] a3 = { 17, 12, 3 };

            allPairs(a3); // displays (17, 12)

      }

}

/*OUTPUT*/

(2, 1) (2, 9) (1, 2) (1, 10) (1, 4) (2, 9) (9, 10) (9, 4)

No pairs

(17, 12)

Add a comment
Know the answer?
Add Answer to:
Create a class named HW that has the main method. Leave the main method empty for...
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
  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

  • In this lab, you will create one class named ArrayFun.java which will contain a main method...

    In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...

  • Last name is Vhora 3. Write a java class named FinalyourLastName, which contains three methods: main,...

    Last name is Vhora 3. Write a java class named FinalyourLastName, which contains three methods: main, arrayMystery, and countTotalOdd. You need to create main method. Inside the main method...you need to create an integer array named myld that consists of each of the digits in your student ID, call arrayMystery method and print out myld, then call count TotaOdd method and print out the total number of odd digits in your ID. (8 points) The arrayMystery method is given as...

  • Part I: Create a program class named TestArrays This class should have a main() method that...

    Part I: Create a program class named TestArrays This class should have a main() method that behaves as follows: Create an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...

  • Create a class named Program10. Your program should have the following 4 methods (other helper methods...

    Create a class named Program10. Your program should have the following 4 methods (other helper methods are allowed, but these four methods must be implemented as specified). You need to define a global scanner object and only use it inside the main method and the getValues method, since other methods do not get any values from the input. getValues: This method does not have any input parameter and returns an array of integers. This method prompts the user with a...

  • Part 1- Method practice Create a main method. In there, make an array of 100 random...

    Part 1- Method practice Create a main method. In there, make an array of 100 random integers(numbers between 0 and 1000). Create a method to find average of the array. Pass the array and return the number(double) that is the average In the main method, call the method created in Step 2. Print out the average Create a method to find the min value of the array. Pass the array and return the int that is the smallest.(pass the value...

  • Animal classes and create instances of them. In the main method do the following: Create a Farm of size 10 Create 5 Animal Objects with the details specified in the table below Add the 5 Animal objec...

    Animal classes and create instances of them. In the main method do the following: Create a Farm of size 10 Create 5 Animal Objects with the details specified in the table below Add the 5 Animal objects to the Farm Call the printDetails method from the Farm to print all the Farm and Animal details. variable name name birthYear weight gender a1 a2 a3 a4 a5 cOw 2012 1000.5 f pig 2009 550.5 m donkey 1999 773.42 m sheep 2016...

  • Your goal is to create an ‘Array’ class that is able to hold multiple integer values....

    Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main(). Assignment 5: overloading member functions. Overview:...

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