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

bookMethod 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.

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344
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 * ___; } // Method to calculate shipping cost for international shipments // It has two parameters 'weight' and an additional 'destinationCountry' which is of type 'string' ___ { // 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 = ___; double additionalRate = destinationCountry.ToLower() == "canada" ? ___ : ___; 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}"); } }

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 8

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain how method overloading works in this example?

What is the difference between the local and international shipping calculations?

Can you walk me through the logic for calculating the international shipping cost?

Awesome!

Completion rate improved to 2.04

bookMethod Overloading Practice

Deslize para mostrar o menu

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.

index.cs

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344
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 * ___; } // Method to calculate shipping cost for international shipments // It has two parameters 'weight' and an additional 'destinationCountry' which is of type 'string' ___ { // 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 = ___; double additionalRate = destinationCountry.ToLower() == "canada" ? ___ : ___; 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}"); } }

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 8
some-alt