A A

A A

  • NA
  • 21
  • 11.9k

i had to repost. any help with my code?

Sep 30 2013 3:23 PM
hello, i recently asked for help on my practice code and tried to post a response on it but i dont know why its wont let me so i am posting a new question. Somebody helped me with the constructor problem i was having but for some reason we cannot figure out why my set and get commands arent working.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace the_helped
{
    class Animal
    {
        private string name;
        private string race;
        private double weight;
        private double age;

        public double _age
        {
            get
            {
                return age;
            }
            set
            {

                if (value > 0 && value <= 25) age = value;
                else
                {
                    age = 1;//for example here i want '1' to be the default number but when i run                                 //the program, the number that shows is '0' and another thing that
                               
                    Console.WriteLine("Invalid Age!.");//and another thing that is happening is
                                                                //is that when the condition in 'if' is not
                                                               //true 'invalid age!' does not show with the                                                               //default number '1'.
                                                              //ex: "please enter dogs age:" 500 (user input)
                                                     //Invalid age! Dog's age: 1. the same thing
                                                        //happens with weight. i just dont understand why
                                                        //its not working.
                }
            }
        }
        public double _weight
        {

            get
            {
                return weight;
            }
            set
            {

                if (value > 0 && value <= 60) weight = value;

                else
                {
                    weight = 1;
                    Console.WriteLine("Invalid Weight!");
                }
            }
        }

        public Animal()
        {
            Console.Write("These are the constructor details : ");

        }
        public void getdetails()
        {
            Console.WriteLine("\nName: No Name");
            Console.WriteLine("Race: No Race");
            Console.WriteLine("Age: " + age);
            Console.WriteLine("Weight: " + weight);
        }

        public static void Main(String[] args)
        {
            Animal dog = new Animal();
            dog.getdetails();
            Console.Write("Please enter your dog's name: ");
            dog.name = Console.ReadLine();
            Console.Write("Please enter your dog's age: ");
            dog.age = double.Parse(Console.ReadLine());
            Console.Write("Please enter your dog's weight: ");
            dog.weight = double.Parse(Console.ReadLine());
            Console.Write("Please enter your dog's race: ");
            dog.race = Console.ReadLine();


            Console.WriteLine(dog.name);
            Console.WriteLine(dog.race);
            Console.WriteLine(dog.weight);
            Console.WriteLine(dog.age);
            Console.ReadLine();
        }
    }
}

Answers (3)