locked
Help needed with class construction with constructors, properties, methods etc RRS feed

  • Question

  • User-501297529 posted

    I posted this in another section but received no help so I figured I would post it here. I need help badly.

    This is not a project or solution. I'm trying to learn how to build classes with constructors, properties, methods etc. This is what I have so far to start with but I feel like I need little more in the Vehicle class to make this class a little more complete. If someone could assist me on what I need to add to this class from what I have now.

    Vehicle class

    public class Vehicle
        {
    
            private int wins = 0;
            private int loses = 0;
            
            /// <summary>
            /// Constructor takes one argument for name.
            /// </summary>
            /// <param name="name"></param>
            public Vehicle(string name)
            {
                Name = name;
            }
    
            /// Property that has type driver, driver represents the driver of the vehicle.
            public string Name { get; set; }
    
            /// <summary>
            /// Public method named StartRace. Returns void, takes integer. Starts race when called.
            /// </summary>
            /// <param name="laps"></param>
            public void StartRace(int laps)
            {
                /// Winner gets to more than 200 laps, if not you lose.
                if (laps > 200)
                    wins++;
                else
                    loses++;
    
            }
        }

    Driver class

    namespace RaceTrack
    {
        /// <summary>
        ///Driver is class type. This driver will drive vehicle.
        /// </summary>
        public class Driver
        {
    
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }

    What can I add to the Vehicle class besides what I already have? Do I need to have  a seperate Race class and have it keep track of all the vehicles?  Do I need to have a Driver class-Vehicle class relationship? How code I do all of this?

    Wednesday, November 13, 2019 3:42 AM

Answers

  • User475983607 posted

    Yes, the new inheritance structure is a far better design than the previous snippets.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 7:55 PM

All replies

  • User-1780421697 posted

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://enterprisecraftsmanship.com/posts/separation-of-concerns-in-orm/

    https://en.wikipedia.org/wiki/Domain-driven_design

    I think you need to have a look on these articles, they give you some guidelines like separation on concern, single responsibility and domain driven design etc.
    If someone is going to design classes for you this time on forum that will not clear your core concepts, you have to learn the basics by yourself.

    Wednesday, November 13, 2019 5:54 AM
  • User288213138 posted

    Hi bootzila,

    bootzilla

    What can I add to the Vehicle class besides what I already have?

    The below codes list the kinds of members a class may contain, you can add them to the class according to your requirement.

    Fields Constants Properties Methods Events Operators Indexers Constructors Finalizers Nested Types

    More information about C# class you can refer to this link: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/members

    bootzilla

    Do I need to have  a seperate Race class and have it keep track of all the vehicles?  Do I need to have a Driver class-Vehicle class relationship? How code I do all of this?

    You should tell me your requirement first, then I can answer your questions. if you want to call the Vehicle class or the method(or other members) of this class in the Driver class, you can instantiate the Vehicle class first, then call them through the instantiated object.

    public class Driver
            {
    
                public int Id { get; set; }
                public string Name { get; set; }
                Vehicle vehicle=new Vehicle();
                vehicle.Name="name1";
                vehicle.StartRace(Id);
    
            }

    More information about C# class you can refer to this link: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes

    Best regards,

    Sam

    Wednesday, November 13, 2019 5:58 AM
  • User-501297529 posted

    My requirement is to get the Driver Name and Id for each Vehicle. So Vehicle 1 whatever that is gets Driver name and Id, Vehicle 2 same thing, etc. I think? Something like that. This is why I'm asking for help with my code because I don't know what direction to go to make this flow correctly.

    I tried what you suggest in the Driver class and get errors like Constructor takes one argument for name' on Vehicle vehicle = new Vehicle line. Also Make vehicle read only, Name vehicle does not match rule (Instance fields) private' on that same line, which causes erros for the two lines below it:

     public class Driver
        {
            public int Id { get; set; }
            public string Name { get; set; }
            Vehicle vehicle = new Vehicle();
            vehicle.Name = "name1";
            vehicle.StartRace(Id);
        }

    Wednesday, November 13, 2019 2:10 PM
  • User-501297529 posted

    Hi bootzila,

    bootzilla

    What can I add to the Vehicle class besides what I already have?

    The below codes list the kinds of members a class may contain, you can add them to the class according to your requirement.

    Fields Constants Properties Methods Events Operators Indexers Constructors Finalizers Nested Types

    More information about C# class you can refer to this link: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/members

    bootzilla

    Do I need to have  a seperate Race class and have it keep track of all the vehicles?  Do I need to have a Driver class-Vehicle class relationship? How code I do all of this?

    You should tell me your requirement first, then I can answer your questions. if you want to call the Vehicle class or the method(or other members) of this class in the Driver class, you can instantiate the Vehicle class first, then call them through the instantiated object.

    public class Driver
            {
    
                public int Id { get; set; }
                public string Name { get; set; }
                Vehicle vehicle=new Vehicle();
                vehicle.Name="name1";
                vehicle.StartRace(Id);
    
            }

    More information about C# class you can refer to this link: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes

    Best regards,

    Sam

    Hi Sam,

    Thanks for your assistance.

    My requirement is to get the Driver Name and Id for each Vehicle. So Vehicle 1 whatever that is gets Driver name and Id, Vehicle 2 same thing, etc. I think? Something like that. This is why I'm asking for help with my code because I don't know what direction to go to make this flow correctly.

    I tried what you suggest in the Driver class and get errors like Constructor takes one argument for name' on Vehicle vehicle = new Vehicle line. Also Make vehicle read only, Name vehicle does not match rule (Instance fields) private' on that same line, which causes erros for the two lines below it:

     public class Driver
        {
            public int Id { get; set; }
            public string Name { get; set; }
            Vehicle vehicle = new Vehicle();
            vehicle.Name = "name1";
            vehicle.StartRace(Id);
        }
    Wednesday, November 13, 2019 2:28 PM
  • User475983607 posted

    First, set aside time to learn C# constructs such as classes, properties, and methods.  These concepts are found in the C# programming guide.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/

    While the constructs are simple to understand, applying the constructs to craft a program is not as simple.  Basically objects should model real life.  An object like a vehicle should contain items that pertain to the vehicle .  Properties like the vehicle name, id, color, type, top speed, etc describe a vehicle .  A vehicle should not store wins and losses.  Wins and losses are a part of a racing season or group of races.

    While a vehicle can keep track of laps.  A race is the record of truth not the vehicle.  A Race object would track the vehicles and the vehicle laps.

    The problem you are facing has more to do with design than the constructs.

    Wednesday, November 13, 2019 2:45 PM
  • User288213138 posted

    Hi bootzilla,

    I tried what you suggest in the Driver class and get errors like Constructor takes one argument for name' on Vehicle vehicle = new Vehicle line. Also Make vehicle read only, Name vehicle does not match rule (Instance fields) private' on that same line, which causes erros for the two lines below it:

    I'm very sorry, If you want to call an attribute of a class in another class, you should put it in the method instead of writing it directly in the class.

    My requirement is to get the Driver Name and Id for each Vehicle

    You can try below code, I declare a vehicle property in the Driver class and then assign different values to their properties when instantiating.

    public class Driver
        {       
            public int Id { get; set; }
            public string Name { get; set; }       
            public Vehicle vehicle { get; set; }
        }
    
    static void Main(string[] args)
    {
    Vehicle newVehicle = new Vehicle("Vehicle1");
    Vehicle newVehicle2 = new Vehicle("Vehicle2");
    Driver driver = new Driver() { Id = 1, Name = "name1", vehicle = newVehicle };
    Driver driver2 = new Driver() { Id = 2, Name = "name2", vehicle = newVehicle2 };
    }

    Best regards,

    Sam

    Thursday, November 14, 2019 2:53 AM
  • User-501297529 posted

    I updated/cleaned up some of  my code a little bit.

    Vehicle class:

    namespace RaceTrack
    {
        /// <summary>
        ///Vehicle is class type. This vehicle will be raced.
        /// </summary>
        public class Vehicle : Driver
        {
    
            public Driver driver { get; set; }
            ///initial field values for which driver wins and which driver loses.
    
            private int _wins = 0;
            private int _loses = 0;
    
            /// <summary>
            /// Constructor takes arguments for driver.
            /// </summary>
            /// <param name="driver"></param>
            
            public Vehicle(string driver)
            {
            }
    
            /// Property that has type driver, driver represents the driver of the vehicle.
    
            public bool Qualify { get; set; }
    
    
            /// <summary>
            /// Public method named StartRace. Returns void, takes integer that will determine number of laps to win the race. Starts race when called.
            /// </summary>
            /// <param name="laps"></param>
            public void StartRace(int laps)
            {
                // Winner passes 200 laps, if not you lose.
                if (laps > 200)
                {
                    _wins++;
                }
                else
                    _loses++;
            }
    
            /// <summary>
            /// Public method named ToQualify. Returns void, takes integer that will determine if driver qualifies for race.
            /// </summary>
            /// <param name="mph"></param>
            public void ToQualify(int mph)
            {
                // Driver qualifies with 225 mph or greater, if not driver does not qualify for race.
                if (mph > 225)
                {
                   Qualify = true;
                }
                else
                   Qualify =  false;
            }
    
            static void Main(string[] args)
            {
                Vehicle newVehicle = new Vehicle("Vehicle1");
                Vehicle newVehicle2 = new Vehicle("Vehicle2");
                Driver driver = new Driver() { Id = 1, DriverName = "name1", Vehicle = newVehicle };
                Driver driver2 = new Driver() { Id = 2, DriverName = "name2", Vehicle = newVehicle2 };
            }
        }
    }

    Driver class:

    namespace RaceTrack
    {
        ///// <summary>
        /////Driver is class type. This driver will drive vehicle.
        ///// </summary>
        public class Driver
        {
            public int Id { get; set; }
            public string DriverName { get; set; }
    
            public Vehicle Vehicle { get; set; }
    
        }
    }

    What I want to do is have something that is derived from the Vehicle class. What do I need to do to code that? Is that just inheritance or a little more than that.

    Monday, November 18, 2019 8:39 PM
  • User475983607 posted

    The inheritance structure does not make logical sense. 

    A Vehicle is not a type of Driver.  A Driver is a type of Person.  A Race Car, Dump Truck, or Bus are types of Vehicles.  

    A Race Track is a physical location not a Namespace.  Namespaces are used to make objects unique.  For example a table.  A table can be a physical item where you eat lunch.  It can also be grid structure on a web page;  Furniture.Kitchen.Table or System.Web.UI.WebControls.Table.

    I recommend reading the c# programming guide on inheritance and polymorphism.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/inheritance

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/polymorphism

    Hint: a vehicle has a driver or a VehicleDriver.

        public class VehicleDriver
        {
            public int  VehicleDriverId { get; set; }
            public int DriverId { get; set; }
            public int VehicleId { get; set; }
        }

    Monday, November 18, 2019 9:20 PM
  • User-501297529 posted

    The inheritance structure does not make logical sense. 

    A Vehicle is not a type of Driver.  A Driver is a type of Person.  A Race Car, Dump Truck, or Bus are types of Vehicles.  

    A Race Track is a physical location not a Namespace.  Namespaces are used to make objects unique.  For example a table.  A table can be a physical item where you eat lunch.  It can also be grid structure on a web page;  Furniture.Kitchen.Table or System.Web.UI.WebControls.Table.

    I recommend reading the c# programming guide on inheritance and polymorphism.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/inheritance

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/polymorphism

    Hint: a vehicle has a driver or a VehicleDriver.

        public class VehicleDriver
        {
            public int  VehicleDriverId { get; set; }
            public int DriverId { get; set; }
            public int VehicleId { get; set; }
        }

    So can VehicleDriver be inherited? I inherited into the Vehicle class

     public class Vehicle : VehicleDriver
        {
           
            public Driver driver { get; set; }
           
    
            private int _wins = 0;
            private int _loses = 0;
    
           
            public Vehicle(string driver)
            {
            }
            
           
            public bool Qualify { get; set; }
    
    
            public void StartRace(int laps)
            {
                // Winner passes 200 laps, if not you lose.
                if (laps > 200)
                {
                    _wins++;
                }
                else
                    _loses++;
            }
    
            public void ToQualify(int mph)
            {
                
                if (mph > 225)
                {
                   Qualify = true;
                }
                else
                   Qualify =  false;
            }
    
            static void Main(string[] args)
            {
                VehicleDriver driver = new VehicleDriver() { VehicleDriverId = 1, DriverName = "name1", VehicleName = "McLaren Senna" };
                
            }
        }

    VehicleDriver class

     public class VehicleDriver
        {
            public int VehicleDriverId { get; set; }
            public int DriverId { get; set; }
            public int VehicleId { get; set; }
    
            public string VehicleName { get; set; }
            public string DriverName { get; set; }
        }

    Would something like this work?

    Wednesday, November 20, 2019 2:06 PM
  • User475983607 posted

    bootzilla

    So can VehicleDriver be inherited? I inherited into the Vehicle class

    The inheritance structure makes little logical sense for the same reasons explained above.   I'm sorry but there is not much else I can do for you.  Learn Object Oriented Programming (OOP) fundamentals before moving forward.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/inheritance

    Wednesday, November 20, 2019 2:15 PM
  • User-501297529 posted

    mgebhard

    bootzilla

    So can VehicleDriver be inherited? I inherited into the Vehicle class

    The inheritance structure makes little logical sense for the same reasons explained above.   I'm sorry but there is not much else I can do for you.  Learn Object Oriented Programming (OOP) fundamentals before moving forward.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/inheritance

    What? Well you posted this.

    mgebhard

    Hint: a vehicle has a driver or a VehicleDriver.

        public class VehicleDriver
        {
            public int  VehicleDriverId { get; set; }
            public int DriverId { get; set; }
            public int VehicleId { get; set; }
        }

    And now you tell me it logically doesn't make sense.  THAT doesn't make sense. What the hell is the point of posting that VehicleDriver code? I'm not asking for you or anyone to post links to a website which you are constantly doing I'm asking to review the code that I have posted and if it is on the right path. The code I have compiles with no errors. Please look at what I have and let me know if I'm on the right path. That's all I'm asking, not for links to another website.

    Also I do know the basics of object oriented language but I am asking for assistance on here to whether I'm on the right path. As I stated previously I'm not building a full on project but just trying to connect a few class files to see if what I have will work.

    Wednesday, November 20, 2019 3:35 PM
  • User475983607 posted

    And now you tell me it logically doesn't make sense.  THAT doesn't make sense. What the hell is the point of posting that VehicleDriver code?

    You added two properties to the class which are not needed because the DriverId and VehicleId can be used to get to the Driver and Vehicle.  

     public class VehicleDriver
        {
            public int VehicleDriverId { get; set; }
            public int DriverId { get; set; }
            public int VehicleId { get; set; }
    
            public string VehicleName { get; set; }
            public string DriverName { get; set; }
        }

    I'm not asking for you or anyone to post links to a website which you are constantly doing I'm asking to review the code that I have posted and if it is on the right path.

    And that's exactly what happened here.  I reviewed your code and provided feedback that included sample code and reference documentation.  Clearly, not the answer you wanted. 

    Wednesday, November 20, 2019 4:11 PM
  • User-501297529 posted

    I updated a lot of the code. Does this look better? If not any suggestions or if it does what could be better?

    Vehicle class

    namespace RacingVehicleTypes
    {
        public class Vehicle
        {
    
            int _fuelcap; // fuel capacity in gallons   
    
            int _mpg; // fuel consumption in miles per gallon  
    
    
            public Vehicle(int f, int m)
            {
                fuelcap = f;
                mpg = m;
            }
    
            public double fuelneeded(int miles)
            {
                return (double) miles / mpg;
            }
    
    
            public int fuelcap
            {
                get { return _fuelcap; }
                set { _fuelcap = value; }
            }
    
            public int mpg
            {
                get { return _mpg; }
                set { _mpg = value; }
    
            }
        }
    }
    

    Truck class

    namespace RacingVehicleTypes
    {
    
    public class Truck: Vehicle
    {
    
    public int TruckId { get; set; }
    
    public string RacingClass { get; set; }
    
    public string Type { get; set; }
    
    public Truck(int f, int m) : base(f, m)
    {
    
    }
    
    }
    
    public class TruckDemo
    {
    public static void Main()
    {
    
    // construct different types of cars
    Truck sportsman = new Truck(2, 200);
    Truck pickup = new Truck(3, 28);
    double gallons;
    int dist = 250;
    
    gallons = sportsman.fuelneeded(dist);
    
    
    Console.WriteLine("To go " + dist + " miles semi needs " +
    gallons + " gallons of fuel.\n");
    
    gallons = pickup.fuelneeded(dist);
    
    Console.WriteLine("To go " + dist + " miles pickup needs " +
    gallons + " gallons of fuel.");
    }
    }
    
    }

    Car class

    namespace RacingVehicleTypes
    {
        public class Car: Vehicle
        {
            public int CarId { get; set; }
    
            public string RacingClass { get; set; }
            
            public string Type { get; set; }
    
            public Car(int f, int m) : base(f, m)
            {
    
            }
    
        }
       
        public class CarDemo
        {
            public static void Main1()
            {
    
                // construct different types of cars
                Car stock = new Car(2, 200);
                Car drag = new Car(2, 100);
                Car offroad = new Car(2, 80);
                double gallons;
                int dist = 250;
    
                gallons = stock.fuelneeded(dist);
    
    
                Console.WriteLine("To go " + dist + " miles semi needs " +
                                  gallons + " gallons of fuel.\n");
    
                gallons = drag.fuelneeded(dist);
    
    
                Console.WriteLine("To go " + dist + " miles semi needs " +
                                  gallons + " gallons of fuel.\n");
    
                gallons = offroad.fuelneeded(dist);
    
                Console.WriteLine("To go " + dist + " miles pickup needs " +
                                  gallons + " gallons of fuel.");
            }
        }
    }

    Monday, November 25, 2019 7:33 PM
  • User475983607 posted

    Yes, the new inheritance structure is a far better design than the previous snippets.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 7:55 PM