Uitdaging: Methode-overbelasting
In dit oefenprobleem krijg je een ShippingCalculator
-klasse met een methode genaamd CalculateShippingCost. De methode moet worden overbelast om verschillende scenario's van verzendkosten te ondersteunen op basis van het gewicht en de bestemming.
Je taak is om de lege plekken (___
) op de juiste manier in te vullen om de berekeningen in de CalculateShippingCost
-methode te voltooien voor zowel lokale als internationale zendingen.
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344using 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}"); } }
Je moet de CalculateShippingCost
overbelasten. Daarom moet de nieuwe methode dezelfde naam hebben, maar andere parameters. De vereiste parameters worden in de opmerkingen gegeven.
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344using 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}"); } }
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you provide the code for the ShippingCalculator class?
What are the specific blanks that need to be filled in the CalculateShippingCost method?
Could you give an example of how the overloaded methods should work?
Awesome!
Completion rate improved to 2.04
Uitdaging: Methode-overbelasting
Veeg om het menu te tonen
In dit oefenprobleem krijg je een ShippingCalculator
-klasse met een methode genaamd CalculateShippingCost. De methode moet worden overbelast om verschillende scenario's van verzendkosten te ondersteunen op basis van het gewicht en de bestemming.
Je taak is om de lege plekken (___
) op de juiste manier in te vullen om de berekeningen in de CalculateShippingCost
-methode te voltooien voor zowel lokale als internationale zendingen.
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344using 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}"); } }
Je moet de CalculateShippingCost
overbelasten. Daarom moet de nieuwe methode dezelfde naam hebben, maar andere parameters. De vereiste parameters worden in de opmerkingen gegeven.
index.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344using 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}"); } }
Bedankt voor je feedback!