From 061127e8d268a35267238357155cab1c7300a22f Mon Sep 17 00:00:00 2001 From: Isabell Date: Wed, 6 Aug 2025 14:45:01 +0200 Subject: [PATCH] All tests done --- Boolean.CSharp.Main/Core.cs | 17 ++++---- Boolean.CSharp.Main/Extension.cs | 61 ++++++++++++++++++++++++--- Boolean.CSharp.Main/Misc/Bicycle.cs | 8 +++- Boolean.CSharp.Main/Misc/Car.cs | 4 +- Boolean.CSharp.Main/Misc/Motorbike.cs | 1 + Boolean.CSharp.Test/ExtensionTests.cs | 4 ++ 6 files changed, 77 insertions(+), 18 deletions(-) diff --git a/Boolean.CSharp.Main/Core.cs b/Boolean.CSharp.Main/Core.cs index e09c536..1971d33 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", "Bettle"); /* 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 @@ -43,9 +43,6 @@ Within the constructor the 'string Make' variable has scope within the construct //TIP if you click on the Car class name above, right click and then select 'Go to Definition' it'll take you straight to the code - - - return car; } @@ -76,6 +73,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"); + myMotorbike.CC = 373; if(myMotorbike.CC > 0) { @@ -93,6 +91,7 @@ public Bicycle Question3() //but no constructor to set this //TODO: 3. Add a constructor to the Bicycle class that populates the _wheelCount variable Bicycle bike = new Bicycle(); + bike.WheelCount = 2; return bike; @@ -109,9 +108,8 @@ 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.NameOfUnicyclist = "Isabell"; - - return unicycle; } @@ -132,10 +130,11 @@ 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 - - //write code here + + //write code here ' + plane.FlightDetails("Cancelled due weather"); return plane; } diff --git a/Boolean.CSharp.Main/Extension.cs b/Boolean.CSharp.Main/Extension.cs index 80c1db6..c42172c 100644 --- a/Boolean.CSharp.Main/Extension.cs +++ b/Boolean.CSharp.Main/Extension.cs @@ -2,7 +2,11 @@ +using Boolean.CSharp.Main.Misc; +using NUnit.Framework.Internal.Execution; using System.Reflection.Metadata.Ecma335; +using System.Security.Cryptography.X509Certificates; +using System.Xml.Linq; namespace Boolean.CSharp.Main { @@ -12,18 +16,65 @@ 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 f1, float f2) + { + return f1 + f2; + } //TODO: 2. add, which accepts two doubles and returns a double (both doubles added together) + public double add(double d1, double d2) + { + return d1 + d2; - //TODO: 3. subtract, which accepts two floats and returns a float (first float minus second float) + } - //TODO: 4. subtract, which accepts a String and a char and returns a string with all instances of the provided char removed + //TODO: 3. subtract, which accepts two floats and returns a float (first float minus second float) + public float subtract(float f1, float f2) + { + return f1 - f2; + + } + + //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 s, char c) + { + return s.Replace(c.ToString(), ""); + + } //TODO: 5. multiply, which accepts two ints and returns an int (first int multiplied by second int) + public int multiply(int i1, int i2) + { + return i1 * i2; + } //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 s, int i2) + { + string result = ""; + for (int i = 0; i < i2; i++) { + result = String.Concat(result, s); + + if (i < i2 - 1) + { + result = String.Concat(result, ","); + } + } + return result; + } + + //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[] s, int i2) + { + int[] newArray = new int[s.Length]; + for (int i = 0; i < s.Length; i++) + { + int number = int.Parse(s[i]); + newArray[i] = number * i2; + } + + return newArray; + } } -} +} \ No newline at end of file diff --git a/Boolean.CSharp.Main/Misc/Bicycle.cs b/Boolean.CSharp.Main/Misc/Bicycle.cs index f218c0e..da83fc8 100644 --- a/Boolean.CSharp.Main/Misc/Bicycle.cs +++ b/Boolean.CSharp.Main/Misc/Bicycle.cs @@ -10,6 +10,10 @@ public class Bicycle { private int _wheelCount; - public int WheelCount { get; set; } + public int WheelCount + { + get { return _wheelCount; } + set { _wheelCount = value; } + } } -} +} \ No newline at end of file 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..7586d12 100644 --- a/Boolean.CSharp.Main/Misc/Motorbike.cs +++ b/Boolean.CSharp.Main/Misc/Motorbike.cs @@ -37,6 +37,7 @@ public string Model public int CC { get { return _cc; } + set { _cc = value; } } } } diff --git a/Boolean.CSharp.Test/ExtensionTests.cs b/Boolean.CSharp.Test/ExtensionTests.cs index f147497..8aacc8d 100644 --- a/Boolean.CSharp.Test/ExtensionTests.cs +++ b/Boolean.CSharp.Test/ExtensionTests.cs @@ -89,6 +89,8 @@ public void TestQuestion6() string result = extension.multiply(source, 3); + Console.Write(result); + Assert.IsTrue("Hello,Hello,Hello" == result); } [Test] @@ -103,6 +105,8 @@ public void TestQuestion7() int[] answer = { 6, 21, 9 }; + Console.Write("Result is " + result); + Assert.IsTrue(result.SequenceEqual(answer));