Write a program in C#, and create the class Car, having 6 private fields, and 6 public Proprieties, public overloaded 2 Constructors (default and one with six arguments) and a public Method void Display() to display the information about the car (use placeholders).
Loading

Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 9, 2023, 3:17 PM EST
HI Dan,
Here is the code example of your program. You can add or modify the private fields or properties according to your requirements:
using System; public class Car { // These are the private fields private string make; private string model; private int year; private double price; private string color; private int speed; // Default Constructor public Car() { make = "Honda"; model = "Civic"; year = 202; price = 0.0; color = "RED"; speed = 0; } // Overloaded Constructor with all parameters public Car(string make, string model, int year, double price, string color, int speed) { this.make = make; this.model = model; this.year = year; this.price = price; this.color = color; this.speed = speed; } // Properties public string Make { get { return make; } set { make = value; } } public string Model { get { return model; } set { model = value; } } public int Year { get { return year; } set { year = value; } } public double Price { get { return price; } set { price = value; } } public string Color { get { return color; } set { color = value; } } public int Speed { get { return speed; } set { speed = value; } } // Method to display information about the car with placeholder public void Display() { Console.WriteLine("Car Information:"); Console.WriteLine("Make: {0}", make); Console.WriteLine("Model: {0}", model); Console.WriteLine("Year: {0}", year); Console.WriteLine("Price: {0}", price); Console.WriteLine("Color: {0}", color); Console.WriteLine("Speed: {0}", speed); } } // This is the main method static void Main(string[] args) { var car = new Car(); car.Display(); }Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 9, 2023, 10:54 PM EST
Hi Dan,
You can use below example.:
using System; public class Car { private string _manufacturer; private string _vehicleType; private int _productionYear; private string _paint; private int _velocity; private int _fuel; // Default constructor public Car() { _manufacturer = "unknown"; _vehicleType = "unknown"; _productionYear = 0; _paint = "unknown"; _velocity = 0; _fuel = 0; } // Constructor with six arguments public Car(string manufacturer, string type, int year, string paint, int speed, int fuel) { _manufacturer = manufacturer; _vehicleType = type; _productionYear = year; _paint = paint; _velocity = speed; _fuel = fuel; } // Properties public string Manufacturer { get { return _manufacturer; } set { _manufacturer = value; } } public string VehicleType { get { return _vehicleType; } set { _vehicleType = value; } } public int ProductionYear { get { return _productionYear; } set { _productionYear = value; } } public string Paint { get { return _paint; } set { _paint = value; } } public int Velocity { get { return _velocity; } set { _velocity = value; } } public int Fuel { get { return _fuel; } set { _fuel = value; } } // Method to display the information about the car public void DisplayInfo() { Console.WriteLine("Vehicle Manufacturer: {0}", _manufacturer); Console.WriteLine("Vehicle Type: {0}", _vehicleType); Console.WriteLine("Production Year: {0}", _productionYear); Console.WriteLine("Paint: {0}", _paint); Console.WriteLine("Velocity: {0}", _velocity); Console.WriteLine("Fuel: {0}", _fuel); } }Then you can call display method like:
Car myCar = new Car("Toyota", "Camry", 2020, "Red", 120, 50); myCar.DisplayInfo();Thanks
Naimish Makwana
Vishal JoshiRank #377 · 6.7K reputationPosted Feb 9, 2023, 10:58 PM EST
Hello Dan,
Please review the below code as per your requirement.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace POC_Projects { public class CarDepartment { static void Main(String[] args) { //Create instance with default constructor var carDefault = new Car(); carDefault.Display(); //Create instance with overloded constructor var carOverloded = new Car("Hundai","Venue",2021,"2.0 L","Black",220); carOverloded.Display(); Console.ReadLine(); } } public class Car { // private fields private string brand; private string model; private int year; private string engine; private string color; private int maxSpeed; // Default Constructor public Car() { brand = "Honda"; model = "Civic"; year = 2020; engine = "1.5 L"; color = "RED"; maxSpeed = 200; } // Overloaded Constructor with parameter public Car(string brand, string model, int year, string engine, string color, int maxSpeed) { this.brand = brand; this.model = model; this.year = year; this.engine = engine; this.color = color; this.maxSpeed = maxSpeed; } // Public Properties public string Brand { get { return brand; } set { brand = value; } } public string Model { get { return model; } set { model = value; } } public int Year { get { return year; } set { year = value; } } public string Engine { get { return engine; } set { engine = value; } } public string Color { get { return color; } set { color = value; } } public int MaxSpeed { get { return maxSpeed; } set { maxSpeed = value; } } // Method to display information about the car. public void Display() { Console.WriteLine("Car Information:"); Console.WriteLine("Brand: {0}", brand); Console.WriteLine("Model: {0}", model); Console.WriteLine("Year: {0}", year); Console.WriteLine("Price: {0}", engine); Console.WriteLine("Color: {0}", color); Console.WriteLine("Speed: {0}", maxSpeed); } } }You can change code according to your requirement.
Thanks