|
|
|
|
Prime b
posted
407 posts
since
Dec 12, 2011
from
|
Classes and fields
|
|
|
|
|
|
|
|
|
|
|
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) {
}
|
|
|
|
|
Practice makes it perfect && All science is either computer programming or stamp collecting
|
|
|
|
|
|
Maha
posted
809 posts
since
Aug 13, 2006
from
|
|
Re: Classes and fields
|
|
|
|
|
|
|
|
|
|
|
Please see
http://www.c-sharpcorner.com/Forums/Thread/154155/parallel-array.aspx
|
|
|
|
|
|
Vulpes
posted
5113 posts
since
Feb 28, 2011
from
|
|
Re: Classes and fields
|
|
|
|
|
|
|
|
|
|
The first point I'd make is that, if you have an auto-implemented property, then you don't need to declare a field as well. The C# compiler will provide a field but you won't be able to access this directly - you just use the property for everything.
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:
public static void DisplayFleet(params Car[] cars) { double totalValue = 0;
foreach(Car car in cars) { // 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 b
posted
407 posts
since
Dec 12, 2011
from
|
|
Re: Classes and fields
|
|
|
|
|
|
|
|
|
|
|
Oh hey vulpes, you still not bed, great!
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; } }
|
|
|
|
|
Practice makes it perfect && All science is either computer programming or stamp collecting
|
|
|
|
|
|
Vulpes
posted
5113 posts
since
Feb 28, 2011
from
|
|
Re: Classes and fields
|
|
|
|
|
|
|
|
|
|
|
Hmm, it does look like you have the same book as Maha, something by Joyce Farrell IIRC.
Anyway, the error is due to car[4] not being initialized - it's still null.
An Audi would be nice :)
|
|
|
|
|
|
Prime b
posted
407 posts
since
Dec 12, 2011
from
|
|
Re: Classes and fields
|
|
|
|
|
|
|
|
|
|
|
ohhh I didn't even see it. haha audi would be nice!
|
|
|
|
|
Practice makes it perfect && All science is either computer programming or stamp collecting
|
|
|
|
|
|
|
|
|