Question

I am working on a homework related to fractions. I want to prevent the user to input a number smaller than 1 for denominator("nenner") and numerator("zähler). I tried to implement this with set but it doesn't work. If the numerator/denominator is smaller than 1 I want them to be set to 1. Any tips on the mistake in my code would be appreciated.

namespace Aufgabenblatt2
{
class Bruch
{
    private int zähler;
    public int Zähler
    {
        get { return zähler; }
        set 
        {
            if (value < 1)
            {
                //Console.WriteLine("Keine negativen Brüche und Division durch 0 erlaubt! Wert wurde auf 1 gesetzt");
                zähler = 1;
            }
            else
                zähler = value; 
        }
    }

    private int nenner;
    public int Nenner
    {
        get { return nenner; }
        set
        {
            if (value < 1)
            {
                //Console.WriteLine("Keine negativen Brüche und Division durch 0 erlaubt! Wert wurde auf 1 gesetzt");
                nenner = 1;
            }
            else
                nenner = value;
        }
    }


    public Bruch(int zähler, int nenner)
    {
        this.zähler = zähler;
        this.nenner = nenner;
    }

    /// <summary> 
    /// addiert zum Bruch einen weiteren hinzu 
    /// </summary> 
    /// <param name="summand">Summand</param> 
    /// <returns>Summe</returns> 
    public Bruch Addiere(Bruch summand)
    {
        Bruch ergebnis = new Bruch(this.zähler * summand.nenner + summand.zähler * this.nenner, this.nenner * summand.nenner);
        return ergebnis;
    }

    public override string ToString()
    {
        return String.Format("{0}/{1}", this.zähler, this.nenner);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Console.Write("Eingabe erster Zähler:");
        int zähler1 = Convert.ToInt32(Console.ReadLine());       //Read numerator value from console and save to int zähler
        Console.Write("Eingabe erster Nenner:");
        int nenner1 = Convert.ToInt32(Console.ReadLine());       //Read denominator value from console and save to int nenner

        Bruch b1 = new Bruch(zähler1, nenner1);      


        Console.Write("Eingabe zweiter Zähler:");
        int zähler2 = Convert.ToInt32(Console.ReadLine());       //Read second numerator
        Console.Write("Eingabe zweiter Nenner:");
        int nenner2 = Convert.ToInt32(Console.ReadLine());       //Read second denominator

        Bruch b2 = new Bruch(zähler2, nenner2);      

        Console.WriteLine("{0} + {1} = {2}", b1, b2, b1.Addiere(b2));   //Print both fractions, compute and print sum on Console

        Console.ReadLine();
    }
}

}

Was it helpful?

Solution

You don't use your setters. At Bruch b1 = new Bruch(zähler1, nenner1); you set your fields in the constructor, not the properties.

Change your constructor to set the properties:

public Bruch(int zähler, int nenner)
{
    Zähler = zähler;
    Nenner = nenner;
}

You could've caught this by putting a breakpoint at the setters and see they're never called, then track back through the code to where the values are set.

OTHER TIPS

Your problem is with your constructor:

public Bruch(int zähler, int nenner)
{
    this.zähler = zähler;
    this.nenner = nenner;
}

Your constructor is by-passing the setter by directly setting the field. Change it to:

public Bruch(int zähler, int nenner)
{
    this.Zähler = zähler;
    this.Nenner = nenner;
}

You setter would have been hit if you did this:

 b1.Zähler = 0; 
 Console.Writeline(b1.Zähler.ToString());     // should print 1

But your constructor side-stepped this. This is one of the problems you get into when your property and your backing field only vary by capitalization. They are very easy to mix up. Which is a good argument for prefixing the backing field with something like _, although I'm not personally a fan of that style.

You do not use the setter in the constructor!

public Bruch(int zähler, int nenner)
{
    this.Zähler = zähler; // << Use the setter here! 
    this.Nenner = nenner;
}

On a different topic: It is a much better style to not use "Umlaute" for variable names. Always use english names.

that is because you are setting the wrong parameter. Replace the constructor on Bruch for this one.

public Bruch(int zähler, int nenner)
{
    this.Zähler = zähler;
    this.Nenner = nenner;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top