diff --git a/Boolean.CSharp.Main/Core.cs b/Boolean.CSharp.Main/Core.cs index e09c536..1d8d3df 100644 --- a/Boolean.CSharp.Main/Core.cs +++ b/Boolean.CSharp.Main/Core.cs @@ -11,7 +11,7 @@ public class Core { public Car Question1() { - + /* Examine the code in the Car class. There are 2 constructor methods, identified because they have the same name as the class which in this case is Car.. @@ -29,8 +29,8 @@ public Car(string Make) _model = string.Empty; } */ - - Car car = new Car("Volkswagen"); + + /* 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 @@ -38,15 +38,15 @@ Within the constructor the 'string Make' variable has scope within the construct e.g. In the Constructor signature: public Car(string Make, string Model) e.g. Instantiating: Car car = new Car("Volkswagen", "Beetle"); */ - + //TODO: 1. Change car instantiation code above, pass in the make AND model. //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 - + Car car = new Car("Volkswagen", "Beetle"); - return car; + return car; } @@ -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("KTM", "Duke", 373); 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(2); 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("Tein Schoemaker"); @@ -132,11 +132,10 @@ 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 + plane.FlightDetails(""); return plane; } diff --git a/Boolean.CSharp.Main/Extension.cs b/Boolean.CSharp.Main/Extension.cs index 80c1db6..f3649d1 100644 --- a/Boolean.CSharp.Main/Extension.cs +++ b/Boolean.CSharp.Main/Extension.cs @@ -12,18 +12,64 @@ 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) + 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 + public string Subtract(string _string, char _char) + { + return _string.Replace(_char.ToString(), ""); + } //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" - + public string Multiply(string a, int b) + { + string result= ""; + + for (int i = 0; i < b; i++) + { + result += a; + + if (i < b - 1) + { + 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[] a, int b) + { + + List result = new List(); + foreach (string num in a) + { + result.Add(int.Parse(num) * b); + } + return result.ToArray(); + } } } diff --git a/Boolean.CSharp.Main/Misc/Bicycle.cs b/Boolean.CSharp.Main/Misc/Bicycle.cs index f218c0e..5096f14 100644 --- a/Boolean.CSharp.Main/Misc/Bicycle.cs +++ b/Boolean.CSharp.Main/Misc/Bicycle.cs @@ -8,8 +8,12 @@ namespace Boolean.CSharp.Main.Misc { public class Bicycle { - private int _wheelCount; public int WheelCount { get; set; } + + 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..d4e871e 100644 --- a/Boolean.CSharp.Main/Misc/Motorbike.cs +++ b/Boolean.CSharp.Main/Misc/Motorbike.cs @@ -20,10 +20,11 @@ public Motorbike() _model = string.Empty; } - public Motorbike(string Make, string Model) + public Motorbike(string Make, string Model, int CC) { _make = Make; _model = Model; + _cc = CC; } public string Make @@ -37,6 +38,7 @@ public string Model public int CC { get { return _cc; } + set { _cc = value; } } } } diff --git a/Boolean.CSharp.Main/Misc/Unicycle.cs b/Boolean.CSharp.Main/Misc/Unicycle.cs index 461cb23..9aa7766 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 nameOfUnicyclist) + { + _nameOfUnicyclist = nameOfUnicyclist; + } } } diff --git a/Boolean.CSharp.Test/ExtensionTests.cs b/Boolean.CSharp.Test/ExtensionTests.cs index f147497..f3e3352 100644 --- a/Boolean.CSharp.Test/ExtensionTests.cs +++ b/Boolean.CSharp.Test/ExtensionTests.cs @@ -22,7 +22,7 @@ public void TestQuestion1() Extension extension = new Extension(); - float result = extension.add(a, b); + float result = extension.Add(a, b); Assert.AreEqual(3.0f, result); @@ -36,7 +36,7 @@ public void TestQuestion2() Extension extension = new Extension(); - double result = extension.add(a, b); + double result = extension.Add(a, b); Assert.AreEqual(3.0, result); } @@ -49,7 +49,7 @@ public void TestQuestion3() Extension extension = new Extension(); - float result = extension.subtract(a, b); + float result = extension.Subtract(a, b); Assert.AreEqual(1.0f, result); } @@ -62,7 +62,7 @@ public void TestQuestion4() Extension extension = new Extension(); - string result = extension.subtract(source, z); + string result = extension.Subtract(source, z); Assert.IsTrue(result == "the quick brown fox jumps over the lay dog"); } @@ -75,7 +75,7 @@ public void TestQuestion5() Extension extension = new Extension(); - int result = extension.multiply(a, b); + int result = extension.Multiply(a, b); Assert.AreEqual(10, result); } @@ -87,7 +87,7 @@ public void TestQuestion6() Extension extension = new Extension(); - string result = extension.multiply(source, 3); + string result = extension.Multiply(source, 3); Assert.IsTrue("Hello,Hello,Hello" == result); } @@ -99,7 +99,7 @@ public void TestQuestion7() int multiplier = 3; Extension extension = new Extension(); - var result = extension.multiply(source, multiplier); + var result = extension.Multiply(source, multiplier); int[] answer = { 6, 21, 9 };