diff --git a/Boolean.CSharp.Main/Core.cs b/Boolean.CSharp.Main/Core.cs index e09c536..e55f899 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"); + + 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 @@ -38,12 +38,12 @@ 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 - + return car; @@ -75,9 +75,9 @@ 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", '1'); - if(myMotorbike.CC > 0) + if (myMotorbike.CC > 0) { return true; } @@ -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; @@ -103,14 +103,29 @@ public Unicycle Question4() //A unicycle is a bike with one wheel //Note the wheelcount has been assigned on the class's property in this case. //Also note we are instantiating the class below even though there is no constructor on the class?! - + //TODO: 3. Add a constructor to the Unicycle class to accept/store the rider name and instantiate with your name below - + //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(); - - + // public Unicycle(string riderName) + //: this() // Calls the default constructor to ensure wheel count is set + //{ + // _nameOfUnicyclist = riderName; + // } + + // Other members and methods in the class... + + // Getter for the rider name + // public string GetNameOfUnicyclist() + //{ + // return _nameOfUnicyclist; + //} + + Unicycle unicycle = new Unicycle("Henrik"); + //string riderName = unicycle.GetNameOfUnicyclist(); + + return unicycle; @@ -118,7 +133,7 @@ public Unicycle Question4() public Aeroplane Question5() { - + /* Method overloading is possible too. @@ -130,12 +145,12 @@ What are the parameters and types? Overloading is simply multiple methods that have the same name but do different things */ - Aeroplane plane = new Aeroplane(); + 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("Flight cancelled"); + //write code here return plane; } diff --git a/Boolean.CSharp.Main/Extension.cs b/Boolean.CSharp.Main/Extension.cs index 80c1db6..e432135 100644 --- a/Boolean.CSharp.Main/Extension.cs +++ b/Boolean.CSharp.Main/Extension.cs @@ -1,29 +1,54 @@  - - using System.Reflection.Metadata.Ecma335; namespace Boolean.CSharp.Main { public class Extension - { - - //Implement the following methods: - + { //TODO: 1. add, which accepts two floats and returns a float (both floats added together) + public float AddTwoFloats(float number1, float number2) + { + return number1 + number2; + } //TODO: 2. add, which accepts two doubles and returns a double (both doubles added together) - - //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 + public double AddTwoDoubles(double number1, double number2) + { + return number1 + number2; + } + + //TODO: 3. subtract, which accepts two floats and returns a float (first float minus second float) + public float SubtractTwoFloats(float number1, float number2) + { + return number1 - number2; + } + + //TODO: 4. subtract, which accepts a String and a char and returns a string with all instances of the provided char removed + public string SubtractCharFromString(string source, char charToRemove) + { + return source.Replace(charToRemove.ToString(), string.Empty); + } //TODO: 5. multiply, which accepts two ints and returns an int (first int multiplied by second int) - - //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 int MultiplyTwoInts(int number1, int number2) + { + return number1 * number2; + } + + //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 MultiplyStringWithComma(string inputString, int count) + { + return string.Join(",", Enumerable.Repeat(inputString, count)); + } + + //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[] MultiplyStringArrayWithInt(string[] stringArray, int multiplier) + { + return stringArray.Select(s => int.Parse(s) * multiplier).ToArray(); + } } } diff --git a/Boolean.CSharp.Main/Misc/Bicycle.cs b/Boolean.CSharp.Main/Misc/Bicycle.cs index f218c0e..72db424 100644 --- a/Boolean.CSharp.Main/Misc/Bicycle.cs +++ b/Boolean.CSharp.Main/Misc/Bicycle.cs @@ -9,7 +9,12 @@ namespace Boolean.CSharp.Main.Misc public class Bicycle { private int _wheelCount; + public Bicycle(int wheelcount) + { + _wheelCount = wheelcount; - public int WheelCount { get; set; } + } + + public int WheelCount { get { return _wheelCount; } } } } diff --git a/Boolean.CSharp.Main/Misc/Car.cs b/Boolean.CSharp.Main/Misc/Car.cs index b50f8f7..bffa467 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 { get => _make; set => _make = value; } diff --git a/Boolean.CSharp.Main/Misc/Motorbike.cs b/Boolean.CSharp.Main/Misc/Motorbike.cs index b57180f..612698c 100644 --- a/Boolean.CSharp.Main/Misc/Motorbike.cs +++ b/Boolean.CSharp.Main/Misc/Motorbike.cs @@ -11,8 +11,8 @@ public class Motorbike { private string _make; private string _model; - private int _cc = 0; - + private int _cc = 373; + public Motorbike() { @@ -20,15 +20,24 @@ public Motorbike() _make = string.Empty; _model = string.Empty; } - public Motorbike(string Make, string Model) - { + public Motorbike(string Make, string Model, int CC) + { _make = Make; - _model = Model; - + _model = Model; + _cc = CC; + + } + public string Make + { + get + { return _make; } + } + public string Model + { + get + { return _model; } } - public string Make { get; } - public string Model { get; } - public int CC { get; } + public int CC { get { return _cc; } } } } diff --git a/Boolean.CSharp.Main/Misc/Unicycle.cs b/Boolean.CSharp.Main/Misc/Unicycle.cs index 461cb23..823c2c9 100644 --- a/Boolean.CSharp.Main/Misc/Unicycle.cs +++ b/Boolean.CSharp.Main/Misc/Unicycle.cs @@ -9,6 +9,10 @@ namespace Boolean.CSharp.Main.Misc public class Unicycle { private string _nameOfUnicyclist; + public Unicycle(string nameOfUnicyclist) + { + _nameOfUnicyclist = nameOfUnicyclist; + } public string NameOfUnicyclist { get => _nameOfUnicyclist; set => _nameOfUnicyclist = value; } public int WheelCount { get; set; } = 1; diff --git a/Boolean.CSharp.Test/ExtensionTests.cs b/Boolean.CSharp.Test/ExtensionTests.cs index f147497..2e65303 100644 --- a/Boolean.CSharp.Test/ExtensionTests.cs +++ b/Boolean.CSharp.Test/ExtensionTests.cs @@ -1,33 +1,23 @@ using Boolean.CSharp.Main; using NUnit.Framework; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Boolean.CSharp.Test { [TestFixture] public class ExtensionTests { - - [Test] public void TestQuestion1() { - float a = 1.0f; float b = 2.0f; Extension extension = new Extension(); - - float result = extension.add(a, b); + float result = extension.AddTwoFloats(a, b); Assert.AreEqual(3.0f, result); - - } + [Test] public void TestQuestion2() { @@ -35,8 +25,7 @@ public void TestQuestion2() double b = 2.0; Extension extension = new Extension(); - - double result = extension.add(a, b); + double result = extension.AddTwoDoubles(a, b); Assert.AreEqual(3.0, result); } @@ -48,8 +37,7 @@ public void TestQuestion3() float b = 1.0f; Extension extension = new Extension(); - - float result = extension.subtract(a, b); + float result = extension.SubtractTwoFloats(a, b); Assert.AreEqual(1.0f, result); } @@ -61,8 +49,7 @@ public void TestQuestion4() char z = 'z'; Extension extension = new Extension(); - - string result = extension.subtract(source, z); + string result = extension.SubtractCharFromString(source, z); Assert.IsTrue(result == "the quick brown fox jumps over the lay dog"); } @@ -74,11 +61,11 @@ public void TestQuestion5() int b = 5; Extension extension = new Extension(); - - int result = extension.multiply(a, b); + int result = extension.MultiplyTwoInts(a, b); Assert.AreEqual(10, result); } + [Test] public void TestQuestion6() { @@ -86,27 +73,23 @@ public void TestQuestion6() int dupes = 3; Extension extension = new Extension(); - - string result = extension.multiply(source, 3); + string result = extension.MultiplyStringWithComma(source, 3); Assert.IsTrue("Hello,Hello,Hello" == result); } + [Test] public void TestQuestion7() { - //multiply([], 3) -> [6, 21, 9] string[] source = { "2", "7", "3" }; int multiplier = 3; Extension extension = new Extension(); - var result = extension.multiply(source, multiplier); + var result = extension.MultiplyStringArrayWithInt(source, multiplier); int[] answer = { 6, 21, 9 }; Assert.IsTrue(result.SequenceEqual(answer)); - - } - } }