Create a class named Car with auto-implemented properties
for the vehicle ID number, make, model, color, and value of a
Car object. Write a DisplayFleet( ) method that accepts any
number of Car objects, displays their values, and displays the
total value of all Car objects passed to the method. Write a
Main( ) method that declares fi ve Car objects and assigns values
to each, then calls DisplayFleet() three times—passing
three, four, and fi ve Car objects in successive calls..
This is as far as I could get... I am not sure what they mean by "accept any number of Car objects" , do I like have to create list, array or what?
What should I do next?
private int vIdNumber;
private string vMake;
private int vModel;
private string vColor;
private double vValue;
public int VidNumber { get; set; }
public string Vmake { get; set; }
public int Vmodel { get; set; }
public string Vcolor { get; set; }
public double VValue { get; set; }
public void DisplayFleet(double value)
{
}
Loading
VulpesPosted Feb 3, 2012, 6:23 PM
It's not clear whether the DisplayFleet method is to be part of the Car class or part of the class containing the Main() method - my guess would be the latter. You can accept any number of Car objects by using a parameter array:
double totalValue = 0;
{
// code to display car values and add them up
}
// code to display total value
If c1, c2, c3 are Car objects, you could call this method as follows:
DisplayFleet(c1, c2, c3);
Prime bPosted Feb 4, 2012, 12:12 PM
VulpesPosted Feb 4, 2012, 4:47 AM
Anyway, the error is due to car[4] not being initialized - it's still null.
An Audi would be nice :)
Prime bPosted Feb 3, 2012, 6:48 PM
This is what I have done using the link maha provided, I am guessing Maha has same book like I do. Except when i run in it gives me an error.
Car[] car = new Car[6];
car[0] = new Car(1111, "Buick", "x58", "red", 20000);
car[1] = new Car(2222, "Lame", "car", "blue", 111);
car[2] = new Car(3333, "Most lame", "Kinda", "pink", 1);
car[3] = new Car(4444, "Mercedes", "C class", "Black", 30000);
car[5] = new Car(5555, "BMW", "M5", "Black", 40000);
DisplayFleet(car[0], car[1], car[2]);
DisplayFleet(car[4], car[2], car[5]);
DisplayFleet(car[3], car[2]);
}
public static void DisplayFleet(params Car[] carsArray)
{
double carSum = 0;
double carPrice = 0;
int index = 0;
foreach (Car car in carsArray)
{
carSum = carSum + car.VValue;
carPrice = car.VValue;
Console.WriteLine("The price {0} of the car {1}", index, carPrice.ToString("C"));
++index;
}
Console.WriteLine("The sum of all the prices is {0}", carSum.ToString("C"));
Console.WriteLine();
class Car
{
public int VidNumber { get; set; }
public string Vmake { get; set; }
public string Vmodel { get; set; }
public string Vcolor { get; set; }
public double VValue { get; set; }
public Car(int vidNumber, string vMake,string vModel,string vColor, double vValue)
{
this.VidNumber = vidNumber;
this.Vmake = vMake;
this.Vmodel = vModel;
this.Vcolor = vColor;
this.VValue = vValue;
}
}
Posted Feb 3, 2012, 6:15 PM
http://www.c-sharpcorner.com/Forums/Thread/154155/parallel-array.aspx