Link to home
Start Free TrialLog in
Avatar of kensy11
kensy11Flag for Belgium

asked on

C# method

Hello,

i can't get the following code to work.

1) it ask for a number above 0
2) ask for a number above the the first number


using System;

namespace ConsAppl1TI19 {
  class ConsCode {
    public void ExecuteProgram() {
      
      ClsVerglijk verglijk = new ClsVerglijk();
      ClsDelen del = new ClsDelen();
      begin bg = new begin();
      
      

      Console.Write("Geef de ondergrens in a.u.b ... ");
      int onder = int.Parse(Console.ReadLine());
      bg.beginnen(onder);


      Console.Write("Geef de bovengrens in a.u.b ... ");
      int boven = int.Parse(Console.ReadLine());
      bg.beginnen(boven);


    }/*ExecuteProgram*/
  }/*ConsCode*/
}/*ConsAppl1TI19*/

Open in new window


using System;


namespace ConsAppl1TI19 {
  class begin {

    public void beginnen(int onder ) {

      
      int ondergrens = onder;
      int bovengrens;
      while (ondergrens <= 0) {

        Console.WriteLine("!!! Fout: de ondergrens moet groter dan of glijk aan 1 zijn !!!");
        Console.Write("Geef de ondergrens in a.u.b ... ");
        ondergrens = int.Parse(Console.ReadLine());

      }


      while (bovengrens < ondergrens) {

        Console.WriteLine("!!! Fout: het getal moet grooter of gelijk zijn dan ondergresns ({0}) ", ondergrens);
        Console.Write("Geef de bovengrens in a.u.b .. ");
        bovengrens = int.Parse(Console.ReadLine());

      }
        
    
    }


  }
}

Open in new window



thank you
Avatar of Eric Flamm
Eric Flamm
Flag of United States of America image

Do you have the code for the 2 referenced classes? ClsVerglijk,   ClsDelen
Avatar of kensy11

ASKER

yes, but i have no problem with those. you can ignore them
The problem starts at the line 18  where i  read the number and then i want to check if the given number is bigger then the first number.

Avatar of Vel Eous
Vel Eous

One of your problems is that lines 15 and 20 of your ConsCode class overwrite the same variable in beginnen, then the variable bovengrens which you are comparing to ondergrens in your beginnen method is never being initialised.  You declare it, but never give it a value.

Take a look at the attached code snippet and see if that works for you.
using System;

namespace EE.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsCode c = new ConsCode();
            c.ExecuteProgram();
        }
    }

    class ConsCode
    {
        public void ExecuteProgram()
        {

            //ClsVerglijk verglijk = new ClsVerglijk();
            //ClsDelen del = new ClsDelen();
            Begin bg = new Begin();

            Console.Write("Geef de ondergrens in a.u.b ... ");
            int _onder = 0;
            while (_onder <= 0)
            {
                Console.WriteLine("!!! Fout: de ondergrens moet groter dan of glijk aan 1 zijn !!!");
                Console.WriteLine("Geef de ondergrens in a.u.b ... ");
                _onder = int.Parse(Console.ReadLine());
            }
            bg.Onder = _onder;


            Console.Write("Geef de bovengrens in a.u.b ... ");
            int _boven = 0;
            _boven = int.Parse(Console.ReadLine());
            bg.Boven = _boven;

            if (bg.Compare() == false)
            {
                Console.WriteLine("!!! Fout: het getal moet grooter of gelijk zijn dan ondergresns ({0}) ", _onder);
            }
            else
            {
                Console.WriteLine("Einige Erfolgsmeldung");
            }

            Console.ReadLine();

            //Console.Write("Geef de ondergrens in a.u.b ... ");
            //int onder = int.Parse(Console.ReadLine());

            // this next line initially writes one value to ondergrens

            //bg.beginnen(onder);


            //Console.Write("Geef de bovengrens in a.u.b ... ");
            //int boven = int.Parse(Console.ReadLine());

            // this next line overwrites writes that pervious value to ondergrens
            // so bovengrens is never being initialised

            //bg.beginnen(boven);


        }
    }

    class Begin
    {
        private int _onder = 0;
        private int _boven = 0;

        public Begin()
        {
        }

        public int Onder
        {
            get
            {
                return this._onder;
            }
            set
            {
                this._onder = value;
            }
        }

        public int Boven
        {
            get
            {
                return this._boven;
            }
            set
            {
                this._boven = value;
            }
        }

        public bool Compare()
        {
            if (this._boven < this._onder)
            {
                return false;
            }
            return true;
        }

        // when you were calling this you are overwriting the same variable in the method; ondergrens
        // bovengrens is never initialised or used, the passed argument is always assigned to ondergrens
        //public void beginnen(int onder)
        //{
        //    int ondergrens = onder;
        //    int bovengrens;
        //    while (ondergrens <= 0)
        //    {
        //        Console.WriteLine("!!! Fout: de ondergrens moet groter dan of glijk aan 1 zijn !!!");
        //        Console.Write("Geef de ondergrens in a.u.b ... ");
        //        ondergrens = int.Parse(Console.ReadLine());
        //    }
        //    while (bovengrens < ondergrens)
        //    {
        //        Console.WriteLine("!!! Fout: het getal moet grooter of gelijk zijn dan ondergresns ({0}) ", ondergrens);
        //        Console.Write("Geef de bovengrens in a.u.b .. ");
        //        bovengrens = int.Parse(Console.ReadLine());

        //    }
        //}
    }
}

