So I failed this coding technical exam. I am very confident with my answer. Please help I really wanted to improve. So what is wrong with this? I can’t find the issue with my code.
This is the Exam
Create a windows form application that has the following requirements in C#.
The program’s primary functionality is to sort a string with the input from a user.
Example input: befdac
Expected output: abcdef
The user interface will contain the following four elements:
A textbox that allows the user to input a string A control (for
instance a combo box) that selects the strategy to use for sorting the string.
A button that will sort the string using the selected strategy.
A label where the output will be shown (the sorted string)
The FOCUS of the assignment is on implementing the DESIGN PATTERNS.
a. Strategy pattern: is a software design pattern that enables an algorithm’s behavior to be selected at runtime. The strategy pattern will be used for sorting the strings in 2 different implementations. You can choose two of these sorting methods: Bubble Sort, Quicksort, or Merge Sort.
b. For the logic and view part of the application, it will be implemented in the Model-View-Controller pattern and you are not allowed to use a framework for this.
Make sure to follow proper development practices, and ensure that all validations are in place. Write unit tests for the individual components of your code to gain an advantage.
My Code
Interface
public interface ISortStrategy
{
string Sort(string input);
}
Controller
namespace YouSourceCodingExam.Controller
{
public class SortController
{
private readonly SortModel _model;
private ISortStrategy _strategy;
public SortController(SortModel model)
{
_model = model;
}
public void SetSortStrategy(ISortStrategy strategy)
{
_strategy = strategy;
}
public void Sort()
{
if (_strategy != null)
{
_model.Sorted = _strategy.Sort(_model.Input);
}
}
public void UpdateModel(string input)
{
_model.Input = input;
}
public string GetSortedString()
{
return _model.Sorted;
}
}
}
Model
public class SortModel
{
public string Input { get; set; }
public string Sorted { get; set; }
}
Concrete Strategies
public class BubbleSortStrategy : ISortStrategy
{
//BUBBLE SORT STRATEGY
public string Sort(string input)
{
//Repeatedly steps through the list, compares elements, and swaps them if they are
//in the wrong order.This process is repeated until the list is sorted.
if (string.IsNullOrEmpty(input)) return input;
char[] str = input.ToCharArray();
for (int i = 0; i < str.Length - 1; i++)
{
for (int j = 0; j < str.Length - i - 1; j++)
{
if (str[j] > str[j + 1])
{
// Swap str[j] and str[j+1]
char t = str[j];
str[j] = str[j + 1];
str[j + 1] = t;
}
}
}
return new string(str);
//OR USING LINQ
var sortedChars = input.OrderBy(c => c);
return new string(sortedChars.ToArray());
}
}
public class QuickSortStrategy : ISortStrategy
{
//QUICK SORT STRATEGY
public string Sort(string input)
{
if (input == null)
throw new ArgumentNullException(nameof(input), "Input cannot be null.");
if (input.Length == 0)
return input;
char[] str = input.ToCharArray();
QSort(str, 0, str.Length - 1);
return new string(str);
}
private void QSort(char[] str, int l, int h)
{
if (l < h)
{
int pi = Split(str, l, h);
//Iterate repetitively before spllitting
QSort(str, l, pi - 1);
QSort(str, pi + 1, h);
}
}
//Method to split or partition the string array
private int Split(char[] str, int l, int h)
{
char p = str[h];
int i = (l - 1); //smaller element
for (int j = l; j < h; j++)
{
//if current is smaller than or equal to p
if (str[j] < p)
{
i++;
char t = str[i];
str[i] = str[j];
str[j] = t;
}
}
char s = str[i + 1];
str[i + 1] = str[h];
str[h] = s;
return i + 1;
}
}
View
public partial class MainForm : Form
{
private readonly SortModel _model;
private readonly SortController _controller;
public MainForm()
{
InitializeComponent();
_model = new SortModel();
_controller = new SortController(_model);
// Populate ComboBox with sorting strategies
comboBoxSortStrategy.Items.Add("Bubble Sort");
comboBoxSortStrategy.Items.Add("Quick Sort");
comboBoxSortStrategy.SelectedIndex = 0;
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void buttonSort_Click(object sender, EventArgs e)
{
string input = textBoxInput.Text;
// Validate the input
if (string.IsNullOrEmpty(input))
{
MessageBox.Show("Input string cannot be null or empty.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_controller.UpdateModel(input);
// Set the sorting strategy based on ComboBox selection
ISortStrategy strategy;
string selectedStrategy = comboBoxSortStrategy.SelectedItem.ToString();
if (selectedStrategy == "Bubble Sort")
{
strategy = new BubbleSortStrategy();
}
else if (selectedStrategy == "Quick Sort")
{
strategy = new QuickSortStrategy();
}
else
{
throw new InvalidOperationException("Invalid sorting strategy selected");
}
_controller.SetSortStrategy(strategy);
_controller.Sort();
labelOutput.Text = _controller.GetSortedString();
}
private void comboBoxSortStrategy_SelectedIndexChanged(object sender, EventArgs e)
{
labelOutput.Text = "";
textBoxInput.Text = "";
}
}
Nate 18 years ago is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3