TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Maha
NA
0
326k
Constructor & get & set method
Feb 18 2012 10:27 PM
Q(11)
This question talks about "appropriate get and set methods". In the following program set method is comment out but output is same. I wish to know whether set() method is needed if there is a public constructor. Program is as follows:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Car[] car = new Car[5];
car[0] = new Car(111, "Toyota", "Corolla", "Black", 5000);
car[1] = new Car(222, "Morris", "Minor", "Red", 10000);
car[2] = new Car(333, "Morris", "Oxford", "White", 15000);
car[3] = new Car(444, "Ford", "Mondeo", "Brown", 20000);
car[4] = new Car(555, "Mazda", "MX5", "Green", 25000);
DisplayFleet(car[0], car[1], car[2]);
DisplayFleet(car[0], car[1], car[2], car[3]);
DisplayFleet(car[0], car[1], car[2], car[3], car[4]);
Console.ReadKey();
}
static void DisplayFleet(params Car[] carArray)
{
int total = 0;
for (int x = 0; x < carArray.Length; ++x)
{
Console.Write("Car[{0}]${1} ", x, carArray[x].getPrice());
total = total + carArray[x].getPrice();
}
Console.WriteLine("\nTotal ${0}", total);
Console.WriteLine();
}
}
}
class Car
{
int id;
string make;
string model;
string color;
int price;
public Car(int id, string make, string model, string color, int price)
{
this.id = id;
this.make = make;
this.model = model;
this.color = color;
this.price = price;
}
public int getId()
{
return this.id;
}
//public void setId(int id)
//{
//this.id = id;
//}
public string getMake()
{
return this.make;
}
//public void setMake(string make)
//{
//this.make = make;
//}
public string getModel()
{
return this.model;
}
//public void setModel(string model)
//{
//this.model = model;
//}
public string getColor()
{
return this.color;
}
//public void setColor(string color)
//{
//this.color = color;
//}
public int getPrice()
{
return this.price;
}
//public void setPrice(int price)
//{
//this.price = price;
//}
}
/*
Car[0]$5000 Car[1]$10000 Car[2]$15000
Total $30000
Car[0]$5000 Car[1]$10000 Car[2]$15000 Car[3]$20000
Total $50000
Car[0]$5000 Car[1]$10000 Car[2]$15000 Car[3]$20000 Car[4]$25000
Total $75000
*/
Attachment:
chapter6-exercises.zip
Reply
Answers (
3
)
for instead of foreach
Key value pair with FIFO or FILO to store recent history?