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
- Create a new blank console application.
- Add a class named “Monster” that has the following attributes: string name, int health, int positionX, int positionY.
- Create a constructor that initializes accepts data via parameters to create a new monster.
- 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!
- 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. - 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.
- 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();
}
}
}