From 878491304c07dc5e355248564ffec7da52f566e2 Mon Sep 17 00:00:00 2001 From: Roman Eriksen Date: Tue, 5 Aug 2025 15:57:12 +0200 Subject: [PATCH] test passed --- Boolean.CSharp.Main/Core.cs | 11 +++--- Boolean.CSharp.Main/Extension.cs | 48 ++++++++++++++++++++++++--- Boolean.CSharp.Main/Misc/Bicycle.cs | 11 ++++++ Boolean.CSharp.Main/Misc/Car.cs | 4 +-- Boolean.CSharp.Main/Misc/Motorbike.cs | 3 +- Boolean.CSharp.Main/Misc/Unicycle.cs | 5 +++ Boolean.CSharp.Test/ExtensionTests.cs | 2 ++ 7 files changed, 72 insertions(+), 12 deletions(-) diff --git a/Boolean.CSharp.Main/Core.cs b/Boolean.CSharp.Main/Core.cs index e09c536..d77a9b3 100644 --- a/Boolean.CSharp.Main/Core.cs +++ b/Boolean.CSharp.Main/Core.cs @@ -30,7 +30,7 @@ public Car(string Make) } */ - Car car = new Car("Volkswagen"); + Car car = new Car("Volkswagen", "Beetle"); /* When the car in instantiated, the constructor is passed a string in this case Volkswagen which is a Make of car is passed in. Within the constructor the 'string Make' variable has scope within the constructor and assiged to the _make member now visible to the whole class @@ -75,7 +75,7 @@ Having to 2 constructors is an example of Overloading. */ //TODO 2. Ensure both constructors on the Motorbike class set the cc of the Motorcycle to 373. - Motorbike myMotorbike = new Motorbike("KTM", "Duke"); + Motorbike myMotorbike = new Motorbike(373, "KTM", "Duke"); if(myMotorbike.CC > 0) { @@ -92,7 +92,7 @@ public Bicycle Question3() //See there is somewhere to store the number of wheels the bike has //but no constructor to set this //TODO: 3. Add a constructor to the Bicycle class that populates the _wheelCount variable - Bicycle bike = new Bicycle(); + Bicycle bike = new Bicycle(3); return bike; @@ -108,7 +108,7 @@ public Unicycle Question4() //TIP see we already have an internal member for the unicyclist name: _nameOfUnicyclist so you can use this to store the name internally // it is good practice to name internal class variable with an _ at the beginning - Unicycle unicycle = new Unicycle(); + Unicycle unicycle = new Unicycle("Roman"); @@ -132,8 +132,9 @@ What are the parameters and types? */ Aeroplane plane = new Aeroplane(); plane.FlightDetails("LHR", "JFK"); - + //TODO: 5. Call the FlightDetails method that sets the cancelled message and cancel the flight + plane.FlightDetails("flight DZ708 LHR to JFK is cancelled"); //write code here diff --git a/Boolean.CSharp.Main/Extension.cs b/Boolean.CSharp.Main/Extension.cs index 80c1db6..ed8642f 100644 --- a/Boolean.CSharp.Main/Extension.cs +++ b/Boolean.CSharp.Main/Extension.cs @@ -2,6 +2,7 @@ +using System.Numerics; using System.Reflection.Metadata.Ecma335; namespace Boolean.CSharp.Main @@ -12,18 +13,57 @@ public class Extension //Implement the following methods: //TODO: 1. add, which accepts two floats and returns a float (both floats added together) + public float add(float a, float b) + { + return a + b; + } //TODO: 2. add, which accepts two doubles and returns a double (both doubles added together) + public double add(double a, double b) + { + return a + b; + } - //TODO: 3. subtract, which accepts two floats and returns a float (first float minus second float) + //TODO: 3. subtract, which accepts two floats and returns a float (first float minus second float) + public float subtract(float a, float b) + { + return a - b; + } - //TODO: 4. subtract, which accepts a String and a char and returns a string with all instances of the provided char removed + //TODO: 4. subtract, which accepts a String and a char and returns a string with all instances of the provided char removed + public string subtract(string str, char chr) + { + string newstr = str.Replace(chr.ToString(), null); + return newstr; + } //TODO: 5. multiply, which accepts two ints and returns an int (first int multiplied by second int) + public int multiply(int a, int b) + { + return a * b; + } //TODO: 6. multiply, which accepts a string and an int, and returns a string containing the provided string as many times as the provided int separated by a comma. E.g. multiply("Hello", 3) -> "Hello,Hello,Hello" - - //TODO: 7. multiply, which accepts an array of Strings that each contain a number, and an int. The method should return an array of ints that contain the value of multiplying each String number by the provided int E.g. multiply(["2", "7", "3"], 3) -> [6, 21, 9] + public string multiply(string a, int b) + { + string newstr = a; + for (int i = 0; i < b-1; i++) + { + newstr = newstr + "," + a; + } + + return newstr; + } + //TODO: 7. multiply, which accepts an array of Strings that each contain a number, and an int. The method should return an array of ints that contain the value of multiplying each String number by the provided int E.g. multiply(["2", "7", "3"], 3) -> [6, 21, 9] + public int[] multiply(string[] a, int b) + { + int[] lst = new int[a.Length]; + for (int i = 0; i < a.Length; i++) + { + lst[i] = int.Parse(a[i]) * b; + } + return lst; + } } } diff --git a/Boolean.CSharp.Main/Misc/Bicycle.cs b/Boolean.CSharp.Main/Misc/Bicycle.cs index f218c0e..74c7ac1 100644 --- a/Boolean.CSharp.Main/Misc/Bicycle.cs +++ b/Boolean.CSharp.Main/Misc/Bicycle.cs @@ -11,5 +11,16 @@ public class Bicycle private int _wheelCount; public int WheelCount { get; set; } + + + public Bicycle() + { + WheelCount = 2; + } + public Bicycle(int wheelCount) + { + WheelCount = wheelCount; + } } + } diff --git a/Boolean.CSharp.Main/Misc/Car.cs b/Boolean.CSharp.Main/Misc/Car.cs index d810f5f..2dde0d8 100644 --- a/Boolean.CSharp.Main/Misc/Car.cs +++ b/Boolean.CSharp.Main/Misc/Car.cs @@ -17,10 +17,10 @@ public Car() _model = string.Empty; _make = string.Empty; } - public Car(string Make) + public Car(string Make, string Model) { _make = Make; - _model = string.Empty; + _model = Model; } public string Make diff --git a/Boolean.CSharp.Main/Misc/Motorbike.cs b/Boolean.CSharp.Main/Misc/Motorbike.cs index 6fcf20b..3efe4fe 100644 --- a/Boolean.CSharp.Main/Misc/Motorbike.cs +++ b/Boolean.CSharp.Main/Misc/Motorbike.cs @@ -20,8 +20,9 @@ public Motorbike() _model = string.Empty; } - public Motorbike(string Make, string Model) + public Motorbike(int cc, string Make, string Model) { + _cc = cc; _make = Make; _model = Model; } diff --git a/Boolean.CSharp.Main/Misc/Unicycle.cs b/Boolean.CSharp.Main/Misc/Unicycle.cs index 461cb23..7effe35 100644 --- a/Boolean.CSharp.Main/Misc/Unicycle.cs +++ b/Boolean.CSharp.Main/Misc/Unicycle.cs @@ -12,5 +12,10 @@ public class Unicycle public string NameOfUnicyclist { get => _nameOfUnicyclist; set => _nameOfUnicyclist = value; } public int WheelCount { get; set; } = 1; + + public Unicycle(string name) + { + _nameOfUnicyclist = name; + } } } diff --git a/Boolean.CSharp.Test/ExtensionTests.cs b/Boolean.CSharp.Test/ExtensionTests.cs index f147497..ee313ee 100644 --- a/Boolean.CSharp.Test/ExtensionTests.cs +++ b/Boolean.CSharp.Test/ExtensionTests.cs @@ -63,6 +63,8 @@ public void TestQuestion4() Extension extension = new Extension(); string result = extension.subtract(source, z); + Console.WriteLine(result); + Assert.IsTrue(result == "the quick brown fox jumps over the lay dog"); }