Open in new window

Avatar of kensy11

ASKER

i have started from scratch . here is my code , please tell me if i did something wrong

using System;

namespace ConsAppl1TI23 {
  class Program {
    static void Main(string[] args) {
      ConsCode Cc = new ConsCode();
      Cc.ExecuteProgram();
      Console.ReadKey(true);
    }/*Main*/
  }/*Program*/
}/*ConsAppl1TI23*/

Open in new window


using System;

namespace ConsAppl1TI23 {
  class ConsCode {
    public void ExecuteProgram() {
      vergelijking vrg = new vergelijking();


      Console.Write("Geef de ondergrens in aub ... ");
      int ondergrens = vrg.ondergrens();

      Console.Write("Geef de bovengrens in aub ... ");
      int bovengrens = vrg.bovengrens(ondergrens);

      Console.WriteLine("\nHet grootste aantal palindroomdelers voor een getal in het interval:[{0}, {1}] ", ondergrens, bovengrens);


    }/*ExecuteProgram*/
  }/*ConsCode*/
}/*ConsAppl1TI23*/

Open in new window


using System;

namespace ConsAppl1TI23 {
  class vergelijking {
      
    public int ondergrens (){

      int ondg = int.Parse(Console.ReadLine());

      while (ondg <= 0) {

        Console.WriteLine("!!! Fout: de ondergrens moet groter dan of glijk aan 1 zijn !!!");
        Console.Write("Geef de ondergrens in a.u.b ... ");
        ondg = int.Parse(Console.ReadLine());

      }
      return ondg;
  
  }
    
    public int bovengrens(int ondg) {

      int bovg = int.Parse(Console.ReadLine());

      while (bovg < ondg) {

        Console.WriteLine("!!! Fout: het getal moet grooter of gelijk zijn dan ondergresns ({0}) ", ondg);
        Console.Write("Geef de bovengrens in a.u.b .. ");
        bovg = int.Parse(Console.ReadLine());

      }

      return bovg;
    
    
    
    }
  }/*vergelijking*/
}/*ConsAppl1TI23*/

Open in new window

It all appears to function correctly as it should.  

I would say however that you don't want to be mixing your input/output with your logic.
Avatar of kensy11

ASKER

Here is my final program i add some more features , it works but i dont know if i did it the right way .

for example on my Berekening.cs class im sure i can do this differently but i dont know how. please help me

i have upload entire project.
ConsAppl1TI23.zip
Avatar of kensy11

ASKER

ooh where did it mix it ??
In your vergelijking class you have some console output, it is not the end of the world but it is not a habit you want to get into.

Both of the methods in your Berekening class appear to be doing the same calculation and are therefore duplicating functionality, you might want to consider just having the single method.

I would also suggest you consolidate your methods, all your classes are working with your numbers and could legitimately be in a single class named Berekening.

Let us know if you want further assistance with any of this.

Avatar of kensy11

ASKER

thanks for the reply ,   about my Berekening class, yes its the same calculation but the first returns 'antwoord' and the seconde returns max  .

how can i get them in one method ? it only can return one number right ?
"it only can return one number right ?"

Correct, a method can only return a single data type, however, it has no memory of what it performed before.  So a single method can be used to do the job of two methods, if the body of the methods are both identical (identical in the programming they perform, variable naming has no effect).

Take a look at the code snippet, I have combined your classes which all perform maths calculations into a single class as they all deal with the same data.  This is referred to as encapsulation.

I have also combined your resultaat and maximum methods into a single DoBerekening method.

Hopefully it all makes sense, if not though, always ask.
using System;

