subject

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));

}

}

ansver
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 10:00, michael3592
When is an original work considered public domain? a. when posted via social media b. when it is posted on the internet c. when a copyright symbol is not included with the piece of work d. when explicit permission is given by the author / owner
Answers: 1
image
Computers and Technology, 22.06.2019 17:30, kallee10
The forerunner to cell phones, pdas, and smartphones was
Answers: 1
image
Computers and Technology, 23.06.2019 03:50, nakeytrag
Iam a bacterium. i cause stomach cramps and diarrhea. i am caused by eating rotten foodssuch as chicken, fish, or eggs. sometimes turtles carry my bacteria. what am i?
Answers: 2
image
Computers and Technology, 23.06.2019 07:00, lin550
Why were most movies from the late 1890s until the early 1930s only filmed in black and white? there were only a few people who could afford the technology to produce color motion pictures back then. audiences did not want color motion pictures until later. the film used to make color motion pictures often overheated, which was a safety hazard, so it was generally not allowed. color films had to be hand-colored, frame by frame.
Answers: 3
You know the right answer?
I keep receiving an error for out of bounds index 0. How do I solve this? // read 2 strings from th...

Questions in other subjects:

Konu
Mathematics, 05.06.2020 05:02