diff --git a/Boolean.CSharp.Main/Core.cs b/Boolean.CSharp.Main/Core.cs index e09c536..e11ffdd 100644 --- a/Boolean.CSharp.Main/Core.cs +++ b/Boolean.CSharp.Main/Core.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; using Boolean.CSharp.Main.Misc; @@ -30,7 +31,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 @@ -43,10 +44,7 @@ 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; + return car; } @@ -92,7 +90,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(4); return bike; @@ -104,11 +102,13 @@ public Unicycle Question4() //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 + //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(); + //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("Rose"); @@ -132,12 +132,14 @@ 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 + //TODO: 5. Call the FlightDetails method that sets the cancelled message and + //cancel the flight + + //write code here + plane.FlightDetails("Your flight is canceled!"); return plane; + } } diff --git a/Boolean.CSharp.Main/Extension.cs b/Boolean.CSharp.Main/Extension.cs index 80c1db6..2cba9cd 100644 --- a/Boolean.CSharp.Main/Extension.cs +++ b/Boolean.CSharp.Main/Extension.cs @@ -2,7 +2,9 @@ +using System.ComponentModel; using System.Reflection.Metadata.Ecma335; +using System.Text; namespace Boolean.CSharp.Main { @@ -12,18 +14,82 @@ 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 value1, float value2) + { + return value1 + value2; + } //TODO: 2. add, which accepts two doubles and returns a double (both doubles added together) + public double add (double value1, double value2) + { + return value1 + value2; + } //TODO: 3. subtract, which accepts two floats and returns a float (first float minus second float) + public float substract(float value1, float value2) + { + return value1 - value2; + } - //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 substract(string string1, char char1) + { + // what method to use to remove the char from the given string input. + // create a new StringBuilder + StringBuilder sb = new StringBuilder(); + foreach(char character in string1) + { + if(character != char1) + { + sb.Append(character); + } + } + return sb.ToString(); + + //throw new NotImplementedException(); + + //return value1 - value2; + } //TODO: 5. multiply, which accepts two ints and returns an int (first int multiplied by second int) + public int multiply (int value1, int value2) + { + return value1 * value2; + } - //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] + //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 strWord, int multiplier) { + + StringBuilder sb = new StringBuilder(); + for(int i = 0; i < multiplier; i++) { + sb.Append("Hello,"); + } + string result = sb.ToString(); + //throw new NotImplementedException(); + // repeat the string in the given int value input + // remove the comma before returning + return result.Remove(result.Length - 1); + } + //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[] strNumber, int multiplier) + { + // string Array parse to int. + // for loop -> in + int[] result = new int[strNumber.Length]; + for(int i = 0; i < strNumber.Length ; i++) + { + //int value2 = int.Parse(value2[i]); + // the index of the new array result[i] is the int index of the Parsed string[] source[i] + // multiplied with the int multiplier + int strNumberToMultiply = int.Parse(strNumber[i]); + result[i] = strNumberToMultiply * multiplier; + + } + return result; + } } } diff --git a/Boolean.CSharp.Main/Misc/Aeroplane.cs b/Boolean.CSharp.Main/Misc/Aeroplane.cs index d99d1b0..f968056 100644 --- a/Boolean.CSharp.Main/Misc/Aeroplane.cs +++ b/Boolean.CSharp.Main/Misc/Aeroplane.cs @@ -13,10 +13,12 @@ public class Aeroplane private string _journeyDetails; private bool _flightCancelled; + // empty constructor public Aeroplane() { } + // constructor with two parameters public bool FlightDetails(string departureAirport, string arrivalAirport) { _departureAirport = departureAirport; @@ -42,12 +44,11 @@ public int FlightDetails(AeroplanePassengerManifest list) { throw new NotImplementedException(); } - - - - public bool IsFlightCancelled => _flightCancelled; - - - + + + + public bool IsFlightCancelled => _flightCancelled; + } + } diff --git a/Boolean.CSharp.Main/Misc/Bicycle.cs b/Boolean.CSharp.Main/Misc/Bicycle.cs index f218c0e..8a4b417 100644 --- a/Boolean.CSharp.Main/Misc/Bicycle.cs +++ b/Boolean.CSharp.Main/Misc/Bicycle.cs @@ -10,6 +10,11 @@ public class Bicycle { private int _wheelCount; - public int WheelCount { get; set; } + public Bicycle(int wheelCount) + { + _wheelCount = wheelCount; + } + + public int WheelCount { get => _wheelCount ; } } } diff --git a/Boolean.CSharp.Main/Misc/Car.cs b/Boolean.CSharp.Main/Misc/Car.cs index b50f8f7..6943935 100644 --- a/Boolean.CSharp.Main/Misc/Car.cs +++ b/Boolean.CSharp.Main/Misc/Car.cs @@ -7,7 +7,7 @@ namespace Boolean.CSharp.Main.Misc { - public class Car + public class Car { private string _model; private string _make; @@ -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..f723b25 100644 --- a/Boolean.CSharp.Main/Misc/Motorbike.cs +++ b/Boolean.CSharp.Main/Misc/Motorbike.cs @@ -12,7 +12,9 @@ public class Motorbike private string _make; private string _model; private int _cc = 0; - + + + public Motorbike() { @@ -20,15 +22,18 @@ public Motorbike() _make = string.Empty; _model = string.Empty; } + public Motorbike(string Make, string Model) { _make = Make; - _model = Model; + _model = Model; + _cc = 373; } public string Make { get; } public string Model { get; } - public int CC { get; } + // the private int _cc will be the public int CC + public int CC { get => _cc; } } } diff --git a/Boolean.CSharp.Main/Misc/Unicycle.cs b/Boolean.CSharp.Main/Misc/Unicycle.cs index 461cb23..fb73eea 100644 --- a/Boolean.CSharp.Main/Misc/Unicycle.cs +++ b/Boolean.CSharp.Main/Misc/Unicycle.cs @@ -10,6 +10,11 @@ public class Unicycle { private string _nameOfUnicyclist; + // constructor that accepts the name of the unicyclist + public Unicycle(string uniCyclist) { + _nameOfUnicyclist = uniCyclist; + } + 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..623d77e 100644 --- a/Boolean.CSharp.Test/ExtensionTests.cs +++ b/Boolean.CSharp.Test/ExtensionTests.cs @@ -40,6 +40,7 @@ public void TestQuestion2() Assert.AreEqual(3.0, result); } + [Test] public void TestQuestion3() @@ -49,11 +50,12 @@ public void TestQuestion3() Extension extension = new Extension(); - float result = extension.subtract(a, b); + float result = extension.substract(a, b); Assert.AreEqual(1.0f, result); - } + } + [Test] public void TestQuestion4() { @@ -62,10 +64,11 @@ public void TestQuestion4() Extension extension = new Extension(); - string result = extension.subtract(source, z); + string result = extension.substract(source, z); Assert.IsTrue(result == "the quick brown fox jumps over the lay dog"); } + [Test] public void TestQuestion5() @@ -79,6 +82,8 @@ public void TestQuestion5() Assert.AreEqual(10, result); } + + [Test] public void TestQuestion6() { @@ -87,10 +92,12 @@ 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); } + + [Test] public void TestQuestion7() { @@ -107,6 +114,7 @@ public void TestQuestion7() } + } }