I'm trying to have a method in a separate class do some math for me, and then write the result into the console. The issue I'm hitting now is that its saying the object reference doesn't have an instance to use. I thought I had instantiated it earlier in the class that the method that calls all the other methods is in, but apparently something isn't right, and I have no clue what to do to make it work. The second section of math will give me the same error, but if i can fix this one I should be able to fix the second one easily.
class FruitGarden
{
private Apple apple;
private Banana banana;
static void Main(string[] args)
{
FruitGarden fruitGarden = new FruitGarden();
fruitGarden.EatFruits();
}
public void MakeFruits()
{
Apple apple = new Apple();
apple.apple(1.5);
Banana banana = new Banana();
banana.banana(3.5);
}
public void EatFruits()
{
double dblpercent;
MakeFruits();
Console.WriteLine("You have an Apple and a Banana in your fruit garden.\n");
Console.WriteLine("What Percent of the Apple would you like to eat?");
dblpercent = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("\nWhat Percent of the Banana would you like to eat?");
dblpercent = Convert.ToDouble(Console.ReadLine());
Console.Write("You have ");
apple.Eat(dblpercent);
Console.Write("% of your apple, and ");
banana.Eat(dblpercent);
Console.Write("% of your banana left.");
Console.ReadLine();
}
}
The apple class that its trying to reference is:
class Apple : Fruit
{
public double Radius { get;set;}
public void apple(double radius)
{
Radius = Radius;
}
}
I thought the apple apple = new Apple();
would make the instance it needed, but apparently not?