Pledged and Timed Lab QuizThis 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
Purpose and OutputIn 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 TaskYour 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 static! Not 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() methodThis 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 n. YOU MAY NOT USE any method in the Java SDK that tests for primality (such as BigInteger.isProbablePrime(), etc.). Incremental development
Other RequirementsYou 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.
SubmissionWhen you are finished, submit your LabQuiz2.java file. You will have to "sign" a pledge on the submission.
|