Click here to Skip to main content
16,020,378 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I am asked to write a class called “Magic.” Include private int member fields spiders and toads and their properties, which include get and set functions. Also include a public void member function called do_Magic(), that print out “Poooofff”. And finally include 2 Constructors, one that takes no arguments, and sets toads and spider both equal 0; one that takes two int arguments, and sets toads equal to the first argument and spiders equal to the second argument. In Main(), I am trying to create an instance myMagic and assign the values 3,5 to toads and spiders, respectively.
I have wrote the codes that output Poooofff, but I don’t know it that is write or wrong. I am confused what this program’s output would be? If someone direct me whatever I have done is correct or help me understand what above question is asking for?

C#
{
    class Magic
    {
        private int spiders;
        public int Spiders
        {
            set
            {
                spiders = value;
            }
            get
            {
                return spiders;
            }
        }

        private int toads;
        public int Toads
        {
            set
            {
                toads = value;
            }
            get
            {
                return toads;

            }
        }
        public void do_myMagic()
        {
             spiders= 0;
             toads = 0;
            Console.WriteLine("Pooofff");

        }
        public void do_myMagic(int arg1, int arg2)
        {
             toads = arg1;
            spiders = arg2;
        }
        class Program
        {
            static void Main()
            {
                Magic myMagic = new Magic();
                myMagic.spiders = 5;
                myMagic.toads = 3;
                myMagic.do_myMagic();
            }
        }
    }
Posted

You are getting close but your understanding of constructors is deficient.

To begin read up on constructors:
An Intro to Constructors in C#[^]

do_MyMagic() only needs this:

C#
public void do_myMagic()
{
     Console.WriteLine("Pooofff");

}


You now need 2 constructors:

C#
class Magic {
public Magic()
{
// code
}

public Magic(int iToads, int iSpiders)
{
// code
}
}

I won't do the rest - you seem quite capable of that.

Then you will be able to write:

C#
Magic myMagic = new Magic(3, 5);


[EDIT] Now add at the top

C#
using System;

namespace Homework


And below
C#
myMagic.do_myMagic();

add the line
C#
Console.ReadKey(); // Hit key - Stops from exiting immediately.


This should compile and run printing out "pooff". Then you can play around with set get eg:
C#
Console.WriteLine(myMagic.Spiders);
myMagic.Spiders = 9;
Console.WriteLine(myMagic.Spiders);


I agree with Bill's comments.
 
Share this answer
 
v5
Comments
RalvarezHose 24-Mar-14 0:48am    
Thank you for your help. You are right I have very little knowledge on constructors, in fact very little knowledge. I will definitely read on constructors and try to complete this.
RalvarezHose 24-Mar-14 13:40pm    
With your help, so far I have come up with following codes. Still not getting completely what I am missing.
RalvarezHose 24-Mar-14 13:41pm    
{ class Magic
{
private int spiders;
public int Spiders
{
set
{spiders = value;
}
get
{return spiders;
}
}

private int toads;
public int Toads
{
set
{toads = value;
}
get
{return toads;
}
}
public void do_myMagic()
{
Console.WriteLine("Pooofff");
}
public Magic()
{

}
public Magic(int arg1, int arg2)
{
toads = arg1;
spiders = arg2;
}
class Program
{
static void Main()
{
Magic myMagic = new Magic(3,5);

myMagic.do_myMagic();
}
}
}
}
[no name] 24-Mar-14 19:46pm    
Well done so far. Have added an edit.
RalvarezHose 25-Mar-14 0:01am    
Thank you so much. I think I got it this time. Wouldn't be possible without your help though. You are awesome.
Constructor names should be the same name as the Class: so, use
public Magic()
public Magic(int arg1, int arg2)
do_myMagic should be the name of a method/function with return Type 'void that simply writes the string value to whatever you wish to output it to:
public void do_myMagic()
{
    // your code to do whatever
}
In my humble opinion, whoever gave you this "task" is unqualified to teach programming: it is a silly, and useless, assignment.

I recommend you get a good book on C# programming, and start working on code examples that will really teach you something.
 
Share this answer
 
Comments
RalvarezHose 24-Mar-14 0:36am    
My instructor has a C++ background, he recently studied C# and this is his second semester. He himself is confused while teaching. Whatever I have learned, I used youtube and all other online resources. I even don't know what the above program was asking and what would be its output.
RalvarezHose 24-Mar-14 0:36am    
Can you refer me to a good C# programming book?
BillWoodruff 24-Mar-14 2:01am    
If you feel comfortable telling me, I'd be curious to know what type of course you are in; what type of school.

Please check these posts out for book recommendations:

http://www.codeproject.com/Answers/704043/which-book-is-good-for-simple-csharp-coding

http://www.codeproject.com/Answers/314900/Books-in-C-sharp-anyone-knows-any-please-reply

http://www.codeproject.com/Answers/272546/programing-is-very-difficult

good luck !
RalvarezHose 24-Mar-14 12:16pm    
I am taking intermediate programming C#. It is a four year college and this is my second class in programming. Not only I, most of the students in my class having same problem. Not understanding the concept pretty well. Anyway, thank you for the sending those resources. I really appreciate that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900