namespace ConsAppl1TI23
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsCode Cc = new ConsCode();
            Cc.ExecuteProgram();
            Console.ReadKey(true);
        }
    }

    class ConsCode
    {
        public void ExecuteProgram()
        {
            Berekening1 brk = new Berekening1();

            // personally I would test your input the following way ...

            Console.Write("Geef de ondergrens in aub ... ");
            brk.Ondergrens = uint.Parse(Console.ReadLine());
            while (brk.Ondergrens <= 0)
            {
                Console.WriteLine("!!! Fout: de ondergrens moet groter dan of glijk aan 1 zijn !!!");
                Console.Write("Geef de ondergrens in a.u.b ... ");
                brk.Ondergrens = uint.Parse(Console.ReadLine());
            }
            Console.Write("Geef de bovengrens in aub ... ");
            brk.Bovengrens = uint.Parse(Console.ReadLine());
            while (brk.Bovengrens < brk.Ondergrens)
            {
                Console.WriteLine("!!! Fout: het getal moet grooter of gelijk zijn dan ondergresns ({0}) ", brk.Ondergrens);
                Console.Write("Geef de bovengrens in a.u.b .. ");
                brk.Bovengrens = uint.Parse(Console.ReadLine());
            }

            // if you would prefer to use a method to compare ondergrens and bovengrens then you could do the following

            //Console.Write("Geef de ondergrens in aub ... ");
            //brk.Ondergrens = uint.Parse(Console.ReadLine());
            //Console.Write("Geef de bovengrens in aub ... ");
            //brk.Bovengrens = uint.Parse(Console.ReadLine());
            //while (brk.Vergelijking(brk.Ondergrens, brk.Bovengrens) == false)
            //{
            //    Console.WriteLine("!!! Fout: het getal moet grooter of gelijk zijn dan ondergresns ({0}) ", brk.Ondergrens);
            //    Console.Write("Geef de bovengrens in a.u.b .. ");
            //    brk.Bovengrens = uint.Parse(Console.ReadLine());
            //}

            Console.WriteLine("\nHet grootste aantal palindroomdelers voor een getal in het interval:[{0}, {1}] ", brk.Ondergrens, brk.Bovengrens);
            Console.WriteLine("\n bedraagt {0}  ", brk.DoBerekening(brk.Ondergrens, brk.Bovengrens));
            Console.WriteLine("\nen wordt gevonden voor het getal {0}", brk.DoBerekening(brk.Ondergrens, brk.Bovengrens));

        }
    }

    // all your code that performs maths calculations is in the one class
    // keeping related code together in such a manner is called encapsulation
    public class Berekening1
    {
        // private properties of your class
        private uint _ondergrens;
        private uint _bovengrens;

        // constructor
        public Berekening1()
        {
        }

        // field public accessor
        public uint Ondergrens
        {
            get
            {
                return this._ondergrens;
            }
            set
            {
                this._ondergrens = value;
            }
        }

        // field public accessor
        public uint Bovengrens
        {
            get
            {
                return this._bovengrens;
            }
            set
            {
                this._bovengrens = value;
            }
        }

        public bool Vergelijking(uint ondergrens, uint bovengrens)
        {
            if (ondergrens > bovengrens)
            {
                return false;
            }
            return true;
        }

        public uint DoBerekening(uint ondergrens, uint bovengrens)
        {
            uint antwoord = 0;
            ushort totaal = 0;
            ushort max = 0;
            for (uint teller = ondergrens; teller <= bovengrens; teller++)
            {
                for (uint i = 1; i != teller; i++)
                {
                    if (teller % i == 0 && Palindroom(i))
                    {
                        totaal++;
                        if (totaal > max)
                        {
                            max = totaal;
                            antwoord = teller;
                        }
                    }
                }
                totaal = 0;
            }
            return antwoord;
        }

        public bool Palindroom(uint getal)
        {
            uint kopie = getal;
            uint vergelijk = 0;
            uint temp = 0;
            while (getal > 0)
            {
                temp = getal % 10;
                vergelijk = vergelijk * 10 + temp;
                getal = getal / 10;
            }
            if (vergelijk == kopie)
            {
                return true;
            }
            // you don't need an else clause here
            // if the above if condition is true, this return statement will never be run
            return false;
        }
    }
}

Open in new window

Avatar of kensy11

ASKER

thanks , but i get a wrong outcome :

the 'bedraag' shuld be 14

now i get two times 792
01.jpg
I apologise.

Your resultaat and maximum functions did the same computation, but returned different values which I didn't notice initially.

I have attached some amended code for you to look at.  I altered the DoBerekening to take a third parameter of type string, which is used to determine if you want the number of denominators, or the maximum value.

Let me know if that works better for you.
using System;

