Computers and Technology

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

}

}

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 06:00, kamilahmcneil3969
What are the most likely causes of conflict at the meeting? check all that apply.
Answers: 1
image
Computers and Technology, 22.06.2019 14:30, chaparro0512
Create a pseudocode design to prompt a student for their student id and the titles of the three classes they want to add. the solution should display the student’s id and a total bill. • bill a student using the following rules: o students can only add up to 3 classes at a time.
Answers: 3
image
Computers and Technology, 24.06.2019 10:00, lashaunahard
Each time you save a document, you will need to type in the file type in which it should be saved you can select the save button to save it with the same file name if it has been previously saved you will need to select the location to save the file you will need to use the save as dialog box
Answers: 1
image
Computers and Technology, 24.06.2019 14:00, superstarsara5ouh83x
Fast answer i need for apex ! smartphones should be banned from the classroom in public schools so that students cannot cheat on test so easily? which is an example of a counter argument to the thesis above? a. the classroom is a place for learning not for making phone calls b. smartphones are useful learning tools in the modern classroom c. banning smartphones will not students pay attention to teachers any better d. banning smartphones would decreased incidents of theft
Answers: 2
Do you know the correct 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, 01.02.2022 14:00
Konu
Mathematics, 01.02.2022 14:00
Konu
Mathematics, 01.02.2022 14:00