Lab 12 – Loops, Loops Everywhere…. (+Unit Tests)

Objective

The purpose of this lab is to:

·         Gain practice writing methods with loops. Please use your notes or the chapter 4 slides.

·         Use while and do-while loops

·         Use for loops

·         A secondary objective is to write code and test it with JUnit test cases. This is actually one of the better ways of testing code, especially when compared to dropping everything into a main() method and testing by hand. The main() method is really the entry point into a program and should be where the programmer is actually defining the execution of the program; adding testing just confuses what might be a complicated method already. Unit tests are designed to test smaller program parts, generally methods, individually. The tests are included in the Dr. Java project – you don’t have to write them yourself.

·         Another objective is to practice working within a given specification. The methods of a class, e.g. ManyLoops.java, may be given to you and, while you may be able to define other methods to aid your work, you will not be able to change specified methods since that would break or require changes in any code relying on your code, which you may not have control over. This is a very common issue in projects made by groups, which are very common in industry.

·         Gain a little exposure to writing methods (outside of main()). Most code does not go in the main() method.

Pitfalls and Rules

·         Do not change the test file, TestManyLoops.java, if you do so accidentally your code may never pass all the tests. If you do so on purpose, which will be obvious, it will be considered unethical (cheating) since you will be attempting to get credit for work you did not do.

·         Only fill in the code in ManyLoops.java where indicated in the lab. Changing a method name or signature (parameters/arguments) will break the test associated with the method.

·         Don’t wait until the end to test your code. As soon as you’re done with a method, click “Test Project”, besides “Run”, saving first, as always.

·         The tests output the function that failed the test, its inputs, your result and the expected result.

·         You can add testing code to the main() if you like. Delete it when done.

·         Printing values in a loop that is "misbehaving" is useful (the testing may supress it, so test in main() ) if the unit tests are not helping.

Steps

1.       Download the project from here and place it somewhere you can find it.

2.       Extract the folder. Right click on it and click “Extract All”. A new Folder should appear.

3.       Open Dr. Java.

4.       In Dr. Java, Click Project -> Open

5.       Navigate to the Lab12 folder in the “Open” window, to wherever you unzipped it, and select the “Lab12.drjava” project. The project will open with two files on the left.

6.       ONLY MAKE CHANGES TO ManyLoops.java file and even then only inside the method definitions unless you are adding testing to the file.

7.       Click “Test Project”, besides “Run”. No tests should pass and five should fail (the color will turn green when the tests have passed. Note that the unit tests are written, as all good unit tests should, to help you figure out what the actual error was (see region indicated by rounded green rectangle):

8.       From Lab04, SackOfFunctions, a brief review of methods, since we have not covered them "officially". Note:

a.       The parameter a is in parentheses after twice, the method name at the top. It is a double value. You can treat it like a variable inside the twice() method. It is not visible outside the method (after its closing curly brace, } ), meaning that it is only accessible between the curly braces, { … }.

b.       Note the return type, double, before the method name, twice. It indicates the type of the value returned, double in this case.

c.       The return statement. The return statement is what actually “returns” or yields the value to the code that calls the method. Many, perhaps most, methods are called to yield some sort of result or otherwise do something and then possibly return other information.

 

9.       Define the smallestPowerOf2GTOE() method. This method returns the smallest power of two that is greater than or equal to the input number.

a.       you must use a while or do-while loop

b.       you may assume num>2

c.       the test outputs some powers of 2

10.   Define the greatestPowerOf2LTOE () method. This method returns the greatest power of two that is less than or equal to the input number.

a.       you must use a for loop

b.       you may assume num>2

c.       the test outputs some powers of 2

11.   Define the DIYDivide() method. This method returns the quotient without any remainder (just like / with integers)

a.       It should return the same result as integer division for positive numbers

b.       Don't worry about efficiency, that's not the point

c.       you may use any loop

d.       you may assume a and b  are positive numbers greater than 0

e.       you cannot use ordinary arithmetic to simply give you the answer, like /, obviously, or a*(1/b).

12.   Define the DIYindexOf() method. This method takes two inputs, a String s and char c, and returns the index of c into s, just like someString.indexOf(), which wouldn't have the first parameter.

a.       It should return -1 if the character isn't in the String.

b.       remember a string is stringIdentifier.length() characters long

c.       character indices start at 0

d.       (not a lab report question but it's worth considering which String methods you've been shown require loops or something similar)

13.   Define the firstNAlphabet() method. This method returns the first n letters of the alphabet, where n is the parameter.

a.       Old programmer's trick, since the characters are in order and somewhat integral:

                                                               i.      (char)(1+'a') will be the character 'b'

                                                             ii.      (char)(2+'a') will be the character 'c'...

b.       You may assume n is ­0-26.

 

14.   Define the goUntilRepeatsOrNothing() method. It should repeatedly query the user until the user enters the same value twice in a row or enters a blank line.

a.       We will test this manually, so run the lab like all of our non-unit test labs.

b.       The main() method already calls the function, so you don't need to add anything. As given, it doesn't do anything.

15.   Make sure you upload your completed ManyLoops.java file (no need to upload anything else). It’s in the Lab12 directory you unzipped at the beginning.

 

Lab Report Questions

1.       Give a high level "sketch" of how you might sort a pile of cards, now that you have some experience with loops. Try to rephrase your Lab00 answer with loops, and maybe variables.

2.       Compare your answer to your Lab00 answer. Notice anything?