سؤال

I have this homework. And I am beginner C# program, and now learn the C# and Java

Homework: Write static method that get a Drink[] array, where the AlcoholDrink has got different alcohol values. The method returns an array containing the three highest alcohol value! If it is not so much AlcoholDrink in the array, the method is returning a null reference!

I have tried write this method, but it not work correctly...

Cause at the sz[1] = (AlcoholDrink)t[1]; it is a Drink object (non-alcohol), I don't understand why it is there...

Or maybe my Compare method is not perfect, maybe there is the mistake... How can I aim, the non AlcoholDrink objects (Drink Objects) go to the end of the array?

Here my C# code:

    class DrinkComparer : IComparer<Drink>
    {
        public int Compare(Drink x, Drink y)
        {
            // AlcoholDrink class is children of Drink class
            if (x is AlcoholDrink && y is AlcoholDrink)
            {
                AlcoholDrink a = (AlcoholDrink)x;
                AlcoholDrink b = (AlcoholDrink)y;
                double aAlk = a.GetValueOfAlcohol; 
                double bAlk = b.GetValueOfAlcohol;
                if (aAlk > bAlk)
                    return -1;
                else if (aAlk < bAlk)
                    return 1;
                else
                    return 0;
            }
            // Drink objects haven't got GetValueOfAlcohol method...
            // How can I aim, the non AlcoholDrink objects (Drink objects) go to the end     array?
            else
                return 1;                
        }
    }
    static AlcoholDrink[] Largest3AlcoholDrink(Drink[] t)
    {
        Array.Sort(t, new DrinkComparer());
        AlcoholDrink[] sz = new AlcoholDrink[3];
        sz[0] = (AlcoholDrink)t[0];
        sz[1] = (AlcoholDrink)t[1];
        sz[2] = (AlcoholDrink)t[2];
        return sz;
    }
        AlcoholDrink sz = new AlcoholDrink( "Kékfrankos" , "0.75 l", 1500, 4.5);
        Console.WriteLine(sz);

        Drink[] t = new Drink[8];
        t[0] = new AlcoholDrink("Kék Portói", "0.75 l", 1200, 20.5);
        t[1] = new Drink("Tocsik", "0.75 l", 1100); // Non Alcohol Drink
        t[2] = new AlcoholDrink("Tokaji Asszú", "0.75 l ", 1600, 14.5);
        t[3] = new AlcoholDrink("Egri Bikavér", "0.75 l", 1500, 23.5);
        t[4] = new Drink("Egri Szamóca", "0.75 l", 1100); // Non Alchol Drink
        t[5] = new AlcoholDrink("Egri Merlot", "0.75 l", 1700, 18.5);
        t[6] = new AlcoholDrink("Egri Medina", "0.75 l", 900, 16.5);
        t[7] = new AlcoholDrink("Törley Talisman", "0.75 l", 750, 4.5);
        Console.WriteLine(DrinkKeres( t, "Egri Bikavér"));

        Largest3AlcoholDrink(t);

        Console.ReadLine();
هل كانت مفيدة؟

المحلول

Problem is here:

        // Drink objects haven't got GetValueOfAlcohol method...
        // How can I aim, the non AlcoholDrink objects (Drink objects) go to the end     array?
        else
            return 1;

You want non alcoholic drinks to go the end, but this will return 1 even if one of the drinks is alcoholic. Do this:

else if (x is AlcoholicDrink) return -1;
else if (y is AlcoholicDrink) return 1;
else return 0;

نصائح أخرى

Suppose the sort function calls your compare method with a normal Drink and an AlcoholDrink. You return 1, which is good because Drink > AlcoholDrink. However if your Compare method is called with an AlcoholDrink and a normal Drink you also return 1, which is bad because AlcoholDrink is not greater than Drink.

You need to explicitly handle the four separate cases (AlcoholDrink,AlcoholDrink); (AlcoholDrink,Drink); (Drink, AlcoholDrink) and (Drink, Drink) and return appropriate values for each one.

Do you really need the non-alcoholic drinks to go to the end of the array? Could you filter them out instead?

If LINQ is a possibility for you, you could use t.OfType<AlcoholDrink>() to do the filtering. Otherwise, you could implement your own method:

public AlcoholDrink[] GetAlcoholicDrinks(Drink[] drinks) {
  ArrayList alcoholDrinks = new ArrayList();
  foreach (Drink drink in drinks) {
    if (drink is AlcoholDrink) {
      alcoholDrinks.Add(drink);
    }
  }
  return alcoholDrinks.ToArray(typeof(AlcoholDrink)) as AlcoholDrink[];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top