Exercise Sunday

Goal: Use branching, looping and other foundational C# concepts to build working console applications with C#. Don't worry about trying to complete all the exercises. Focus on getting more comfortable with C# and understanding key concepts.

Warm Up


  • What is a strongly typed language?
  • What is a compiler?
  • What data types are included with C#? How are they different?
  • How is C# different from JavaScript?

Code


Car Dealership

Follow along with the pre-work's lessons and build out your dealership application. Then try adding additional static methods to give the Car class interesting (and possibly quirky) functionality. You can pick from these suggestions or come up with your own:

  • Adding in functionality to lower the cost of the cars by a percentage amount for a sales event.
  • Calculating how well each car would do in the Dakar Rally. You can calculate this however you want, though you may want to add more fields to the Car class.
  • Calculating each car's resale value by year. Same as above, you can calculate this however you want.

Ping-Pong

Ping-pong is a classic programming exercise that utilizes branching and looping. Here's how it works:

  • A user enters a number and the application returns all numbers from 1 to the user input with the following changes:
    • All numbers divisible by 3 are replaced by "ping".
    • All numbers divisible by 5 are replaced by "pong"
    • All numbers divisible by both 3 and 5 are replaced by "ping-pong".

If the user enters 15, the program will return:

1
2
"ping"
4
"pong"
"ping"
7
8
"ping"
"pong"
11
"ping"
13
14
"ping-pong"

Write a console ping-pong application.

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

Riddles of the Sphinx

Create a console application where the Sphinx asks a riddle and the user must answer the riddle correctly. If the riddle is answered correctly, the Sphinx asks a second riddle and so on.

  • Start with one riddle. If the user answers correctly, the Sphinx is defeated. If the user is incorrect, the Sphinx eats the user.
  • Once the application is working with a single riddle, add a few more.
  • Try writing a method to randomize which riddle the Sphinx asks.

Queen Attack

In chess, a queen can move horizontally, vertically, and diagonally, making it the most powerful piece on the board. If another piece is within its line of sight along these three lines, the queen may attack it.

Create a console application where the user can enter X and Y coordinates for both a queen and another chess piece. The program should return whether the queen is capable of 'attacking' the other chess piece.

Hints

  • When the user submits coordinates, the program should create an instance of a Queen class, with properties for its X and Y coordinates.
  • The Queen class should have a method that takes X and a Y coordinate as arguments, and returns true if the queen can attack the given space, and false if it cannot.
  • A message should be displayed to inform the user of the results.

Lesson 4 of 20
Last updated April 14, 2022