Exercise Sunday

Goal: Continue building out applications using MSTest and the "Red, Green, Refactor" workflow.

Warm Up

  • What is the difference between a property and a field?
  • What is an auto-implemented property?
  • What is the difference between static and instance methods?
  • What is namespacing and why should we use it?


To Do List

Follow along with the homework and create the To Do List app. After writing tests for the backend logic, create a Program class to interact with a user through the console. The app should allow the user to add new items to a to do list. The user should also be able to see a list of the tasks they have already added.

For example, the program could work like this:

  • Program: Welcome to the To Do List.
  • Program: Would you like to add an item to your list or view your list? (Add/View)
  • User: Add
  • Program: Please enter the description for the new item.
  • User: Walk the dog.
  • Program: 'Walk the dog' has been added to your list. Would you like to add an item to your list or view your list? (Add/View)
  • User: View
  • Program: 1. Walk the dog.
  • Program: Would you like to add an item to your list or view your list? (Add/View)

Consider what the program should print if the user asks to view their list before they have added any items to it.

Prime Sifting

Given a number, write a method that returns all of the prime numbers less than that number.

This is a tricky problem and you should use the Sieve of Eratosthenes to solve it. Here's how the Sieve of Eratosthenes works to find a number up to a given number:

  • Create a list of numbers from 2 through n: 2, 3, 4, ..., number.
  • Initially, let prime equal 2, the first prime number.
  • Starting from prime, remove all multiples of prime from the list.
  • Increment prime by 1.
  • When you reach number, all the remaining numbers in the list are primes.

You also might find this video helpful in explaining the Sieve.

Rock, Paper, Scissors

Recreate the game Rock, Paper, Scissors. For anyone who has never played it, here are the rules:

  • It is played by 2 people. Each person chooses "Rock", "Paper", or "Scissors" by forming a shape with their hand.
  • They choose at the same time, so that each person doesn't know what the other person has picked.
  • If one person chooses "Rock" and the other person picks "Scissors" then "Rock" wins. It's a game of combinations:
Rock v. Scissors = Rock wins
Rock v. Paper = Paper wins
Paper v. Scissors = Scissors wins
  • If both people choose the same thing, then it is a draw and nobody wins.

Write a method that can handle the different combinations of plays and make sure to write specs for all possible combination of plays. Your method should take 2 inputs - one for each player's choice. It should return "Player 1" if the first input wins, "Player 2" if the second input wins, and "Draw" if nobody wins.

Start by considering all possible inputs and outputs. Then choose the simplest input/output values and use them to write your first spec. Write all specs in English first, and then translate them into test methods and make them pass, one by one. Don't start coding until you've written your English specs.

Make sure you include a README for this project and create a Git repository for it.

If you get this far, you've done great! Don't worry about getting to the rest of the projects by the end of the day.

Here's optional further exploration to explore:

  • Find a way to get the computer to randomly select a 'play' and make your console app into a one player game. We don't yet know how to test something that's random, so it's okay to skip writing a spec for that part.
  • Or, expand the rules of your game to include 3 players. Make sure to write specs for that before coding. You must understand your goal clearly before you can figure out how to get there.

Weekday Finder

Make an console app that allows users to input a day of the year (like 7/11/2014) and then write a method to find out what day of the week it was, i.e. Monday, Tuesday, etc. Make sure you start with your specs and include a README for this project and create a Git repository for it.

Peer Code Review

  • Did you complete your method by making your tests pass before starting on the console portion of the app?
  • Did you include English specs as a separate text file (or README)? Was it committed before you began your code?
  • Do your specs include specific input and output as well as a descriptive sentence?
  • Do your specs start with the simplest case and progress to more complex cases? Do they cover enough different input values?
  • Have all specs been written up correctly as test methods?
  • Are all tests passing?

Lesson 1 of 12
Last updated more than 3 months ago.