Exercise Monday

Goal: Use C# classes to build basic console applications. Use constructors in classes and add custom methods.

Warm Up


  • What is C#? What is .NET 5.0?
  • How are classes declared? Why should we use custom classes?
  • What is a constructor? How do they help DRY up code?
  • Why is the 'red-green-refactor' method so important to test-driven development?
  • Discuss the benefits of TDD with your partner.
  • Why do we want our tests to fail before they pass?

Code


Triangle Tracker

Create a Triangle Tracker console application similar to the Triangle Tracker website we made in Intro.

  • Make a console app with a Triangle class.

  • Use Console.ReadLine() to gather three numbers from the user.

  • Create a method in the Triangle class that takes these 3 numbers and returns what type of triangle (if any) they form:

    • Equilateral: All sides are equal;
    • Isosceles: Exactly 2 sides are equal;
    • Scalene: No sides are equal.
  • Not all combinations make valid triangles. If any side is longer than the other two sides combined, the lengths cannot form a triangle. For example, 2, 2, and 8 does not make a triangle. Make sure to account for this.

  • Write plain English spec descriptions first, including both an input and desired output for each test.

Palindromes

A palindrome is any word, phrase, number, or other sequence of characters that reads the same both backward or forward.

Create a console application that includes a method to identify whether a word is a palindrome. It should return true if the user-provided word is a palindrome and false if it is not.

Here are several recommended approaches:

  • Simpler: There is a method Array.Reverse(). Check it out in the REPL to see what it does.

  • More Challenging: Create the method without using the Array.Reverse() method. If the simpler way of doing this isn't a challenge, we recommend trying this out!

  • Bonus Points: Make a method that checks a string of words and also an integer. For example: "Hello olleH" is a palindrome by our definition. An integer palindrome would be: 101.

Allergies

Here's something a bit trickier and please only attempt this if you have completed the previous projects and had them checked by an instructor. An allergy score is a single number that tells what someone is allergic to. The scores for each allergen are:

allergen     score
eggs         1
peanuts      2
shellfish    4
strawberries 8
tomatoes     16
chocolate    32
pollen       64
cats         128

So if someone is allergic to eggs and strawberries, they get a score of 9.

Build a website that uses a method that is called on someone's score - i.e. the score is the object, and returns an array listing what they're allergic to. For example, running allergies(3) should print a list of the person's allergies (eggs and peanuts) to the screen.

Peer Code Review


  • Do projects include custom classes, constructors and methods?
  • Could code be DRYer or more concise?
  • Are all possible outcomes considered when branching? For instance, if the console application eliminates all possible outcomes in the high/low game, how does it respond? For instance, the app could say "there are no numbers that fit these constraints" if this is the case.

Lesson 10 of 20
Last updated April 14, 2022