methods

Hello, I have this assignment for my intro to computer programming class, and have tried to complete it, but keep running into some problems. The instructions for the assignment are below, along with the code I have. Thanks so much!

Directions

  1. Create a new blank console application.
  2. Add a class named “Monster” that has the following attributes: string name, int health, int positionX, int positionY.
  3. Create a constructor that initializes accepts data via parameters to create a new monster.
  4. Create your properties for your fields and use them to access the fields. Use Alt + Insert and use the dialog box to make this process go quickly!
  5. Write the following methods that would be part of this class:
    a. Write a method named “alterHealth” that takes in an integer value via parameter and increases or decreases health by that much. If the monster’s health reaches 0, display to the console “the monster is dead.” The method does not need to return anything.
    b. Set a maximum value for the monster’s health and implement this rule in the Health property. If the monster’s health reaches this value, display to the console “the monster has reached its maximum health.” No points for this step if you implement in the maximum value in a method; do this in the property!
    c. Write a method named “move” that takes in two integer values via parameters. Use this information to change the x and y positions of the monster, relative to the current position (don’t just assign the new value). This method does not need to return anything.
    d. Assume the monster can gain health by resting. Write a method named “rest” that asks the user how many minutes the monster rested (do not collect via parameters); increase the health value by that number, calling the alterHealth method. This method does not return anything.
    e. Override the ToString() to output the state of the monster. Make sure you display all fields.
  6. Create at least three instances of monster in Main and invoke ALL of your methods on each one, passing literals if data is required by any method.
  7. Output the state of each monster after interacting with the instances to make sure your methods are working properly.
using System;

namespace Lab_06
{

    class Monster
    {
        public string name;
        public int health;
        public int positionX;
        public int positionY;

        public Monster (string name, int health, int positionX, int positionY)
        {
           
        }

        public string Name {
            get{return name; }
            set { name = value; }
        }

        public int Health{
            get{return health; }
            set {health = value; }
            if (health>=100)
            {
                health = 100;
                Console.WriteLine("The monster has reached its maximum health.");
            }
            if (health <=0){
                health = 0;
                Console.WriteLine("The monster is dead.");
            }
        }

        public int PositionX{
            get{return positionX;}
            set{positionX = value;}
        }

        public int PositionY{
            get{return positionY;}
            set {positionY = value;}
        }

        public void alterHealth (int health)
        {
            setHealth(Health + health);
        }

        public void move (int positionX, int positionY)
        {
            setX(positionX + PositionX);
            setY(positionY + PositionY);
        }

        public void reset()
        {
            int minutes;
            Console.WriteLine("How many minutes the monster rested?");
            minutes = Convert.ToInt32(Console.ReadLine());
            setHealth(Health+minutes);
        }

        public void ToString()
        {
            Console.WriteLine("Name:" + name);
            Console.WriteLine("Health:" + health);
            Console.WriteLine("X:" + positionX + ", Y:" positionY);
        }


    class MainClass
    {
        public static void Main (string[] args)
        {
                Monster instance1, instance2, instance3;
                instance1 = newMonster("Akash", 50, 3, 6);
                instance2 = newMonster("Tapas", 90, 10, 6);
                instance3 = newMonster("Arka", 20, 15, 20);

                Console.WriteLine("Monster 1:");
                instance1.alterHealth(30);
                instance1.ToString();
                instance1.move(5, 10);
                instance1.ToString();
                instance1.rest();
                instance1.ToString();

                Console.WriteLine("Monster 2:");
                instance2.alterHealth (10);
                instnace2.ToString();
                instance2.move(5,10);
                instance2.ToString();
                instance2.rest();
                instance2.ToString();

                Console.WriteLine("Monster 3:");
                instance3.alterHealth(-20);
                instance3.ToString();
                instance3.move(5,10);
                instance3.ToString();
                instance3.rest();
                instance3.ToString();
                Console.Read();
        }
    }
}

Ummm…

What does this have to do with Unity?

It’s a part of a larger unity 3d game project that we are building up to

Also…

If this were Unity related.

You didn’t describe your problem.

You’re just like “I have an assignment, I keep running into problems, here’s the assignment and what I’ve done.”

What… are we supposed to read the assignment, look at your code, and then do the work for you?

How about you describe the problems you’re having. Are there errors/exceptions you’re getting? What do they say? Have you tried researching those errors/exceptions?

You don’t have a ‘setHealth’ method defined and it’s arguments are ambiguous so how is that bit supposed to work for you?

You expect us to do your homework?

You havent even described what problems you have.

I was going to post some help here but you should definitely try and help yourself where possible.
So if you really want some help I have posted it HERE instead.
Make sure you take some time to explore a couple of the references I have posted at the top and I implore you to not just copy some of the code and instead make edits and try to figure out how it varies from what you have currently.

Bears repeating. This is basic stuff. If you don’t understand this stuff, then you’re doomed for the rest of the course.

Make sure you thoroughly understand this stuff. Then re-write it again from scratch without looking at the previous code to prove (to yourself) you do understand it.

2 Likes

Can’t help solve the problems you’re having if you can’t be bothered to say what problems you’re having…