Jump to content

Can you help us with an oop problem in c#?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
nando

nando

    Newbie

  • Members
  • Pip
  • 1 posts
Hi guys!

How are you?
We are from Argentina
Sorry but my english isn't very good.

I have got an exercise to resolve with OOP and we are looking for the best way to solve it

The problem to solve is count the number of instances of any subtype (Car, Truck) in a supertype collection (Vehicle)


Posted Image


C# code


IList<Vehicle> vehicles = new List<Vehicle>();


Car aCar = null;

Truck aTruck = null;


aCar = new Car();

vehicles.Add(aCar);


aCar = new Car();

vehicles.Add(aCar);


aCar = new Car();

vehicles.Add(aCar);


aTruck = new Truck();

vehicles.Add(aTruck);


aTruck = new Truck();

vehicles.Add(aTruck);


// 3 cars

// 2 trucks


int cars = 0;

int trucks = 0;


foreach(Vehicle v in vehicles) {

    if(v is Car){

        cars++;

    } else if(v is Truck){

        trucks++;

    }

}


is there a better way to solve this problem, without using clause "is Type"?

Cheers!
Fernando López Guevara

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Here is another way, but still uses 'is Type':

int cars = vehicles.Count(p => p is Car);
int trucks = vehicles.Count(p => p is Truck);


#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Hmm i think that would be about the easiest solution.

Another would be to create your own list class. that extends the List.
following code may not be 100% correct.

public class MyList : List
{
  int carAmount, truckAmount;
  
  public MyList()
  {
    carAmount = 0;
    truckAmount = 0;  
  }

  public void add(Truck Truck)
  {
    base.add(trck);
    carAmount++;
  }

  public void add(Car car)
  {
    base.add(car);
    truckAmount++;
  }
}

And then you can just ask the amounts of this class. It'll propably need more functions to direct to the list like remove and get etc.

#4
nakor

nakor

    Newbie

  • Members
  • Pip
  • 3 posts
	public class Vehicle
	{
		private static int count = 0;
		public static int Count {get {return count;} protected set {count = value;} }
		
		public Vehicle ()
		{
			Count++;
		}
	}
	
	public class Car : Vehicle
	{
		public Car ()
		{
					
		}
	}
	
	public class Truck : Vehicle
	{
		public Truck ()
		{
			
		}
	}

now Vehicle.Count will give you the count of total vehicles

static void Main(string[] args)
        {
            Vehicle v = new Vehicle();
	    Truck t = new Truck();
	    Car c = new Car();

	    Console.WriteLine (Vehicle.Count); // will output 3
        }


#5
nakor

nakor

    Newbie

  • Members
  • Pip
  • 3 posts
And to add that functionality to each subtype just add a static count to each like so
	public class Car : Vehicle
	{
		private static int count = 0;
		public static int CarCount {get{return count;} protected set {count = value;}}
		
		public Car ()
		{
			CarCount++;
		}
	}
	
	public class Truck : Vehicle
	{
		private static int count = 0;
		public static int TruckCount {get{return count;} protected set {count = value;}}
		
		public Truck ()
		{
			TruckCount++;
		}
	}

Now, Vehicle.Count will keep count of all vehicles, Car.CarCount tracks number of cars and Truck.TruckCount tracks number of trucks




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users