Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Java class LoopsAndArrays. Your program will contain a method public static void analyzeArray(int[] a) that takes an argument consisting of an array of

Create a Java class LoopsAndArrays. Your program will contain a method public static void analyzeArray(int[] a) that takes an argument consisting of an array of int values and prints the requested output using System.out.println.

An example array is shown below, but your code must work with any array of ints containing at least one positive value. The test program will call analyzeArray with different arrays to test your programs correctness. For example, your method could be called in the following way: int[] b = {13, 2, 20, -32, 3}; analyzeArray(b); You may write and use additional static methods within LoopsAndArrays if you would like, but you can complete the assignment entirely within the analyzeArray method without writing other methods. However, your code should not call any methods built into Java or from any third party to assist your work. The test program will test for this. Instead, you should perform all calculations yourself using basic math operations. You can borrow code we wrote in class. The only methods you may call (aside from ones that you write) are System.out.print and System.out.println. Your program will produce four lines of output in the order shown below. The example lines below are what would be printed for the example array above, but your code should calculate the values in a way that would work for any array of ints stored in a.

First, what is the smallest positive value in the array (greater than 0)? Print a line that looks like this: Smallest positive value: 2

Second, how many prime numbers are contained within the array? Only consider numbers which are 2 or larger to be prime. Print a line that looks like this: Prime count: 3

Third, does the array contain a number which is a square of an integer plus one? In the above example, 12 +1 = 2. Another example of such a number (not in the example array): 92 +1 = 82. Answer the question with yes or no when you print a line that looks like this: Contains a square plus one: yes

Fourth, does the array contain two elements x and y whose product (x times y) is equal to the sum of the entire array? The two elements may have the same value but they should have different indexes. Print out a line that looks like this, but put no after the colon instead of x and y if there is no such x and y according to the above definition. (If there is more than one possibility, just print one of them.) Two elements with product equal to array sum: 2 and 3

Finally, modify the array so that each element becomes the multiplicative product of that element, and the elements immediately before and after it from the original array before modification. If there are no elements before or after, dont include them in the product (a single number is its own product). The example array would become: {26, 520, -1280, -1920, -96};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions

Question

=+ c. a company president deciding whether to open a new factory

Answered: 1 week ago