Showing posts with label c# coding challenge. Show all posts
Showing posts with label c# coding challenge. Show all posts

Thursday, 16 January 2025

CSharp C# Method Overloading with Practical Example and Coding Challenge / Coding Excercise

I've linked my YouTube video below, where I explain method overloading in C# with a practical example and a fun coding challenge at the end. Below the video, you'll find the actual code featured in the tutorial.

Try solving the coding challenge yourself before checking out the solution provided at the end of this blog - it’s a fantastic way to test your understanding and sharpen your skills! Happy coding! 😊





Actual code featured in the tutorial


using System;

namespace MethodOverLoading
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Log log = new Log();
            log.DisplayLog("Welcome to this program.");
            log.DisplayLog("You have successfully created the file on ", DateTime.Now);
            log.DisplayLog("Requires user authentication", 401);
        }
    }

    public class Log
    {
        public void DisplayLog(string message)
        {
            Console.WriteLine(message);
        }
        public void DisplayLog(string message, DateTime dateAndTime)
        {
            Console.WriteLine($"{message}, {dateAndTime}");
        }
        public void DisplayLog(string error, int errorCode)
        {
            Console.WriteLine($"{error}, error code: {errorCode}");
        }
    }
}



Method Overloading coding challenge solution

Below is the solution to the coding challenge featured in the video. In this example, I’ve used the double data type for the square and rectangle area parameters, and int for the circle area. However, feel free to use any data types you prefer—as long as the method signatures are different, your solution will work perfectly for this challenge.


using System;

namespace MethodOverLoadingExcercise
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Area(3.0);
            Area(1.5, 4);
            Area(4);
        }

        //area of a square
        public static double Area(double length)
        {
            //if decimal, then round to 2 decimal places
            double output = Math.Round(length * length, 2);

            Console.WriteLine($"Area of square: {output}");

            return output;
        }

        //area of a rectangle
        public static double Area(double length1, double length2)
        {
            double output = Math.Round(length1 * length2, 2);

            Console.WriteLine($"Area of rectangle: {output}");

            return output;
        }

        /* area of a circle
        We use an integer parameter for the radius to make this 
        method's signature unique compared to the square's area method. 
        This is important because both shapes involve a single numeric input.
         */
        public static double Area(int radius)
        {
            //const means variable is a constant
            const double pi = Math.PI;

            //Area of a circle = pi * r * r, rounded to 2 decimals
            double output = Math.Round(pi * radius * radius, 2);

            Console.WriteLine($"Area of circle: {output}");

            return output;
        }
    }
    
}

Sunday, 5 January 2025

CSharp C# Method Overriding in with a Practical Example and Coding Challenge

I've linked my YouTube video below, where I explain method overriding in C# with a practical example and a fun coding challenge at the end. Below the video, you'll find the actual code featured in the tutorial.

I have used virtual method in the example so i have used abstract class and method for the coding challenge solution. You can solve using both ways. Try solving the coding challenge yourself before checking out the solution provided at the end of this blog - it’s a fantastic way to test your understanding and sharpen your skills! Happy coding! 😊



Actual code featured in the youtube tutorial above


using System;

namespace Method_Overriding
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Engineer engineer = new Engineer();
            engineer.Work();

            FrontEndEngineer frontEnd = new FrontEndEngineer();
            frontEnd.Work();

            BackEndEngineer backEnd = new BackEndEngineer();
            backEnd.Work();

            DevOpsEngineer devOps = new DevOpsEngineer();
            devOps.Work();
        }
    }

    public class Engineer   //Parent class, base class, superclass
    {
        public virtual void Work()
        {
            Console.WriteLine("All engineers need to work");
        }
    }

    public class FrontEndEngineer : Engineer //Child class, derived class
    {
        public override void Work()
        {
            Console.WriteLine("Front end engineers creates the user interface of website and application");
        }
    }

    public class BackEndEngineer : Engineer    
    {
        public override void Work()
        {
            Console.WriteLine("Back end engineers creates and maintains the serverside of application");
        }
    }

    public class DevOpsEngineer : Engineer
    {
        public override void Work()
        {
            Console.WriteLine("Dev ops engineers maintains the companies software operation");
        }
    }
}



Method Overriding Coding Challenge Solution


using System;

namespace MethodOverridingExcerciseDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Remote TV = new Tv();
            TV.PressPlay();

            Remote MusicPlayer = new MusicPlayer();
            MusicPlayer.PressPlay();

            Remote VideoGameConsole = new VideoGameConsole();
            VideoGameConsole.PressPlay();
        }
    }

    //Remote considered as abstract and it will be our parent class
    public abstract class Remote
    {
        public abstract void PressPlay(); //abstract method has no body
    }

    public class Tv : Remote
    {
        public override void PressPlay()
        {
            Console.WriteLine("Starting a movie.");
        }
    }

    public class MusicPlayer : Remote
    {
        public override void PressPlay()
        {
            Console.WriteLine("Starting a song");
        }
    }

    public class VideoGameConsole : Remote
    {
        public override void PressPlay()
        {
            Console.WriteLine("Starting a game");
        }
    }
}