September 7, 2007
Hi Guys
NP39 Position of the DisplayFleet() method
I got the following question from the book.
In the question 1st author is talking about DisplayFleet() method then about Main() method and finally again DisplayFleet() method what author is saying she wants DisplayFleet() method both within the Car class and Main() method or only within the
Thank you
Create a class named Car containing fields that hold a vehicle ID number, make, model, color, and value for a Car object. Include appropriate get and set methods for each field. 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 five Car objects and assigns values to each, then calls DisplayFleet ( ) three times—passing three, four, and five Car objects in successive calls. Save the program as Car.es.
Posted Sep 8, 2007, 2:33 PM
Thank you very much for your help Alan
AlanPosted Sep 7, 2007, 7:12 PM
OK, here we go:
using System;
public class CarRecords
{
public static void Main()
{
Car car1 = new Car(111, "Toyota", "Corolla", "Black", 5000);
Car car2 = new Car(222, "Morris", "Minor", "Red", 10000);
Car car3 = new Car(333, "Morris", "Oxford", "White", 15000);
Car car4 = new Car(444, "Ford", "Mondeo", "Brown", 20000);
Car car5 = new Car(555, "Mazda", "MX5", "Green", 25000);
DisplayFleet(car1, car2, car3);
DisplayFleet(car1, car2, car3, car4);
DisplayFleet(car1, car2, car3, car4, car5);
}
public static void DisplayFleet(params Car[] carArray)
{
double sum = 0.0;
for (int x = 0; x < carArray.Length; x++)
{
Console.Write("{0} ", carArray[x].Price.ToString("c0"));
sum += carArray[x].Price;
}
Console.WriteLine("\nSum = {0}", sum.ToString("c0"));
Console.WriteLine();
}
}
public class Car
{
int id;
string make, model, color;
double price;
public Car(int carId, string carMake, string carModel, string carColor, double carPrice)
{
id = carId;
make = carMake;
model = carModel;
color = carColor;
price = carPrice;
}
public int ID
{
get{ return id;}
set{ id = value;}
}
public string Make
{
get{ return make;}
set{ make = value;}
}
public string Model
{
get{ return model;}
set{ model = value;}
}
public string Color
{
get{ return color;}
set{ color = value;}
}
public double Price
{
get{ return price;}
set{ price = value;}
}
}
Posted Sep 7, 2007, 6:31 PM
Alan, please correct the following program.
//page229
using System;
public class CarRecords
{
public static voidMain ()
{
Car[] carArray = new Car[]{ new car(111, "Toyota ", "Corola", "Black", 5000),
new car(222, "Morris", "Minor", "Red", 10000), new Car(333, "Morris", "Oxerd", "White", 15000)};
//DisplayFleet(12000, 4000, 6000);
//DisplayFleet(7000, 8000, 9000, 3000);
DisplayFleet(carArray);
}
public static void DisplayFleet(Car[] carArray)
{
int sum = 0;
for (int x = 0; x < carArray.Length; x++)
{
carArray[x] = new Car();
Console.Write("{0} ", carArray[x].GetPrice().ToString("c0"));
sum = sum + carArray[x];
}
Console.WriteLine("\nSum = {0}", sum.ToString("c0"));
Console.WriteLine();
}
}
class Car
{
int idNumber;
string make, model, color;
double price;
public Car(int id, string carMake, string carModel, string carColor, double carPrice)
{
idNumber = id;
make = carMake;
model = carModel;
color = carColor;
price = carPrice;
}
public int GetID()
{
return idNumber;
}
public string GetMake()
{
return make;
}
public string GetModel()
{
return model;
}
public string GetColor()
{
return color;
}
public double GetPrice()
{
return price;
}
/*
public void SetID(int carId)
{
idNumber = carId;
}
public void SetMake(string carMake)
{
make = carMake;
}
public void SetModel(string carModel)
{
model = carModel;
}
public void SetColor(string carColor)
{
color = carColor;
}
public void SetPrice(double carPrice)
{
price = carPrice;
}
*/
}
AlanPosted Sep 7, 2007, 3:42 PM
Well, it's not very clear but I'd say they intend you to place the DisplayFleet() method in the same class as your Main() method, not in the Car class itself.
Having said that, why don't you just include both Main() and DisplayFleet() as static methods within the Car class, so that you're covered either way ;)