namespace ConsAppl1TI23
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsCode Cc = new ConsCode();
            Cc.ExecuteProgram();
            Console.ReadKey(true);
        }
    }

    class ConsCode
    {
        public void ExecuteProgram()
        {
            Berekening1 brk = new Berekening1();

            // personally I would test your input the following way ...

            Console.Write("Geef de ondergrens in aub ... ");
            brk.Ondergrens = uint.Parse(Console.ReadLine());
            while (brk.Ondergrens <= 0)
            {
                Console.WriteLine("!!! Fout: de ondergrens moet groter dan of glijk aan 1 zijn !!!");
                Console.Write("Geef de ondergrens in a.u.b ... ");
                brk.Ondergrens = uint.Parse(Console.ReadLine());
            }
            Console.Write("Geef de bovengrens in aub ... ");
            brk.Bovengrens = uint.Parse(Console.ReadLine());
            while (brk.Bovengrens < brk.Ondergrens)
            {
                Console.WriteLine("!!! Fout: het getal moet grooter of gelijk zijn dan ondergresns ({0}) ", brk.Ondergrens);
                Console.Write("Geef de bovengrens in a.u.b .. ");
                brk.Bovengrens = uint.Parse(Console.ReadLine());
            }

            // if you would prefer to use a method to compare ondergrens and bovengrens then you could do the following

            //Console.Write("Geef de ondergrens in aub ... ");
            //brk.Ondergrens = uint.Parse(Console.ReadLine());
            //Console.Write("Geef de bovengrens in aub ... ");
            //brk.Bovengrens = uint.Parse(Console.ReadLine());
            //while (brk.Vergelijking(brk.Ondergrens, brk.Bovengrens) == false)
            //{
            //    Console.WriteLine("!!! Fout: het getal moet grooter of gelijk zijn dan ondergresns ({0}) ", brk.Ondergrens);
            //    Console.Write("Geef de bovengrens in a.u.b .. ");
            //    brk.Bovengrens = uint.Parse(Console.ReadLine());
            //}

            Console.WriteLine("\nHet grootste aantal palindroomdelers voor een getal in het interval:[{0}, {1}] ", brk.Ondergrens, brk.Bovengrens);
            Console.WriteLine("\n bedraagt {0}  ", brk.DoBerekening(brk.Ondergrens, brk.Bovengrens, "palindroomdelers"));
            Console.WriteLine("\nen wordt gevonden voor het getal {0}", brk.DoBerekening(brk.Ondergrens, brk.Bovengrens, "getal"));

        }
    }

    // all your code that performs maths calculations is in the one class
    // keeping related code together in such a manner is called encapsulation
    public class Berekening1
    {
        // private properties of your class
        private uint _ondergrens;
        private uint _bovengrens;

        // constructor
        public Berekening1()
        {
        }

        // field public accessor
        public uint Ondergrens
        {
            get
            {
                return this._ondergrens;
            }
            set
            {
                this._ondergrens = value;
            }
        }

        // field public accessor
        public uint Bovengrens
        {
            get
            {
                return this._bovengrens;
            }
            set
            {
                this._bovengrens = value;
            }
        }

        public bool Vergelijking(uint ondergrens, uint bovengrens)
        {
            if (ondergrens > bovengrens)
            {
                return false;
            }
            return true;
        }

        public uint DoBerekening(uint ondergrens, uint bovengrens, string maxOrDenominator)
        {
            uint antwoord = 0;
            uint totaal = 0;
            uint max = 0;
            uint result = 0;
            for (uint teller = ondergrens; teller <= bovengrens; teller++)
            {
                for (uint i = 1; i != teller; i++)
                {
                    if (teller % i == 0 && Palindroom(i))
                    {
                        totaal++;
                        if (totaal > max)
                        {
                            max = totaal;
                            antwoord = teller;
                        }
                    }
                }
                totaal = 0;
            }
            if (string.Compare("palindroomdelers", maxOrDenominator, true) == 0)
            {
                result = max;
            }
            else
            {
                result = antwoord;
            }
            return result;
        }

        public bool Palindroom(uint getal)
        {
            uint kopie = getal;
            uint vergelijk = 0;
            uint temp = 0;
            while (getal > 0)
            {
                temp = getal % 10;
                vergelijk = (vergelijk * 10) + temp;
                getal = getal / 10;
            }
            if (vergelijk == kopie)
            {
                return true;
            }
            // you don't need an else clause here
            // if the above if condition is true, this return statement will never be run
            return false;
        }
    }
}

Open in new window

Avatar of kensy11

ASKER

Thank you again. but is there another way of doing this without the string ??
ASKER CERTIFIED SOLUTION
Avatar of Vel Eous
Vel Eous

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial