ulrail.gif

Lab Quiz 2

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Slides | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif spacer.gif

Pledged Lab Quiz

This 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

  • To maximize your points, make sure your program compiles successfully.
     
  • If towards the end things are not going as well as you would want (i.e., the program does not compile), it might be best to try commenting out errant statements (i.e., put a // in front of them)
     
  • If you run out of time, submit what you have -- there will be partial credit for this quiz.  Also remember that you can submit a file as many times as you want -- only the most recent submission is graded.  So once you have it working, submit what you have.

Background

A 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 generation

The 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 output

The 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


Other Requirements

You are REQUIRED to include most of the good programming practices discussed in HW J1 (all but the test code and echoing of input).  For your reference, they are included below.  However, you should make sure the rest of the program works before taking care of these issues -- a working program will count for most of the grade.

  • Header: The comments at the very beginning of the file, and need to include your name, e-mail ID, 101 section (or 101-E, if appropriate), and a line or two about what this program does.
     
  • Comments: A line or two before each major "block" (section of code, class, method, variable, etc.) describing what it does.
     
  • Whitespace: A line or two between separate elements of the code (methods, code segments, etc.) and good indentation.
     
  • Legend: The program should print out a line or two that states what the purpose of the program is (i.e. "This is a BMI calculator", or "Are you ready for an Ironman Triathlon? Let's find out!"). This should be the first line(s) printed to the screen.
     
  • Variable names: All final variables should be in all caps with underscores between the words, such as BOX_WIDTH. Non-final variable names should have the firstLetterOfEachWordCaptilized in the variable name (excepting the very first letter). Variable names should be relevant and informative, such as mileage and not m.
     
  • Line length: The lines should not go over about 72 characters. Essentially, when displayed on an 80-character wide screen (what we are using), the line should not go past the end of the screen. You can have a println() statement, for example, use multiple lines (as long as each string starts and ends on the same line). JCreator tells you the column that the cursor is currently in (at the bottom on the status bar), so you can use that to gauge how long your lines are.

Submission

When you are finished, submit your Primes.java file.  You will have to "sign" a pledge on the submission.

Lastly, you should be able to pick up your test at the end of your lab quiz.  If it's not ready, then you can pick it up next week in lab.  Either way, your score will be available on the website by the end of the day tomorrow (you'll receive and e-mail about it).

 

spacer.gif
spacer.gif footer-middle.gif spacer.gif