❔ Looking to turn this GUI integer calculator into a Fraction calculator using the provided class

Final project for school, need to create a GUI fraction calculator. I have a GUI integer calculator already built, and a Fraction class already built. Currently stuck on where to even begin to have the GUI calculate fractions vs. integers. GUI Code Attached Fraction Class
namespace FractionCalculatorWithGUI_Final
{
internal class Fraction : IComparable
{
private int numerator = 0;
private int denominator = 1;
public int CompareTo(object rightObject)
{
// typecast the object parameter to a Fraction
Fraction rightFrac = (Fraction)rightObject;

// get both Fractions as (comparable) doubles
double f1 = (double)(this.numerator) / (double)(this.denominator);
double f2 = (double)(rightFrac.numerator) / (double)(rightFrac.denominator);

// compare the 2 doubles
int retVal = 0;
if (f1 < f2)
retVal = -1;
else if (f1 == f2)
retVal = 0;
else
retVal = 1;

return retVal;
}
public override bool Equals(object obj)
{
bool equals = false;
if (obj is Fraction)
equals = (this == (Fraction)obj); // assumes you have operator == overridden
return equals;
}
public static Fraction Parse(String str)
{

Fraction newFraction = new Fraction();

int indexSlash = str.IndexOf("/");
if (indexSlash == -1)
{
throw new ArgumentException("'/' Character not detected. Please input '/' when writing the fraction.");
}
newFraction.numerator = int.Parse(str.Substring(0, indexSlash));
newFraction.denominator = int.Parse(str.Substring(indexSlash + 1));

return newFraction;
}

public static Fraction operator + (Fraction firstFrac, Fraction secondFrac)
{
Fraction sum = new Fraction();
sum.numerator = ((firstFrac.numerator * secondFrac.denominator) + (secondFrac.numerator * firstFrac.denominator));
sum.denominator = firstFrac.denominator * secondFrac.denominator;
return sum;
}
public static Fraction operator - (Fraction firstFrac, Fraction secondFrac)
{
Fraction difference = new Fraction();
difference.numerator = ((firstFrac.numerator * secondFrac.denominator) - (secondFrac.numerator * firstFrac.denominator));
difference.denominator = firstFrac.denominator * secondFrac.denominator;
return difference;
}
public static Fraction operator * (Fraction firstFrac, Fraction secondFrac)
{
Fraction product = new Fraction();
product.numerator = firstFrac.numerator * secondFrac.numerator;
product.denominator = firstFrac.denominator * secondFrac.denominator;
return product;
}
public static Fraction operator / (Fraction firstFrac, Fraction secondFrac)
{
Fraction dividend = new Fraction();
dividend.numerator = firstFrac.numerator * secondFrac.denominator;
dividend.denominator = firstFrac.denominator * secondFrac.numerator;
return dividend;
}



override public String ToString()
{
return numerator.ToString() + "/" + denominator.ToString();
}
}
}
namespace FractionCalculatorWithGUI_Final
{
internal class Fraction : IComparable
{
private int numerator = 0;
private int denominator = 1;
public int CompareTo(object rightObject)
{
// typecast the object parameter to a Fraction
Fraction rightFrac = (Fraction)rightObject;

// get both Fractions as (comparable) doubles
double f1 = (double)(this.numerator) / (double)(this.denominator);
double f2 = (double)(rightFrac.numerator) / (double)(rightFrac.denominator);

// compare the 2 doubles
int retVal = 0;
if (f1 < f2)
retVal = -1;
else if (f1 == f2)
retVal = 0;
else
retVal = 1;

return retVal;
}
public override bool Equals(object obj)
{
bool equals = false;
if (obj is Fraction)
equals = (this == (Fraction)obj); // assumes you have operator == overridden
return equals;
}
public static Fraction Parse(String str)
{

Fraction newFraction = new Fraction();

int indexSlash = str.IndexOf("/");
if (indexSlash == -1)
{
throw new ArgumentException("'/' Character not detected. Please input '/' when writing the fraction.");
}
newFraction.numerator = int.Parse(str.Substring(0, indexSlash));
newFraction.denominator = int.Parse(str.Substring(indexSlash + 1));

return newFraction;
}

public static Fraction operator + (Fraction firstFrac, Fraction secondFrac)
{
Fraction sum = new Fraction();
sum.numerator = ((firstFrac.numerator * secondFrac.denominator) + (secondFrac.numerator * firstFrac.denominator));
sum.denominator = firstFrac.denominator * secondFrac.denominator;
return sum;
}
public static Fraction operator - (Fraction firstFrac, Fraction secondFrac)
{
Fraction difference = new Fraction();
difference.numerator = ((firstFrac.numerator * secondFrac.denominator) - (secondFrac.numerator * firstFrac.denominator));
difference.denominator = firstFrac.denominator * secondFrac.denominator;
return difference;
}
public static Fraction operator * (Fraction firstFrac, Fraction secondFrac)
{
Fraction product = new Fraction();
product.numerator = firstFrac.numerator * secondFrac.numerator;
product.denominator = firstFrac.denominator * secondFrac.denominator;
return product;
}
public static Fraction operator / (Fraction firstFrac, Fraction secondFrac)
{
Fraction dividend = new Fraction();
dividend.numerator = firstFrac.numerator * secondFrac.denominator;
dividend.denominator = firstFrac.denominator * secondFrac.numerator;
return dividend;
}



override public String ToString()
{
return numerator.ToString() + "/" + denominator.ToString();
}
}
}
63 Replies
lycian
lycian2y ago
what are you trying to make it look like?
FatJohnson6
FatJohnson6OP2y ago
this is the current build, and it has to stay in this look per the assignment. I have the slash button in the bottom left to enter the "/" between a numerator and a denominator
lycian
lycian2y ago
so when you do something like 7 / 3 is it just supposed to display like that?
FatJohnson6
FatJohnson6OP2y ago
correct and it does not need to be reduced
lycian
lycian2y ago
I would start with buttonSlash_Click which looks like it's setting the display to just "/" and currentValue to 0, which seems undesirable
FatJohnson6
FatJohnson6OP2y ago
what should I change it to?
lycian
lycian2y ago
currentValue can't just be an int, because now you're working with more complex things. You don't want to reset the value to 0, you want to start tracking a fraction where the current value is the numerator (assuming a user typeing 3/4/5 is the same as (3/4)/5) and then textBoxDisplay likely should be currentValue + "/" That should get you started in the right direction
FatJohnson6
FatJohnson6OP2y ago
so should i change the DISPLAY_MODE to accumulator rather than current_value?
lycian
lycian2y ago
that's up to you. What would accumulator be in that case? it seems to only matter if UpdateDisplay is called
FatJohnson6
FatJohnson6OP2y ago
i got rid of currentValue = 0, but the "/" is still deleted when i try to enter the second number
lycian
lycian2y ago
that makes sense are you using the Fraction class at all yet? and if not, why not?
FatJohnson6
FatJohnson6OP2y ago
no, and i dont know where to implement it. thats really what im stuck on at this point; where do i start using the Fraction class GUI's throw me off incredibly. I've done great with console programs
lycian
lycian2y ago
that's fine, just think of the textbox as what you would right to the console. It's a single string you want to display the difference here is in a console you'd have to parse "3/4" and here you're constructing it. When you the second number, what's happening? NumberKeyHit is getting called correct? so from there, NumberKeyHit is assuming you're taking the currentValue, multiplying by 10, and then adding the number that was hit. With fraction that's an incorrect assumption
FatJohnson6
FatJohnson6OP2y ago
ahhh ok so in NumberHitKey, i should start implementing the Fraction class? like currentValue = Fraction.numerator + "/" + Fraction.denominator?
lycian
lycian2y ago
you already implemented the fraction class. You just have a few ways of solving this. The main point is that NumberKeyHit has to know if you're currently doing a fraction or an integer So you need to store the Fraction, and create it at some point. When do you think you should do that?
FatJohnson6
FatJohnson6OP2y ago
as im clicking the buttons?
lycian
lycian2y ago
which buttons?
FatJohnson6
FatJohnson6OP2y ago
number keys
lycian
lycian2y ago
so number keys needs to check if you already have a fraction, if you need to create one, or if you're working with an integer sounds like you would want to update NumberKeyHit then, because that gets called when a number key gets hit
FatJohnson6
FatJohnson6OP2y ago
ok, would i need to add a new enumeration? something like FRACTION_VALUE or something?
lycian
lycian2y ago
that would work
FatJohnson6
FatJohnson6OP2y ago
and frankly i dont even need this to do integers, just fractions
lycian
lycian2y ago
then currentValue should be a fraction shouldn't it? 🙂
FatJohnson6
FatJohnson6OP2y ago
ok let me work on this for a sec could i remove CURRENT_VALUE and ACCUMULATOR entirely, and change them to numerator and denominator?
lycian
lycian2y ago
you could, yes
FatJohnson6
FatJohnson6OP2y ago
i dont even know how to implement this now haha do you have like an example i can see? like i cannot visualize this
lycian
lycian2y ago
I don't have an example offhand. I was just helping you understand the problem. If you're having trouble with the GUi part, maybe try it with just a console app? Basically each key input would be equivalent to pressing a button. What would you display to the user and how? it's a bit much, but if the GUI is what is confusing you there's no reason to worry about it until you understand how to store the data as it comes in that part is where you seem to be getting stuck
FatJohnson6
FatJohnson6OP2y ago
yeah all we've done for 16 weeks is console applications and now the final is a GUI, with no example on how to implement this fraction class into it
lycian
lycian2y ago
do you have a set of tests that would be used to validate the assignment?
FatJohnson6
FatJohnson6OP2y ago
other than just running it and seeing if it works, i dont think so
lycian
lycian2y ago
do you have to be able to add fractions? Like 3/4 + 1/4?
FatJohnson6
FatJohnson6OP2y ago
yes, the program needs to add, subtract, multiply, and divide fractions i have a console program that does all of it flawlessly
lycian
lycian2y ago
this is what I would do then change accumulator and currentValue to Fraction then the only thing you need to change is: 1. What happens when a user hits a number? 2. What happens when a user hits "/"? 3. Operator likely needs to be unchanged when a user hits a number, is it a valid fraction? You have Fraction.Parse, which throws if it's not when a user hits a "/", you know you need one more number before you have a fraction 1 could be ignored, you just need to append the number onto the textbox string. So instead of calling UpdateDisplay everytime you can just do textBoxDisplay.Text += number 2 would also just add to the textBoxDisplay string and then 3 would parse the current string as a fraction. If it fails to parse, display ERROR that is one of a few ways to solve it. Don't parse the input until you know you're doing an operator and likely simplifies it, if you have a console app already. It's like the user input "3/4" and then "+" and then "1/4" Fraction -> Operator -> Fraction
FatJohnson6
FatJohnson6OP2y ago
alright i changed accumulator and currentvalue to fraction, good so far?
FatJohnson6
FatJohnson6OP2y ago
so now, in NumberKeyHit(), currentvalue should be set to? it would still need to be an integer, since the buttons are integers, right? so i shouldnt change that?
lycian
lycian2y ago
currentValue has to be a Fraction now. You could change so a Fraction doesn't need a denominator, making it an integer
FatJohnson6
FatJohnson6OP2y ago
how would that work?
lycian
lycian2y ago
let me take that back, what do you think should happen when you hit a number key?
FatJohnson6
FatJohnson6OP2y ago
a number should appear if you click "1", 1 should appear in the text box
lycian
lycian2y ago
which is the same as textBoxDisplay.Text += 1 in your program
FatJohnson6
FatJohnson6OP2y ago
so if i change it to this, i get a ton of errors, so i assume thats wrong
FatJohnson6
FatJohnson6OP2y ago
because i have individual methods for each button individually
lycian
lycian2y ago
so then it should be textBoxDisplay.Text += number because you passed that into the method
FatJohnson6
FatJohnson6OP2y ago
ok, now every currentValue object after that method is throwing an error, because they're all set to 0 do i go through and change them all to frac?
lycian
lycian2y ago
if you only need to handle fractions and not integers, then yes
FatJohnson6
FatJohnson6OP2y ago
ok, now none of the buttons work lmao
lycian
lycian2y ago
are they showing "0/1" after you hit them?
FatJohnson6
FatJohnson6OP2y ago
yes
lycian
lycian2y ago
What does UpdateDisplay do to the textbox? it sets to currentValue.ToString() right?
FatJohnson6
FatJohnson6OP2y ago
yes
lycian
lycian2y ago
so you are overwriting your textBox value, which you previously set so maybe don't call UpdateDisplay 🙂
FatJohnson6
FatJohnson6OP2y ago
should i just delete the method all together?
lycian
lycian2y ago
sure you're taking a different approach than that now so it's not as needed
FatJohnson6
FatJohnson6OP2y ago
might make it simpler for me, if its not there i dont think about it haha
lycian
lycian2y ago
UpdateDisplay isn't a magic function, it only sets the textBox.Text, which is a string so if it helps to get rid of it, get rid of it
FatJohnson6
FatJohnson6OP2y ago
hmm ok here's what displays now actually, the arithmetic doesnt even work anymore i think that needs to stay
lycian
lycian2y ago
if it needs to stay, then you have to have some other way of getting the numbers into a fraction the arithmatic is expected to break for now, we're rebuilding the thing from the ground up because I don't know how to help you otherwise. I'm not a teacher 😂. I can help you understand what you're building, or give you the answer, but I can't really help otherwise. Everything in your program is up to you to decide if you want to keep it that way or not
FatJohnson6
FatJohnson6OP2y ago
lol ok i think im just gonna call it for the night, ive been working on this for 8 hours
lycian
lycian2y ago
good luck 🙂
FatJohnson6
FatJohnson6OP2y ago
i appreciate your help friend thanks
Anton
Anton2y ago
I suggest you at least use longs instead of ints. operations on fractions tend to break the int limit also, you lose information by casting to double in the constructor ah no sorry, not the constructor, the comparison function also you should always simplify them if possible use Euclid's algorithm to find gcd
Anton
Anton2y ago
GitHub
uni_csharp/RationalNumber.cs at master · AntonC9018/uni_csharp
C# from console. Contribute to AntonC9018/uni_csharp development by creating an account on GitHub.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server