ISTM 250 Exam 2 — Coding Practical Preparation
1. Create a method that calculates the average of an array of integer quiz scores.
Requirements: Handle an empty array and return a decimal average for nonempty arrays.
Reference solution:
private decimal Average(int[] scores)
{
if (scores == null || scores.Length == 0)
throw new ArgumentException("At least one score is required.");
int sum = 0;
foreach (int score in scores) sum += score;
return (decimal)sum / scores.Length;
}
Rationale: The guard prevents division by zero; casting before division preserves fractional results.
2. Write a safe method that parses a date entered as MM/dd/yyyy.
Requirements: Use DateTime.TryParseExact with an explicit format and invariant culture.
Reference solution:
private bool TryReadDate(string text, out DateTime date)
{
return DateTime.TryParseExact(
text, "MM/dd/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out date);
}
Rationale: Exact parsing avoids ambiguous culture-dependent interpretation.
3. Create a class named Product with a name and unit price, ensuring price cannot be negative.
Requirements: Use private state or a property setter that enforces the invariant.
Reference solution:
public class Product
{
public string Name { get; set; }
private decimal unitPrice;
public decimal UnitPrice
{
get => unitPrice;
set
{
if (value < 0m) throw new ArgumentOutOfRangeException(nameof(value));
unitPrice = value;
}
}
}
Rationale: Encapsulation protects the object's valid state through a controlled property setter.
1. Create a method that calculates the average of an array of integer quiz scores.
Requirements: Handle an empty array and return a decimal average for nonempty arrays.
Reference solution:
private decimal Average(int[] scores)
{
if (scores == null || scores.Length == 0)
throw new ArgumentException("At least one score is required.");
int sum = 0;
foreach (int score in scores) sum += score;
return (decimal)sum / scores.Length;
}
Rationale: The guard prevents division by zero; casting before division preserves fractional results.
2. Write a safe method that parses a date entered as MM/dd/yyyy.
Requirements: Use DateTime.TryParseExact with an explicit format and invariant culture.
Reference solution:
private bool TryReadDate(string text, out DateTime date)
{
return DateTime.TryParseExact(
text, "MM/dd/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out date);
}
Rationale: Exact parsing avoids ambiguous culture-dependent interpretation.
3. Create a class named Product with a name and unit price, ensuring price cannot be negative.
Requirements: Use private state or a property setter that enforces the invariant.
Reference solution:
public class Product
{
public string Name { get; set; }
private decimal unitPrice;
public decimal UnitPrice
{
get => unitPrice;
set
{
if (value < 0m) throw new ArgumentOutOfRangeException(nameof(value));
unitPrice = value;
}
}
}
Rationale: Encapsulation protects the object's valid state through a controlled property setter.