diff --git a/Session02/ConsoleApp1/Program.cs b/Session02/ConsoleApp1/Program.cs new file mode 100644 index 0000000..0d67984 --- /dev/null +++ b/Session02/ConsoleApp1/Program.cs @@ -0,0 +1,28 @@ +using System; +using System.Reflection.Metadata.Ecma335; +using System.Runtime.CompilerServices; + +namespace ConsoleApp1 +{ + class Program + { + static void Main(string[] args) + { + bool looping = true; + char answer = 'y'; + while (looping) + { + Console.WriteLine("Please enter an amount of time in seconds."); + int secondsInput = Convert.ToInt32(Console.ReadLine()); + int hours = secondsInput / 3600; + int secondsRemaining = secondsInput % 3600; + int minutes = secondsRemaining / 60; + secondsRemaining = secondsRemaining % 60; + Console.WriteLine(secondsInput + " seconds equals " + hours + " hours, " + minutes + " minutes and " + secondsRemaining + " seconds."); + Console.WriteLine("Would you like to continue? (y/n)"); + answer = Convert.ToChar(Console.ReadLine()); + if (answer == 'n') { looping = false; } + } + } + } +} \ No newline at end of file diff --git a/Session02/ConsoleApp1/Time Converter.csproj b/Session02/ConsoleApp1/Time Converter.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Session02/ConsoleApp1/Time Converter.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Session02/MirrorString/MirrorString.csproj b/Session02/MirrorString/MirrorString.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Session02/MirrorString/MirrorString.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Session02/MirrorString/Program.cs b/Session02/MirrorString/Program.cs new file mode 100644 index 0000000..bb26fa7 --- /dev/null +++ b/Session02/MirrorString/Program.cs @@ -0,0 +1,19 @@ +using System; + +namespace MirrorString +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Please enter a word or sentence."); + string input = Console.ReadLine(); + string reversion = ""; + for (int i = input.Length; i > 0; i--) + { + reversion = String.Concat(reversion, input.Substring(i-1, 1)); + } + Console.WriteLine(reversion); + } + } +} diff --git a/Session02/Session02.sln b/Session02/Session02.sln new file mode 100644 index 0000000..8365e37 --- /dev/null +++ b/Session02/Session02.sln @@ -0,0 +1,55 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30413.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Session02Excercise02", "Session02Excercise02\Session02Excercise02.csproj", "{1C4E07D7-7280-45F3-9766-E751166FC7AE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Session02Excercise1", "..\csharp-plus-2020\Session02\Session02\Session02Excercise1\Session02Excercise1.csproj", "{D45F4DF0-7B02-497A-B116-69F734DA6DBC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Session02Excercise03", "Session02Excercise03\Session02Excercise03.csproj", "{9944D6A5-AFDA-4C3D-9B53-3F873A0791C0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Session02Excercise04", "Session02Excercise04\Session02Excercise04.csproj", "{D8CF8C53-7DC3-41D7-90D3-3A296A96E840}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Time Converter", "ConsoleApp1\Time Converter.csproj", "{2F43B160-4A87-474A-8E47-3E55AC92C0CE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MirrorString", "MirrorString\MirrorString.csproj", "{328984CD-7137-4373-8DEF-BCE1276395D5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1C4E07D7-7280-45F3-9766-E751166FC7AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1C4E07D7-7280-45F3-9766-E751166FC7AE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1C4E07D7-7280-45F3-9766-E751166FC7AE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1C4E07D7-7280-45F3-9766-E751166FC7AE}.Release|Any CPU.Build.0 = Release|Any CPU + {D45F4DF0-7B02-497A-B116-69F734DA6DBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D45F4DF0-7B02-497A-B116-69F734DA6DBC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D45F4DF0-7B02-497A-B116-69F734DA6DBC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D45F4DF0-7B02-497A-B116-69F734DA6DBC}.Release|Any CPU.Build.0 = Release|Any CPU + {9944D6A5-AFDA-4C3D-9B53-3F873A0791C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9944D6A5-AFDA-4C3D-9B53-3F873A0791C0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9944D6A5-AFDA-4C3D-9B53-3F873A0791C0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9944D6A5-AFDA-4C3D-9B53-3F873A0791C0}.Release|Any CPU.Build.0 = Release|Any CPU + {D8CF8C53-7DC3-41D7-90D3-3A296A96E840}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D8CF8C53-7DC3-41D7-90D3-3A296A96E840}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D8CF8C53-7DC3-41D7-90D3-3A296A96E840}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D8CF8C53-7DC3-41D7-90D3-3A296A96E840}.Release|Any CPU.Build.0 = Release|Any CPU + {2F43B160-4A87-474A-8E47-3E55AC92C0CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2F43B160-4A87-474A-8E47-3E55AC92C0CE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2F43B160-4A87-474A-8E47-3E55AC92C0CE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2F43B160-4A87-474A-8E47-3E55AC92C0CE}.Release|Any CPU.Build.0 = Release|Any CPU + {328984CD-7137-4373-8DEF-BCE1276395D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {328984CD-7137-4373-8DEF-BCE1276395D5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {328984CD-7137-4373-8DEF-BCE1276395D5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {328984CD-7137-4373-8DEF-BCE1276395D5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {BDAE20EE-D5CF-4207-A2DB-FC294DC8247C} + EndGlobalSection +EndGlobal diff --git a/Session02/Session02Exercise01/Program.cs b/Session02/Session02Exercise01/Program.cs new file mode 100644 index 0000000..fdca5b3 --- /dev/null +++ b/Session02/Session02Exercise01/Program.cs @@ -0,0 +1,33 @@ +using System; + +namespace Session02Excercise1 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello scrubs"); + Console.WriteLine("Please enter a value:"); + + var integerValue = Convert.ToInt32(Console.ReadLine()); + + Console.WriteLine("Integer's value is: " + integerValue); + + string stringValue = "of Steel"; + + Console.WriteLine("The walls are made " + stringValue); + if (integerValue > 90) + { + Console.WriteLine(integerValue + " is over 90."); + } + else if (integerValue == 90){ + Console.WriteLine(integerValue + " is equal to 90."); + } + else + { + Console.WriteLine(integerValue + " is under 90."); + }//enter text here + } + + } +} diff --git a/Session02/Session02Exercise01/Session02Excercise1.csproj b/Session02/Session02Exercise01/Session02Excercise1.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Session02/Session02Exercise01/Session02Excercise1.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Session02/Session02Exercise02/Program.cs b/Session02/Session02Exercise02/Program.cs new file mode 100644 index 0000000..6d940e0 --- /dev/null +++ b/Session02/Session02Exercise02/Program.cs @@ -0,0 +1,41 @@ +using System; + +namespace Session02Excercise02 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Do you want to build a trebuchet? (y/n)"); + string answer = Console.ReadLine(); + + if (answer.ToLower() == "y") + { + Console.WriteLine("Awesome, let's get started..."); + } + else if (answer.ToLower() == "n") + { + Console.WriteLine("That's too bad. Maybe another time."); + } + else + { + Console.WriteLine("Input not recognized, please try again."); + } + int userInput = 0; + Console.WriteLine("If you want to escape the loop, guess the number between 1 and 50."); + while(userInput != 23) + { + userInput = Convert.ToInt32(Console.ReadLine()); + if (userInput < 23) + { + Console.WriteLine("Too low, try again."); + } + else if (userInput > 23) + { + Console.WriteLine("Too high, try again."); + } + } + Console.WriteLine("Congratulations, you've escaped the loop."); + } + } +} diff --git a/Session02/Session02Exercise02/Session02Excercise02.csproj b/Session02/Session02Exercise02/Session02Excercise02.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Session02/Session02Exercise02/Session02Excercise02.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Session02/Session02Exercise03/Program.cs b/Session02/Session02Exercise03/Program.cs new file mode 100644 index 0000000..8aaf907 --- /dev/null +++ b/Session02/Session02Exercise03/Program.cs @@ -0,0 +1,76 @@ +using System; + +namespace Session02Excercise03 +{ + class Program + { + static void Main(string[] args) + { + var additionResult = 1 + 2; + var incrementResult = additionResult++; + + Console.WriteLine("Addition Result: " + additionResult + " and Increment Result: " + incrementResult); + + var trueValue = true; + var falseValue = false; + var andResult = trueValue & falseValue; + var orResult = trueValue | falseValue; + var xorResult = trueValue ^ falseValue; + + Console.WriteLine("andResult is " + andResult + " while orResult is " + orResult + " and xorResult is " + xorResult); + + var moduloResult = 3 % 2; + Console.WriteLine("ModuloResult is " + moduloResult); + + int highInteger = 1000; + var divisionResult = highInteger / 3; + Console.WriteLine("divisionResult is " + divisionResult); + + int higherInteger = 10000; + var doubleDivisionResult = highInteger / 3.0; + Console.WriteLine("doubleDivisionResult is " + doubleDivisionResult); + + int forcedIntDivisionResult = (int)(highInteger / 3.0); + Console.WriteLine("forcedIntDivisionResult is equal to " + forcedIntDivisionResult); + + var conversionResult = Convert.ToInt32(doubleDivisionResult); + Console.WriteLine("conversionResult is " + conversionResult); + + var midpointDivisionResult = 1.0 / 2.0; + Console.WriteLine("midPointDivisionResult is " + midpointDivisionResult); + Console.WriteLine("castToInt " + ((int)(midpointDivisionResult)).ToString()); + Console.WriteLine("Math.Ceiling " + Math.Ceiling(midpointDivisionResult)); + Console.WriteLine("Math.Floor" + Math.Floor(midpointDivisionResult)); + Console.WriteLine("Math.Round" + Math.Round(midpointDivisionResult, 3)); + + Console.WriteLine("additionResult is currently: " + additionResult); + additionResult += 2; + Console.WriteLine("additionResult is currently: " + additionResult); + //Also, these work + additionResult -= 2; + Console.WriteLine("additionResult is currently: " + additionResult); + additionResult *= 2; + Console.WriteLine("additionResult is currently: " + additionResult); + additionResult /= 2; + Console.WriteLine("additionResult is currently: " + additionResult); + + var greaterResult = 5 > 3; + Console.WriteLine("Is five really greater than three? Of course it's " + greaterResult); + + var lessThanResult = 5 < 3; + Console.WriteLine("Is five less than three? You can bet that it is " + lessThanResult); + + //And there's two more + var greaterOrEqual = 3 >= 3; + var lessOrEqual = 5 <= 5; + Console.WriteLine("Is three greater or equal to three? It's " + greaterOrEqual + " And of course is five less than or equal to five? " + lessOrEqual); + + var andOperationResult = true && false; + var orOperationResult = false || false; + + Console.WriteLine("andOperationResult " + andOperationResult); + Console.WriteLine("orOperationResult" + orOperationResult); + + } + } +} diff --git a/Session02/Session02Exercise03/Session02Excercise03.csproj b/Session02/Session02Exercise03/Session02Excercise03.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Session02/Session02Exercise03/Session02Excercise03.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Session02/Session02Exercise04/Program.cs b/Session02/Session02Exercise04/Program.cs new file mode 100644 index 0000000..8b2e16b --- /dev/null +++ b/Session02/Session02Exercise04/Program.cs @@ -0,0 +1,66 @@ +using System; + +namespace Session02Excercise04 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Please enter an integer."); + var input = Console.ReadLine(); + var integer = Convert.ToInt32(input); + + + if (integer >= 18) + { + Console.WriteLine("The number you entered is greater than or equal to 18."); + } + else if (integer >= 10 && integer <18) + { + Console.WriteLine("The number you entered is between 10 and 17."); + } + else if (integer >= 7 && integer <10) + { + Console.WriteLine("The number you entered is between 7 and 9."); + } + else if (integer >= 2 && integer <7) + { + Console.WriteLine("The number you enetered is between 2 and 6."); + } + else + { + Console.WriteLine("The number you entered is less than 2."); + } + + + Console.WriteLine("Ange temperatur i grader C:"); + var temp = Console.ReadLine(); + var tempInt = Convert.ToInt32(temp); + string waterLabel; + + + if (tempInt >= 27) + { + waterLabel = "Går att bada"; + } + else { + waterLabel = "Fel temperatur."; + } + + Console.WriteLine(waterLabel); + //ALternativ + string waterLabelAlt = integer >= 27 ? "Går att bada" : "Går inte att bada"; + Console.WriteLine(waterLabelAlt); + + + switch (integer) + { + case 1: waterLabel = "Går inte att bada alls."; break; + case -3: waterLabel = "Det är 3 minusgrader."; break; + default: /* Vad som händer ifall inget annat träffar*/ break; + } + + + } + } +} diff --git a/Session02/Session02Exercise04/Session02Excercise04.csproj b/Session02/Session02Exercise04/Session02Excercise04.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Session02/Session02Exercise04/Session02Excercise04.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Session03/Exercise01/Program.cs b/Session03/Exercise01/Program.cs new file mode 100644 index 0000000..b5fb2e0 --- /dev/null +++ b/Session03/Exercise01/Program.cs @@ -0,0 +1,33 @@ +using System; + +namespace Session03Exercise01 +{ + class Program + { + static void Main(string[] args) + { + int[] integerValues = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + var integerValuesName = nameof(integerValues); + + for (var i = 0; i < integerValues.Length; i++) + { + var value = integerValues[i]; + Console.WriteLine($"Index {i} i arrayen {integerValuesName} har värdet: {value}."); + } + + var doWhileIndex = 0; + + do + { + var value = integerValuesName[doWhileIndex]; + Console.WriteLine($"Index {doWhileIndex} i arrayed {integerValuesName} "); + } + while (doWhileIndex < integerValues.Length); + { + var value = integerValues[doWhileIndex]; + + Console.WriteLine($"While Index {doWhileIndex} i arrayen {integerValuesName} har värdet {value}."); + } + } + } +} diff --git a/Session03/Exercise01/Session03Exercise01.csproj b/Session03/Exercise01/Session03Exercise01.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Session03/Exercise01/Session03Exercise01.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Session03/Exercise02/Program.cs b/Session03/Exercise02/Program.cs new file mode 100644 index 0000000..9177c44 --- /dev/null +++ b/Session03/Exercise02/Program.cs @@ -0,0 +1,20 @@ +using System; + +namespace Exercise02 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Ange ett antal siffror, separerade med kommatecken."); + var input = Console.ReadLine(); + + var inputArray = input.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); + + foreach(var number in inputArray) + { + Console.WriteLine("Värdet är: " + number); + } + } + } +} diff --git a/Session03/Exercise02/Session03Exercise02.csproj b/Session03/Exercise02/Session03Exercise02.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Session03/Exercise02/Session03Exercise02.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Session03/Exercise03/Program.cs b/Session03/Exercise03/Program.cs new file mode 100644 index 0000000..b34246e --- /dev/null +++ b/Session03/Exercise03/Program.cs @@ -0,0 +1,85 @@ + using System; +using System.Diagnostics; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection.Metadata.Ecma335; + +namespace Exercise03 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Ange ett antal siffror, separerade med kommatecken."); + var input = Console.ReadLine(); + var inputArray = input.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); + double[] numberArray = new double[inputArray.Length]; + + for (int i = 0; i < inputArray.Length; i++) + { + try + { + numberArray[i] = Convert.ToDouble(inputArray[i]); + + Debug.Assert(i < inputArray.Length - 1); + } + catch (Exception) + { + numberArray[i] = 0; + } + finally //kommer köras även om try/catch har gjort en break. Körs alltid. + { + + } + + + foreach (var number in numberArray) + { + Console.WriteLine("Värde: " + number.ToString()); + } + int exceptionStatus = GetExceptionStatus(); + } + } + static int GetExceptionStatus() + { + + int exceptionResult; + + try + { + throw new Exception("FEL"); + exceptionResult = 1; + return exceptionResult; + } + catch (Exception ex) + { + exceptionResult = ex.HResult; + return exceptionResult; + } + finally + { + exceptionResult = int.MaxValue; + + } + + static double getDoubleValue(string input); + { + try + { + var result = int.Parse(input); + + return result; + } + catch (FormatException ex) when (ex.Message.Contains("Input string")) + { + return input.MinValue; + } + catch (Exception ex) + { + return 0; + } + } + } + } +} + diff --git a/Session03/Exercise03/Session03Exercise03.csproj b/Session03/Exercise03/Session03Exercise03.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/Session03/Exercise03/Session03Exercise03.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/Session03/Session03.sln b/Session03/Session03.sln new file mode 100644 index 0000000..e4f26bf --- /dev/null +++ b/Session03/Session03.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30413.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Session03Exercise01", "Session03Exercise01\Session03Exercise01.csproj", "{4D080642-D8C2-4573-A87D-2516ED9F01AD}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Session03Exercise02", "Exercise02\Session03Exercise02.csproj", "{F5E18B8E-6063-4BE3-BC57-169CF774E8CC}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exercise03", "Exercise03\Exercise03.csproj", "{9DC89FCF-EB68-4956-B2B2-872B54419305}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4D080642-D8C2-4573-A87D-2516ED9F01AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4D080642-D8C2-4573-A87D-2516ED9F01AD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4D080642-D8C2-4573-A87D-2516ED9F01AD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4D080642-D8C2-4573-A87D-2516ED9F01AD}.Release|Any CPU.Build.0 = Release|Any CPU + {F5E18B8E-6063-4BE3-BC57-169CF774E8CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F5E18B8E-6063-4BE3-BC57-169CF774E8CC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F5E18B8E-6063-4BE3-BC57-169CF774E8CC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F5E18B8E-6063-4BE3-BC57-169CF774E8CC}.Release|Any CPU.Build.0 = Release|Any CPU + {9DC89FCF-EB68-4956-B2B2-872B54419305}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9DC89FCF-EB68-4956-B2B2-872B54419305}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9DC89FCF-EB68-4956-B2B2-872B54419305}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9DC89FCF-EB68-4956-B2B2-872B54419305}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7DFB8D31-8555-4C83-8F0B-CC5830F1F52E} + EndGlobalSection +EndGlobal