CS 145 Lab 5 – Testing and loops
Prelab
- Complete Exercise 3.1 on the Practice-It website before 8 AM on February 20.
Testing
Once you’ve wrapped some code up inside a method, it becomes a candidate for quality assurance. You must poke it and prod it to make sure it does what its specification says it does. But here’s the kicker: you should write your tests before you write the method. Why? Because you naturally think you’re right. If you write your tests before you’ve done any other coding, you’ll approach the task of writing tests with the proper amount of skepticism.
Complete one of problems 1 and 2 and one of problems 3 and 4. Do not implement your methods yet.
- Collision detection in games and simulations is often done by checking if the mouse or some projectile lands within some simple geometric shape (like a circle or rectangle) surrounding the more complicated target. Write only the skeleton of a method named isInCircle that takes the following double parameters: a circle’s center x and y values, its radius, and the mouse’s x and y coordinate. Ultimately, this method will return true if the mouse is inside the circle and false otherwise. For now, have it always return false. In a separate method called testIsInCircle, write at least five tests for this method, pitting the expected value against the actual returned value as we did in lecture. Try to cover all possible setups: inside cases, outside cases, negative coordinates, positive coordinates, etc.
- Suppose you’ve got a bunch of dates of the form “Y/M/D”. You are not guaranteed that the year is four digits, the month is two, or the day is two. Write the skeletons for the three methods: getYear, getMonth, and getDay, all of which return an int and take in a String parameter for the date in the specified format. Have them all return -1 initially. In a separate method called testDateExtraction, write at least two tests for each of these methods, pitting the expected values against the actual returned values. (Heads up: you can use a Scanner to solve this problem pretty easily. Construct one, but instead of using System.in as the parameter, use the date String. And check out the useDelimiter method to change how it breaks up text.)
- Suppose you’ve done some opinion polling and you have a bunch of Strings of the form “++-+—+++-++-“, e.g., a collection of positive and negative marks. Write the skeleton for a method named getPositiveCount that takes in a String parameter and returns the number of plus signs found in it. For now, have it return -1. In a separate method called testGetPositiveCount, write at least four tests, pitting the expected values against the actual ones.
- You’ve running an election and your tallies are of the form “<# of candidates> <# of votes for candidate 1> <# of votes for candidate 2> … <# of votes for candidate n>”. Write the skeleton for a method getMaximum that takes a String of this form as parameter and returns back the maximum number of votes. Initially return -1. In a separate method called testGetMaximum, write at least four test cases, pitting the expected values against the actual ones.
Checkpoint #1: show your instructor or TA your test cases.
Now go back and implement these methods fully and run your test cases. Checkpoint #2: show your instructor or TA your test cases.
TODO
- Homework 1 is due soon. Avoid surprises — check out the grading key on Piazza.