Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Herausforderung: Methodenüberladung | OOP-Grundlagen
C# Jenseits der Grundlagen

bookHerausforderung: Methodenüberladung

In dieser Übungsaufgabe erhalten Sie eine ShippingCalculator-Klasse mit einer Methode namens CalculateShippingCost. Die Methode soll überladen werden, um verschiedene Szenarien der Versandkosten basierend auf Gewicht und Zielort zu unterstützen.

Ihre Aufgabe ist es, die Lücken (___) entsprechend auszufüllen, um die Berechnungen in der Methode CalculateShippingCost sowohl für lokale als auch internationale Sendungen abzuschließen.

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}"); } }

Sie müssen die Methode CalculateShippingCost überladen. Daher muss die neue Methode denselben Namen, aber unterschiedliche Parameter haben. Die erforderlichen Parameter sind in den Kommentaren angegeben.

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 * 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}"); } }
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 8

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

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

bookHerausforderung: Methodenüberladung

Swipe um das Menü anzuzeigen

In dieser Übungsaufgabe erhalten Sie eine ShippingCalculator-Klasse mit einer Methode namens CalculateShippingCost. Die Methode soll überladen werden, um verschiedene Szenarien der Versandkosten basierend auf Gewicht und Zielort zu unterstützen.

Ihre Aufgabe ist es, die Lücken (___) entsprechend auszufüllen, um die Berechnungen in der Methode CalculateShippingCost sowohl für lokale als auch internationale Sendungen abzuschließen.

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}"); } }

Sie müssen die Methode CalculateShippingCost überladen. Daher muss die neue Methode denselben Namen, aber unterschiedliche Parameter haben. Die erforderlichen Parameter sind in den Kommentaren angegeben.

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 * 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}"); } }
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 8
some-alt