From e789cedb049aea9eb986444b2092b27e76d4a883 Mon Sep 17 00:00:00 2001 From: Erlend Skutlaberg Date: Wed, 8 Jan 2025 11:57:09 +0100 Subject: [PATCH] finished exercises --- Boolean.CSharp.Main/Core.cs | 8 ++-- Boolean.CSharp.Main/Extension.cs | 63 ++++++++++++++++++++++++++- Boolean.CSharp.Main/Misc/Bicycle.cs | 5 +++ Boolean.CSharp.Main/Misc/Car.cs | 4 +- Boolean.CSharp.Main/Misc/Motorbike.cs | 1 + Boolean.CSharp.Main/Misc/Unicycle.cs | 6 +++ 6 files changed, 79 insertions(+), 8 deletions(-) diff --git a/Boolean.CSharp.Main/Core.cs b/Boolean.CSharp.Main/Core.cs index e09c536..77e2d81 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 @@ -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("Erlend Skutlaberg"); @@ -132,9 +132,9 @@ 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 - + plane.FlightDetails("The flight is canceled bro..."); //write code here return plane; diff --git a/Boolean.CSharp.Main/Extension.cs b/Boolean.CSharp.Main/Extension.cs index 80c1db6..d3ba984 100644 --- a/Boolean.CSharp.Main/Extension.cs +++ b/Boolean.CSharp.Main/Extension.cs @@ -12,18 +12,77 @@ 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) + //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 str, char chr) + { + string result = ""; + foreach (char c in str) + { + if (c != chr) + { + result += c; + } + } + + return result; + } //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 str, int i) + { + string answer = ""; + for (int j = 0; j < i; j++) + { + if (answer.Length > 0) + { + answer += ","; + } + answer += str; + + } + + + + return answer; + } + //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[] stringArray, int factor) + { + + int[] result = new int[stringArray.Length]; + for (int i = 0; i < stringArray.Length; i++) + { + string numstr = stringArray[i]; + int num = int.Parse(numstr) * factor; + result[i] = num; + } + return result; + } + } } diff --git a/Boolean.CSharp.Main/Misc/Bicycle.cs b/Boolean.CSharp.Main/Misc/Bicycle.cs index f218c0e..cc3485a 100644 --- a/Boolean.CSharp.Main/Misc/Bicycle.cs +++ b/Boolean.CSharp.Main/Misc/Bicycle.cs @@ -11,5 +11,10 @@ public class Bicycle private int _wheelCount; public int WheelCount { get; set; } + + public Bicycle() + { + WheelCount = 3; + } } } diff --git a/Boolean.CSharp.Main/Misc/Car.cs b/Boolean.CSharp.Main/Misc/Car.cs index d810f5f..5671839 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..700ad8a 100644 --- a/Boolean.CSharp.Main/Misc/Motorbike.cs +++ b/Boolean.CSharp.Main/Misc/Motorbike.cs @@ -22,6 +22,7 @@ public Motorbike() public Motorbike(string Make, string Model) { + _cc = 373; _make = Make; _model = Model; } diff --git a/Boolean.CSharp.Main/Misc/Unicycle.cs b/Boolean.CSharp.Main/Misc/Unicycle.cs index 461cb23..b3103de 100644 --- a/Boolean.CSharp.Main/Misc/Unicycle.cs +++ b/Boolean.CSharp.Main/Misc/Unicycle.cs @@ -12,5 +12,11 @@ public class Unicycle public string NameOfUnicyclist { get => _nameOfUnicyclist; set => _nameOfUnicyclist = value; } public int WheelCount { get; set; } = 1; + + + public Unicycle(string nameOfRider) + { + _nameOfUnicyclist = nameOfRider; + } } }