1. Refactor the following code (C#) to use polymorphism instead of if-else. Please provide your answer in C#, Java or C++. Alternatively for partial points, you could describe the type of change using UML to aid your response.
public class Calculator
{
public int Calculate(int a, int b, Operation operation)
{
if (operation.Type == "add")
{
return a + b;
}
else if (operation.Type == "subtract")
{
return a - b;
}
else if (operation.Type == "multiply")
{
return a * b;
}
else
{
throw new Exception("Unknown operation type");
}
}
}
public class Operation
{
public string Type { get; set; }
}
My Initial Answer:
publicclassCalculator
{
publicint NumberA { get;set;}
publicint NumberB { get; set; }
publicvirtuallong Calculate(int a, int b);
}
publicclassAddCalculator : Calculator
{
publicint NumberA { get; set; }
publicint NumberB { get; set; }
publicoverridelong Calculate(int a, int b)
{
return NumberA + NumberB;
}
}
publicclassMinusCalculator : Calculator
{
publicint NumberA { get; set; }
publicint NumberB { get; set; }
publicoverridelong Calculate(int a, int b)
{
return NumberA - NumberB;
}
}
publicclassMultiplyCalculator : Calculator
{
publicint NumberA { get; set; }
publicint NumberB { get; set; }
publicoverridelong Calculate(int a, int b)
{
return NumberA * NumberB;
}
}
2. The following two tables contain data relating to stock holdings and stock prices respectively:
How would you manipulate this data in order to return the total value (Units * Price) of the client holdings in each stock (you can use SQL, Excel functions or any other means to answer this)?
My Initial Answer:
Select StockHoldings.Units as x, StockPrice.PrizeNZD as y, sum(x*y) from StockHoldings join StockPrice on StockHoldings.StockCode= StockPrice. StockCode group by StockHoldings.ClientID
3. List as many faults with the following code as you can find
Public Class ClassRubbish
Public Function ReallyRubbishFunction(clientRecords As List)
Dim i, j As Decimal
If clientRecords.Count <= 0 Then
clientRecords.MoveFirst()
Do Until clientRecords.AtEnd()
Console.WriteLine("I've got another clientrecord!")
i = i + 1
''' following line of code commented out by Jason 5/02/2014
'''clientRecords.MoveNext()
Loop
End If
Console.WriteLine("Total number of client records" & 5)
End Function
End Class
4. The following code could be simplified / consolidated. Rewrite it in a more sensible way:
void CalculateTaxAndRewardPoints()
{
if (state == TEXAS)
{
rate = TX_RATE;
amt = baseRate * TX_RATE;
calc = 2 * basis(amt) + extra(amt) * 1.05;
}
else if ((state == OHIO) || (state == MAINE))
{
if (state == OHIO)
rate = OH_RATE;
else
rate = MN_RATE;
amt = baseRate * rate;
calc = 2 * basis(amt) + extra(amt) * 1.05;
if (state == OHIO)
points = 2;
}
else
{
rate = 1;
amt = baseRate;
calc = 2 * basis(amt) + extra(amt) * 1.05;
}
}
5. Write some code in any programming language (please specify which) that accepts an integer variable and returns a string variable containing the word “FIZZ” if the integer is a multiple of 3, “BUZZ” if it is a multiple of 5, “FIZZBUZZ” if it is a multiple of both 3 and 5, or “REJECT” if it is a multiple of neither 3 nor 5.