From 5576ecf8750b43e8d2ee606199a1783e06266518 Mon Sep 17 00:00:00 2001 From: Santhia Date: Thu, 11 Jan 2024 11:16:38 +0100 Subject: [PATCH] Constructor exercise, done --- Boolean.CSharp.Main/Core.cs | 19 ++++----- Boolean.CSharp.Main/Extension.cs | 56 ++++++++++++++++++++++++--- Boolean.CSharp.Main/Misc/Bicycle.cs | 10 ++++- Boolean.CSharp.Main/Misc/Car.cs | 4 +- Boolean.CSharp.Main/Misc/Motorbike.cs | 11 ++++-- Boolean.CSharp.Main/Misc/Unicycle.cs | 5 +++ 6 files changed, 83 insertions(+), 22 deletions(-) diff --git a/Boolean.CSharp.Main/Core.cs b/Boolean.CSharp.Main/Core.cs index e09c536..18daa92 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 @@ -38,13 +38,13 @@ 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; @@ -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("Your Unique Name"); @@ -132,10 +132,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("Flight cancelled"); return plane; } diff --git a/Boolean.CSharp.Main/Extension.cs b/Boolean.CSharp.Main/Extension.cs index 80c1db6..c7da3a4 100644 --- a/Boolean.CSharp.Main/Extension.cs +++ b/Boolean.CSharp.Main/Extension.cs @@ -12,18 +12,62 @@ 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 numOne, float numTwo) + { + return numOne + numTwo; + } + //TODO: 2. add, which accepts two doubles and returns a double (both doubles added together) + public double add (double numOne, double numTwo) + { + return numOne + numTwo; + } - //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 numOne, float numTwo) + { + return numOne - numTwo; + } - //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 numOne, char numTwo) + { + return new string(numOne.Where(x => x != numTwo).ToArray()); + } //TODO: 5. multiply, which accepts two ints and returns an int (first int multiplied by second int) + public int multiply (int numOne, int numTwo) + { + return numOne * numTwo; + } + + //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 numOne, int numTwo) + { + return string.Join(",", Enumerable.Repeat(numOne, numTwo)); + } + + + //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[] arr, int num) + { + // Creating an array to store the results + int[] result = new int[arr.Length]; + + // Looping through each element in the input array + for (int i = 0; i < arr.Length; i++) + { + // Parsing the string as an integer and multiply by the input number + result[i] = int.Parse(arr[i]) * num; + } + + // Return the resulting array + return result; + } - //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] } } diff --git a/Boolean.CSharp.Main/Misc/Bicycle.cs b/Boolean.CSharp.Main/Misc/Bicycle.cs index f218c0e..cc56f1e 100644 --- a/Boolean.CSharp.Main/Misc/Bicycle.cs +++ b/Boolean.CSharp.Main/Misc/Bicycle.cs @@ -10,6 +10,14 @@ public class Bicycle { private int _wheelCount; - public int WheelCount { get; set; } + public Bicycle(int wheelCount) + { + _wheelCount = wheelCount; + } + + + public int WheelCount { get => _wheelCount; set => _wheelCount = value; } + } + } 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..58543dc 100644 --- a/Boolean.CSharp.Main/Misc/Motorbike.cs +++ b/Boolean.CSharp.Main/Misc/Motorbike.cs @@ -21,14 +21,17 @@ public Motorbike() _model = string.Empty; } public Motorbike(string Make, string Model) - { + { + _cc = 373; _make = Make; _model = Model; } - public string Make { get; } - public string Model { get; } - public int CC { get; } + public string Make { get => _make; } + public string Model { get => _model; } + public int CC { get => _cc; } + + } } diff --git a/Boolean.CSharp.Main/Misc/Unicycle.cs b/Boolean.CSharp.Main/Misc/Unicycle.cs index 461cb23..9fb80c1 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; + public Unicycle(string name) + { + _nameOfUnicyclist = name; + } + public string NameOfUnicyclist { get => _nameOfUnicyclist; set => _nameOfUnicyclist = value; } public int WheelCount { get; set; } = 1; }