How to pass object values to method call

• October 1st, 2012, 12:31 PM
polyglot0727
How to pass object values to method call
To be clear, having trouble with a homework problem. Don't want solution, just clarify some points. Problem and code follows.

I can't wrap my head around how to create the variables for the class methods, incorporate into method return, and then pass the values in the instances to the method call in the program. i.e. Fractions.add(). I can of course figure out the rest of the methods if I could understand the first one. Any clarification would help.

Functionality
Define a class called Fraction that houses a long numerator and long denominator. The constructor
should accept two parameters representing the numerator and denominator respectively. If the caller
passes a denominator of 0, display an error message using Console.WriteLine, wait for the user to hit
ENTER, and exit the program using Environment.Exit.
Your Fraction class needs to implement the following methods:
 Add. Calculates the sum of two fractions and returns the result as a Fraction.
 Subtract. Calculates the difference of two fractions and returns the result as a Fraction. \
 Multiply. Calculates the product of two fractions and returns the result as a Fraction.
 DivideBy. Calculates the quotient of the two fractions and returns the result as a Fraction.
 Reduced. Calculates the reduced version of the fraction, e.g., 5 / 10 reduced is 1 / 2 and 6 / 20
reduced is 3 / 10.

Here is what I have so far.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class Fractions
{
public long Num { get; set; }
public long Denom { get; set; }
public Fractions(long num, long denom)
{
Num = num;
Denom = denom;

}
{
return );

}

}
}

class Program
{
static void Main()
{
Fractions fraction1 = new Fractions(5, 12);
Console.WriteLine("fraction1 Num = {0} Denom = {1}", fraction1.Num, fraction1.Denom);
Fractions fraction2 = new Fractions(6, 12);
Console.WriteLine("fraction2 Num = {0} Denom = {1}", fraction2.Num, fraction2.Denom);

if (fraction1.Denom == 0 || fraction2.Denom == 0)
{
Console.WriteLine(" Error. Denominator Cannot Be 0. Please Press Enter to Exit Program.");
Environment.Exit(1);
}

else
{
Console.WriteLine("fraction1 Num = {0} Denom = {1}", fraction1.Num, fraction1.Denom);
Console.WriteLine("fraction2 Num = {0} Denom = {1}", fraction2.Num, fraction2.Denom);

// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
}

}
}
}

I apologize about the formatting of the code. I read the homework FAQs but didn't quite understand the procedure for retaining formatting.
• October 1st, 2012, 04:29 PM
remojr76
Re: How to pass object values to method call
I'll help point you in the right direction:

1st when you say:
Quote:

"how to create the variables for the class methods, incorporate into method return, and then pass the values in the instances to the method call in the program"
You are not understanding the concept of creating methods that accept parameters and method return types.

Code:

`public long Add()`
is not correct. According to your assignment you have to return a Fraction not a long. Here is a correct method heading for you assignment for the Add method:

Code:

```public Fraction Add(Fraction fractionToAdd) { }```
this method specifies a return type of Fraction passing a parameter of type Fraction to add to your current Fraction object. I will leave the logic of the method body to you.

Hope this helps!