Help me please, that's really important and urgent

Signaler
Messages postés
5
Date d'inscription
samedi 6 février 2010
Statut
Membre
Dernière intervention
12 mars 2010
-
Messages postés
5
Date d'inscription
samedi 6 février 2010
Statut
Membre
Dernière intervention
12 mars 2010
-
Hello, I have a test that I have to complete for tomorrow.
I'm not really experienced but I need to pass this test to get a job, that's really important since I'm unemployed. Many thanks in advance.
The problem is:

Review the C# module rainbow.cs and comment on:

1. Design errors
2. Implementation errors
3. Limitations on use
4. Possible improvements
5. Possible alternative designs


Code:

/// <File header>
/// Tactiq Ltd


/// <Client>Spectrum Ltd</Client>
/// <Copyright>© Copyright Tactiq Ltd 2008</Copyright>
/// <Version>$Id: Rainbow.cs 100 2008-07-23 15:09:42Z jonest $</Version>
/// Evaluation Source Materials


/// <Description>
/// Rainbow class to handle rainbow colours.
/// </Description>
/// <Comment>
/// Derived from rainbow.c
/// </Comment>
/// T Jones
/// </File header>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rainbow
{
class Rainbow
{
// Colour indicies
public const uint RED = 1;
public const uint ORANGE = 2;
public const uint YELLOW = 3;
public const uint GREEN = 4;
public const uint BLUE = 5;
public const uint INDIGO = 6;
public const uint VIOLET = 7;

// Colour strings
public const string RED_STR = "red";
public const string ORANGE_STR = "orange";
public const string YELLOW_STR = "yellow";
public const string GREEN_STR = "green";
public const string BLUE_STR = "blue";
public const string INDIGO_STR = "indigo";
public const string VIOLET_STR = "violet";

private struct Colours
{
public int[] Cols;
public string[] ColsStr;
}

private Colours colours;

/// <summary>
/// Create a Rainbow.
/// </summary>
public Rainbow()
{
colours.Cols = new int[7];
colours.ColsStr = new string[7];

// Initialise the colours.
colours.ColsStr[0] = RED_STR;
colours.ColsStr[1] = ORANGE_STR;
colours.ColsStr[2] = YELLOW_STR;
colours.ColsStr[3] = GREEN_STR;
colours.ColsStr[4] = BLUE_STR;
colours.ColsStr[5] = INDIGO_STR;
colours.ColsStr[6] = VIOLET_STR;
}

/// <summary>
/// Gets the name associated with a rainbow colour
/// </summary>
/// Index of rainbow colour.


/// <returns></returns>
public string GetColour(uint col)
{
string str = "";
int idx;

str = String.Copy(colours.ColsStr[col]);

return str;
}

/// <summary>
/// Gets the name associated with a rainbow colour
/// </summary>
/// Index of rainbow colour.


/// <returns></returns>
public uint GetColour(string colStr)
{
int idx;

for (idx = RED; idx <= VIOLET; idx++)
{
if (colours.ColsStr[idx].CompareTo(colStr) == 0)
{
return idx;
}
}
}

}

class Program
{
/// <summary>
/// Test the Rainbow public methods.
/// </summary>
///


static void Main(string[] args)
{
Rainbow rainbow = new Rainbow();

Console.WriteLine("rainbow.GetColour(0): " +
rainbow.GetColour(0));
Console.WriteLine("rainbow.GetColour(RED): " +
rainbow.GetColour(Rainbow.RED));
Console.WriteLine("rainbow.GetColour(VIOLET): " +
rainbow.GetColour(Rainbow.VIOLET));

Console.WriteLine(@"rainbow.GetColour(""black""): " +
rainbow.GetColour("black"));
Console.WriteLine("rainbow.GetColour(" + Rainbow.RED_STR + "): " +
rainbow.GetColour(Rainbow.RED_STR));
Console.WriteLine("rainbow.GetColour(" + Rainbow.VIOLET_STR + "): " +
rainbow.GetColour(Rainbow.VIOLET_STR));

System.Threading.Thread.Sleep(10 * 1000);
}
}
}

7 réponses

Messages postés
6063
Date d'inscription
dimanche 13 avril 2003
Statut
Modérateur
Dernière intervention
15 juillet 2011
28
salut,

