Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions week03/Fractions/Fraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@ public class Fraction
private int _top;
private int _bottom;

public Fraction()
public int GetBottom()
{
// Default to 1/1
_top = 1;
_bottom = 1;
return _bottom;
}

public Fraction(int wholeNumber)
public int GetTop()
{
_top = wholeNumber;
_bottom = 1;
return _top;
}

public Fraction(int top, int bottom)
public void SetBottom(int bottom)
{
_top = top;
if (bottom <= 0)
{
Console.WriteLine("ERROR - Bottom cannot be less than or equal to zero.");
Environment.Exit(1); // Exit the program if bottom is zero to prevent division by zero errors.
}
_bottom = bottom;
return;
}

public void SetTop(int top)
{
_top = top;
return;
}
public string GetFractionString()
{
// Notice that this is not stored as a member variable.
Expand Down
14 changes: 11 additions & 3 deletions week03/Fractions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ class Program
static void Main(string[] args)
{
Fraction f1 = new Fraction();
f1.SetBottom(5);
f1.SetTop(1);
Console.WriteLine(f1.GetFractionString());
Console.WriteLine(f1.GetDecimalValue());

Fraction f2 = new Fraction(5);
Fraction f2 = new Fraction();
f2.SetBottom(3);
f2.SetTop(2);
Console.WriteLine(f2.GetFractionString());
Console.WriteLine(f2.GetDecimalValue());

Fraction f3 = new Fraction(3, 4);
Fraction f3 = new Fraction();
f3.SetBottom(1);
f3.SetTop(0);
Console.WriteLine(f3.GetFractionString());
Console.WriteLine(f3.GetDecimalValue());

Fraction f4 = new Fraction(1, 3);
Fraction f4 = new Fraction();
f4.SetBottom(0); // This will cause an error because the bottom cannot be zero, and the program will exit.
f4.SetTop(3);
Console.WriteLine(f4.GetFractionString());
Console.WriteLine(f4.GetDecimalValue());
}
Expand Down