Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Method Overloading Practice | OOP Essentials
C# Beyond Basics

Method Overloading PracticeMethod Overloading Practice

In this practice problem, you are given a ShippingCalculator class with a method named CalculateShippingCost. The method should be overloaded to support different scenarios of shipping costs based on the weight and destination.

Your task is to fill in the blanks (___) appropriately to complete the calculations in the CalculateShippingCost method for both local and international shipments.

cs

index.cs

1. You need to overload the CalculateShippingCost. Therefore the new method needs to have the same name but different parameters. The required parameters are provided in the comments.

        using System;
        public class ShippingCalculator
        {
            // Method to calculate shipping cost for local shipments
            public double CalculateShippingCost(double weight)
            {
                // Local shipments have a fixed rate of $5 per kilogram
                return weight * 5;
            }
        
            // Method to calculate shipping cost for international shipments
            // It has two parameters 'weight' and an additional 'destinationCountry' which is of type 'string'
            public double CalculateShippingCost(double weight, string destinationCountry)
            {
                // International shipments have a base rate of $10 per kilogram
                // Additional $3 per kilogram for shipments to Canada
                // Additional $5 per kilogram for shipments to other countries
                double baseRate = 10;
                double additionalRate = destinationCountry.ToLower() == "canada" ? 3 : 5;
        
                return weight* (baseRate + additionalRate);
            }
        }
        
        class ConsoleApp
        {
            static void Main()
            {
                ShippingCalculator calculator = new ShippingCalculator();
        
                // Test local shipment
                double localCost = calculator.CalculateShippingCost(2);
                Console.WriteLine($"Local Shipment Cost: ${localCost}");
        
                // Test international shipment to Canada
                double internationalToCanadaCost = calculator.CalculateShippingCost(2, "Canada");
                Console.WriteLine($"International Shipment to Canada Cost: ${internationalToCanadaCost}");
        
                // Test international shipment to other countries
                double internationalToOtherCost = calculator.CalculateShippingCost(2, "Germany");
                Console.WriteLine($"International Shipment to Other Country Cost: ${internationalToOtherCost}");
            }
        }        
    

Все було зрозуміло?

Секція 4. Розділ 8
course content

Зміст курсу

C# Beyond Basics

Method Overloading PracticeMethod Overloading Practice

In this practice problem, you are given a ShippingCalculator class with a method named CalculateShippingCost. The method should be overloaded to support different scenarios of shipping costs based on the weight and destination.

Your task is to fill in the blanks (___) appropriately to complete the calculations in the CalculateShippingCost method for both local and international shipments.

cs

index.cs

1. You need to overload the CalculateShippingCost. Therefore the new method needs to have the same name but different parameters. The required parameters are provided in the comments.

        using System;
        public class ShippingCalculator
        {
            // Method to calculate shipping cost for local shipments
            public double CalculateShippingCost(double weight)
            {
                // Local shipments have a fixed rate of $5 per kilogram
                return weight * 5;
            }
        
            // Method to calculate shipping cost for international shipments
            // It has two parameters 'weight' and an additional 'destinationCountry' which is of type 'string'
            public double CalculateShippingCost(double weight, string destinationCountry)
            {
                // International shipments have a base rate of $10 per kilogram
                // Additional $3 per kilogram for shipments to Canada
                // Additional $5 per kilogram for shipments to other countries
                double baseRate = 10;
                double additionalRate = destinationCountry.ToLower() == "canada" ? 3 : 5;
        
                return weight* (baseRate + additionalRate);
            }
        }
        
        class ConsoleApp
        {
            static void Main()
            {
                ShippingCalculator calculator = new ShippingCalculator();
        
                // Test local shipment
                double localCost = calculator.CalculateShippingCost(2);
                Console.WriteLine($"Local Shipment Cost: ${localCost}");
        
                // Test international shipment to Canada
                double internationalToCanadaCost = calculator.CalculateShippingCost(2, "Canada");
                Console.WriteLine($"International Shipment to Canada Cost: ${internationalToCanadaCost}");
        
                // Test international shipment to other countries
                double internationalToOtherCost = calculator.CalculateShippingCost(2, "Germany");
                Console.WriteLine($"International Shipment to Other Country Cost: ${internationalToOtherCost}");
            }
        }        
    

Все було зрозуміло?

Секція 4. Розділ 8
some-alt