1 --> sur la base de ton code
il faut mettre tout en uint pour éviter d'avoir des conversions

2 --> il mettre
return colours.ColsStr[col-1];
et
if (colours.ColsStr[idx-1].CompareTo(colStr) == 0)

pour les 2 recherches
apres il est possible de tout refaire sur une base d'enum

//classe static maintenant
namespace Rainbow
{
  static  class   Rainbow
    {
        public enum Couleur : uint
        {
            RED = 1,
            ORANGE = 2,
            YELLOW = 3,
            GREEN = 4,
            BLUE = 5,
            INDIGO = 6,
            VIOLET = 7
        }

        public static string GetColour(uint col)
        {
            return Enum.GetName(typeof(Couleur), col);
        }

        public static uint GetColour(string colStr)
        {
            return (uint)(Enum.Parse(typeof(Couleur), colStr, false));

        }
    }
}

------------


//Utilisation
Console.WriteLine("rainbow.GetColour(2): " + Rainbow.Rainbow.GetColour(2));
Console.WriteLine("rainbow.GetColour(" + Rainbow.Rainbow.Couleur.VIOLET + "): " + Rainbow.Rainbow.GetColour(Rainbow.Rainbow.Couleur.VIOLET.ToString()));

//résultat
rainbow.GetColour(2): ORANGE
rainbow.GetColour(VIOLET): 7

Messages postés
3246
Date d'inscription
lundi 25 avril 2005
Statut
Modérateur
Dernière intervention
27 octobre 2012
34
J'espere qu'il n'y aura pas trop de C# dans son futur job ; )
Messages postés
1860
Date d'inscription
lundi 28 novembre 2005
Statut
Modérateur
Dernière intervention
14 février 2015
42
Do you really think that asking other people to make your work is a good idea ? Is that what you're gonna do if you get this job ?
You're trying to get a job by lying about what you can do. What will happen when they see you can't program in C# ?

Krimog : while (!(succeed = try())) ;
- Nous ne sommes pas des décodeurs ambulants. Le style SMS est prohibé. -
Messages postés
5
Date d'inscription
samedi 6 février 2010
Statut
Membre
Dernière intervention
12 mars 2010

Alright, so to make it a bit more clear, I am good in C++ and Java programming (2years in company) but that's true much less with C#. Anyway it doesn't matter since the job I apply for is UI Design in java, but the test is in C# because as you can see it's more of a design problem research than a compilation problem research. SO in that case, I already have answers to this problem, the reason I put this on a forum is that I need this job and I wanted other opinions from maybe more experienced guys to be sure that what I'm gonna give to this company is perfect... And you know as well as I do that once you have the job, even if they're some stuff you don't know how to do and you're not an expert, there is always a way around to handle it.
So please, instead of spending time to judge me, could you please just help me if you're so good.

Thank you.

Best regards,

-RaptorKiller
Messages postés
5
Date d'inscription
samedi 6 février 2010
Statut
Membre
Dernière intervention
12 mars 2010

By the way, any idea about:

Console.WriteLine(@"rainbow.GetColour(""black""): " +
rainbow.GetColour("black"));

What is this @? What is it used for?

Thank you.
Messages postés
1860
Date d'inscription
lundi 28 novembre 2005
Statut
Modérateur
Dernière intervention
14 février 2015
42
@ before a string is a way to avoid special chars with \. If you want to write ", you have to double it

If fact,
@"rainbow.GetColour(""black""): "
is the same thing as
"rainbow.GetColour("black"): "

This way of writing strings is really efficient for paths
"C:\\Documents and Settings\\Login\\My Documents\\My Pictures"
=>
@"C:\Documents and Settings\Login\My Documents\My Pictures"

Krimog : while (!(succeed = try())) ;
- Nous ne sommes pas des décodeurs ambulants. Le style SMS est prohibé. -
Messages postés
5
Date d'inscription
samedi 6 février 2010
Statut
Membre
Dernière intervention
12 mars 2010

Cheers mate, now it explains why this code was interpreted as GetColour("black")...

Thanks a lot.

Otherwise, I think I found quite good answers to every single point except the number 3 "Limitation on use" of this code, do you have any idea?

Thank you very much again.