Pledged Lab QuizThis lab quiz is pledged. You may only use your textbook, and NOTHING ELSE. This means you cannot use any files in your home directory, previous assignments, slides, etc. However, you should still save your file into your home directory. You will need to submit a single file called Primes.java. We are providing the skeleton code. The TAs cannot help you with Java-related issues on this quiz. If you are having problems getting JCreator to work, can't log in to the machines, etc., then you are welcome to ask the TAs for assistance. If you do not understand what the lab is asking, the TAs may be able to help you (meaning if it's a problem with our explanation, then they can help you -- if you've been skipping the lectures so far and have no idea what any of this means, you are on your own). Advice
BackgroundA prime number is a number that is only divisible by itself and 1. For example, the first few prime numbers are 2, 3, 5, 7, 11, 13, etc. A composite number is a number that is not prime -- meaning there are divisors other than 1 and the number. For example, the first few composite numbers are 4 (divisible by 1, 2, and 4), 6 (divisible by 1, 2, 3, and 6), 9 (divisible by 1, 3, and 9), etc. Prime numbers only deal with positive integers. Note that 1 is neither prime nor composite -- it is in its own category. Thus, the first prime number is 2. If you are confused about prime / composite numbers, you may ask a TA to explain them to you. Prime number generationThe program you need to submit will generate a list of prime and composite numbers up to (and including!) a user specified value. The program will first read in a value n from the keyboard. For each positive integer up to (and including!) n, it will print out if that number is composite or if the number is prime. As 1 is not a prime number, you should start at 2. In order to find out if a number x is prime, you divide it by all integers less than x (and greater than 1) -- i.e. from all integers from 2 to x-1. If any of them divide it evenly, then the number is not prime. This will probably require a nested loop structure. The numbers you will check to see if they are prime will range from 2 to n (the outer loop), and for each of those numbers, you need to try to divide it by all integers from 2 to n-1 (the inner loop). Remember the modulus operator to see if one number divides another one with a remainder of zero. Sample outputThe text in red is what was input by the user. Prime number listing Enter max number: 20 2 prime 3 prime 4 composite 5 prime 6 composite 7 prime 8 composite 9 composite 10 composite 11 prime 12 composite 13 prime 14 composite 15 composite 16 composite 17 prime 18 composite 19 prime 20 composite
|