Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions Boolean.CSharp.Main/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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..
Expand All @@ -29,21 +29,21 @@ 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
You can pass multiple variables in by placing a comma after the first, then the next variable
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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;

Expand All @@ -103,22 +103,37 @@ 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;

}

public Aeroplane Question5()
{

/*

Method overloading is possible too.
Expand All @@ -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;
}
Expand Down
55 changes: 40 additions & 15 deletions Boolean.CSharp.Main/Extension.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
7 changes: 6 additions & 1 deletion Boolean.CSharp.Main/Misc/Bicycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; } }
}
}
4 changes: 2 additions & 2 deletions Boolean.CSharp.Main/Misc/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
27 changes: 18 additions & 9 deletions Boolean.CSharp.Main/Misc/Motorbike.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@ public class Motorbike
{
private string _make;
private string _model;
private int _cc = 0;
private int _cc = 373;


public Motorbike()
{
_cc = 373;
_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; } }

}
}
4 changes: 4 additions & 0 deletions Boolean.CSharp.Main/Misc/Unicycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 10 additions & 27 deletions Boolean.CSharp.Test/ExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
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()
{
double a = 1.0;
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);
}
Expand All @@ -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);
}
Expand All @@ -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");
}
Expand All @@ -74,39 +61,35 @@ 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()
{
string source = "Hello";
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));


}

}
}