7A

Finish the InputLoop class to read a set of integers. Stop reading input when a non-integer is read. Use this exact prompt: System.out.print("Enter an integer or Q to quit: "); Copy and paste it. That is what I did when I coded the model solution.

Note: you will actually quit on any non-integer.

Do the following things

You will only read the inputs one time and do all the processing as you go. No arrays yet.

The outputs should be on separate lines and in the order given above. If there were no inputs, just print "no input"

Hint: To find the second largest, you will need two variables, max and secondMax. You can not initialize them to 0 since the numbers can be negative, so use a boolean flag first (see Udacity videos), and if first is true (this is the first time through the loop), initialize both max secondMax to the first input. When you read the second number, if it is greater than max, set secondMax to max and the new input to max. If the input is less than max but greater than secondMax, set secondMax to the input.

For the draft, read the numbers and print them, one to a line. Quit on a non-integer. Use the prompt given above.

7A draft:
7A final:

7B

Complete the Power class. It has a constructor that takes a positive integer, base, as a parameter. It has a method getLargestPower(int n) which returns the largest integer that is a power of the base and is less than a specified integer. You are to complete this method.

An example:

Power three = new Power(3); 
int max = three.getLargestPower(27)

Then max will be 9 because 3² is 9 while 3³ is 27 and 27 is not less than 27.

If one can not compute a largest value, return -1 to indicate an error. What if n is 0? What if n is negative? Are there other values for which there is no answer?

Assume the number specified for the base is greater than 0. In actuality, you would want to throw an exception if the base was not positive, but you have not learned how to do that yet.

For the draft, return the correct value when the base is greater than or equal to n. Otherwise return 0 indicating that part has not been coded yet.

NOTE: The javadoc is incorrect for this method. Here is the correct javadoc

    /**
     * Gets the largest number that is a power of the base and also less than n
     * @param n  base^x is largest less than n
     * @return largest number that is a power of the base and also 
     * less than n or else return -1 if the largest number doen not exist
     */

7B draft:
7B final:

7C

You are going to complete the class PrimeGenerator which represents the sequence of prime numbers less than a specified number (the limit).

For example, if limit = 17, the sequence of prime numbers represented by the PrimeGenerator is 2, 3, 5, 7, 11, 13

It has a constructor

It has methods

For example, here is a table showing what is returned for each successive call to nextPrime() when PrimeGenerator is constructed with a limit of 17:

Call number Return value
1 2
2 3
3 5
4 7
5 11
6 13
7 or greater -1
   

 

You can find a lot of information on the Internet about prime numbers in Java. It is okay to use that, but remember that you must site your source if use anyone else's code by putting the URL in a comment. Otherwise it is plagiarism which is an automatic 0.

For the draft, implement isPrime(). You will not need the instance variables for this method. If the number is 1, isPrime returns false. Assume n is positive.

Hint: nextPrime will use isPrime() as a helper method.

Hint: A number is prime if it can not be divided by any number less than its square root. 2 is the only even prime. So you test if the number is divisible by 2 and then test if it is divisible by any odd number. You would check if the number is divisible by 2, 3 5, 7, 9 ... up to square root of the number. To be really efficient you would only divide by primes. You would not need to test 9 because any number divisible by 9 is also divisible by 3. But we are not looking for that level of efficiency

7C draft:
7C final: