An instance of a class
new Car("1234", "P120", "Audi", "green")
public class Die
{
private static Random rnd = new Random();
public int Sides { get; private set; }
public Die() : this(6) { }
public Die(int sides)
{
if (sides < 4 || sides > 20)
throw new System.Exception("A die can only have from 4 to 20 sides");
this.Sides = sides;
Roll();
}
public int FaceValue { get; private set; }
public void Roll()
{
FaceValue = rnd.Next(1, Sides + 1);
}
}
public int Numerator { get; private set; }
public int Denominator { get; private set; }
public Fraction(int numerator, int denominator)
{
Numerator = numerator;
Denominator = denominator;
}
public Fraction Reciprocal
{
get { return new Fraction(Denominator, Numerator); }
}
public override string ToString()
{
string stringValue = "";
stringValue += Numerator + "/" + Denominator;
return stringValue;
}
public double ToDouble()
{
// The casting of numerator to a double helps
// ensure that we don't lose any fractional
// portion due to integer division.
double value = (double)(Numerator) / Denominator;
return value;
}
void FixSign()
- Ensures any negative sign appears only on the numeratorpublic bool IsProperFraction
- Returns true
if it's a proper fraction*
and /
Operators+
and -
Operatorspublic class Angle
{
public Angle(double degrees)
{
this.Degrees = degrees;
}
public double Degrees { get; set; }
// http://unicode.org/notes/tn28/UTN28-PlainTextMath.pdf
// Page 40 of the above reference for the degree symbol
public override string ToString()
{
return Degrees.ToString() + '\u00B0';
}
}
public double Radians
{
get
{
double radians = Degrees * (Math.PI / 180);
return radians;
}
}
public double Grads
{
get
{
double grads = Radians * (200 / Math.PI);
return grads;
}
}
public string AngleType {...}
Angle Range | Angle Type |
---|---|
< = 0 or > 360 | Undefined |
> 0 and < 90 | Acute |
= 90 | Right |
> 90 and < 180 | Obtuse |
= 180 | Straight |
> 180 and < 360 | Reflex |
= 360 | Full Rotation |
DeckOfCards | PlayingCard | CardSuit | CardValue |
---|---|---|---|
public class PlayingCard
{
public CardSuit Suit { get; private set; }
public CardValue Value { get; private set; }
public PlayingCard(CardValue value, CardSuit suit)
{
Value = value;
Suit = suit;
}
public override string ToString()
{
return $"{Value} {Suit}";
}
}
PlayingCard
)public enum CardSuit
{
HEARTS = 1,
CLUBS,
DIAMONDS,
SPADES
}
public enum CardValue
{
ACE = 1,
DEUCE,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING
}
public class DeckOfCards
{
private static Random Rnd = new Random();
public List<PlayingCard> Cards { get; private set; }
public int Count => Cards.Count;
public bool IsEmpty => Cards.Count == 0;
public DeckOfCards()
{
Cards = new List<PlayingCard>();
var allSuits = System.Enum.GetValues(typeof(PlayingCard.CardSuit));
var allValues = System.Enum.GetValues(typeof(PlayingCard.CardValue));
foreach (PlayingCard.CardSuit suit in allSuits)
foreach (PlayingCard.CardValue value in allValues)
Cards.Add(new PlayingCard(value, suit));
}
}
public class DeckOfCards
{
public PlayingCard DrawCard()
{
PlayingCard card = null;
if (Cards.Count != 0)
{
card = Cards[0];
Cards.Remove(card);
}
return card;
}
public void Shuffle()
{
for (int counter = 0; counter < 100; counter++)
{
int index = Rnd.Next(Cards.Count);
PlayingCard card = Cards[0];
Cards.RemoveAt(0);
Cards.Insert(index, card);
}
}
}
DeckOfCards
etc.DeckOfCards
List<PlayingCard> Deal(int count)