ulrail.gif

Lab Quiz 2

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

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

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

spacer.gif

spacer.gif

Pledged and Timed Lab Quiz

This lab quiz is pledged.  You may use the course 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 have 1 hour and 20 minutes to complete this lab quiz.  If you submit your file after this time, you will automatically be given a zero by the grading system.  The TAs will be grading exams, so they may not remember to warn you about this.  There is a clock on the bottom-right of the computer screen, so please budget your time accordingly.

Note that we give you an extra 5 minutes on the quiz (it's normally a 1 hour 15 minute quiz) so that you can spend 30 seconds at the end filling out the questions on the submission survey. 

You will need to submit a single file called LabQuiz2.java.  You may download the skeleton code here: LabQuiz2.java.

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

Note that many parts of this website, such as the lecture notes, will not be visible during the lab quiz.

Advice

  • To maximize your points, make sure your programs compile 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.

Purpose and Output

In this lab quiz, you will finish the development of a program that prints three numerical sequences -- the even numbers, the powers of 2, and the prime numbers.  Sample output is below.  The single input that the user entered is the integer 20, which is colored red.

Welcome to the integer sequence generator!

Please enter an integer:
20

The first 20 even numbers are:
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38

The first 20 powers of 2 are:
2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576

The first 20 prime numbers are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71

Your Task

Your task is to complete the LabQuiz2 class -- in particular, you need to provide full implementations for the three methods that print the numerical sequences shown above.  All three methods take in a single int parameter, and return no value.  Under no circumstances is any more input to be asked of the user!  Doing so will cause your program to not work when we test it, and you will lose lots of points.

The main() method in LabQuiz2.java is (mostly) finished.  You will need to remove the comments in front of the three commented-out lines as you add the appropriate methods.  Other than that, no modifications to the main() method are necessary.  Once the class is fully working, there should be no commented-out Java code inside the main() method (you don't have to add any English comments in the main() method, either).

All methods must be public and staticNot doing so will cause your program to not work!

Note that you are welcome to write other methods, if you want.  No other methods are required, and the lab quiz is fully solvable with just the three methods listed here.

Lastly, the point of writing these methods is to use loops.  Thus, if there is a method in the Java SDK that does the same thing, you cannot use it!  In fact, other than the print()/println() method, your code should not be calling any other Java SDK methods.

printEvenNumbers() method

This method takes in a single int parameter, and returns no value.  This method, like the others, must be both public and static.  It should print the first n even numbers (where n is the parameter).  We'll assume that 0 is the first even number  Thus, if n is 5, then the first 5 even numbers are 0, 2, 4, 6, and 8.  We aren't worried about formatting here -- so if you want to print each of  the numbers on a separate line, that's fine.

printPowersOfTwo() method:

This method takes in a single int parameter, and returns no value.  This method, like the others, must be both public and static.  It should print the first n powers of 2 (where n is the parameter).  We'll assume that 2 is the first power of 2.  Thus, if n is 5, then the first 5 powers of 2 numbers are 2, 4, 8, 16, and 32 -- note that the number is doubling each time.  We aren't worried about formatting here -- so if you want to print each of the numbers on a separate line, that's fine.  You can assume that the powers of 2 that your method should print will never exceed the integer limit (2 billion or so) -- if you enter a value of n greater than 30, you'll run into this problem.  YOU MAY NOT USE THE Math.pow() method (or any other method in the SDK that generates powers of 2)!  This needs to be done with loops, like the other methods.

printPrimeNumbers() method:

This method takes in a single int parameter, and returns no value.  This method, like the others, must be both public and static.  It should print the first n prime numbers.  Note that 1 is not a prime number (really!), which means the first prime number that your program should print is 2.  Thus, if n is 5, then the first 5 prime numbers are 2, 3, 5, 7, and 11.  We aren't worried about formatting here -- so if you want to print each of the numbers on a separate line, that's fine.

This is the hardest method on this lab quiz, and thus we will provide a bit more detail.  A number p is prime if no number from 2 to p-1 will divide it evenly (i.e. there is no remainder when using the % operator).  So 11 is prime, because none of the integers from 2 to 10 divide it evenly; 9 is not prime, because 3 divides it evenly.  If you are having problems getting the method to print the first n prime numbers, you can, for partial credit, print all the prime numbers less than nYOU MAY NOT USE any method in the Java SDK that tests for primality (such as BigInteger.isProbablePrime(), etc.).

Incremental development

  • Test your program incrementally.

  • You should strive to test your program incrementally.

  • Seriously, incremental testing.

  • By incremental testing, we mean that you should get the first method working, and then run it to make sure it works.  Once you are sure the first method works, move onto the second method.  Then onto the third.

  • If you fail to do this, you will have a big pile of broken code that will be impossible for you to debug.

  • Test your program incrementally.

  • We're serious about this.

Other Requirements

You are only required to follow a few of the good programming practices discussed in HW 1.  If you have time, feel free to do the others -- but due to the time constraints of the quiz, you don't have to do them all.  The ones you must do are listed below.

  • Header: The comments at the very beginning of the file, and need to include your name, e-mail ID, and 101 section (or 101-E, if appropriate).  You don't need a purpose for the lab quiz.
     

  • Whitespace: A line or two between separate elements of the code (such as between each of the steps) and good indentation.
     

  • 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 firstLetterOfEachWordCaptalized 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 System.out.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 LabQuiz2.java file.  You will have to "sign" a pledge on the submission.

 

spacer.gif

spacer.gif

footer-middle.gif

spacer.gif