Computers and Technology, 08.12.2020 02:30 brenda0014
I keep receiving an error for out of bounds index 0. How do I solve this?
// read 2 strings from the command line; each string will contain an integer
// let's call those integers, num1 and num2
// write a loop that goes from num1 and num2 looking for prime numbers
// if a number if prime then store it in an array of integers
// pass this array to a method that returns the sum of all the numbers in the array; you will also need to tell this method how many numbers are in the array
// output only the sum of primes
// you may assume a maximum of not more than 1000 numbers in the array
import java. util.*;
public class Program {
// go thru the array primes which has size elements
// total the integer values in primes
// return this total
// the first argument is the array of prime numbers
// the second argument is the number of primes in this array
public static int sumArray(int[] primes, int n) {
int total = 0;
for (int i = 0; i < n; i++) {
total += primes[i];
}
return total;
}
// this method takes an integer, n, and determines if it is prime
// returns true if it is and false if it isn't
// a number is prime if it is only evenly divisible by 1 and itself
// 1 is NOT considered prime
static boolean isPrime(int n) {
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
public static void main(String[] args) {
// define the constant MAX which represents the largest size of the primes array
final int MAX = 1000;
int num1, num2, numPrime = 0;
int[] primes = new int[MAX];
// convert 1st command line argument from a string to an integer
num1 = Integer. parseInt(args[0]);
num2 = Integer. parseInt(args[1]);
// get num2 from the command line
// loop through all integers from num1 to num2
// store those that are prime in the primes array
// keep track of the number of primes - numPrime
for (int i = num1; i <= num2; i++) {
if (isPrime(i)) {
primes[numPrime++] = i;
}
}
// output the sum of all the primes
System. out. println(sumArray(primes, numPrime));
}
}
Answers: 1
Computers and Technology, 22.06.2019 17:00
What allows you to create a wireless connection among your smart devices
Answers: 2
Computers and Technology, 22.06.2019 19:10
How might the success of your campaign be affected if you haven’t carefully completed all field data or if you accidentally insert the wrong merge field in the document?
Answers: 1
Computers and Technology, 23.06.2019 20:00
How much current flows through the alternator brushes? a. 2–5 a b. 25–35 a, depending on the vehicle c. 5–10 a d. 10–15 a
Answers: 2
Computers and Technology, 24.06.2019 07:30
Aproject involves many computing systems working together on disjointed task towards a single goal what form of computing would the project be using
Answers: 3
I keep receiving an error for out of bounds index 0. How do I solve this?
// read 2 strings from th...
Mathematics, 04.02.2022 21:00
History, 04.02.2022 21:00
Mathematics, 04.02.2022 21:00
Mathematics, 04.02.2022 21:00
English, 04.02.2022 21:00
Mathematics, 04.02.2022 21:10
Physics, 04.02.2022 21:10
Biology, 04.02.2022 21:10
Mathematics, 04.02.2022 21:10
Chemistry, 04.02.2022 21:10
Chemistry, 04.02.2022 21:10