In C Car Class Create a class named Car that has the followi

In C#

Car Class

Create a class named Car that has the following properties:

Year - The Year Property holds the car’s year model

Make – The Make property holds the make of the car.

Speed – The Speed property holds the car’s current speed.

In addition, the class should have the following constructor and other methods.

Constructor – The constructor should accept the car’s year and model and make them as arguments. These values should be assigned to the backing fields for the object’s Year and Make properties. The constructor should also assign 0 to the backing field for the Speed property.

Accelerate – The Accelerate method should add 5 to the Speed property’s backing field each time it is called.

Brake – The Brake method should subtract 5 from the Speed property’s backing field each time it is called.

Demonstrate the class in an application that creates a Car object. The application’s form should have an Accelerate button that calls the Accelerate method and then displays the car’s current speed each time the button is clicked. The application’s form should also have a Brake button that calls the Brake method and then displays the car’s current speed each time the button is clicked.

Let the user know if they have not entered a make or year by displaying a messageBox. Show current speed in a label. Make sure the speed is never a negative number by letting the user know the car is not moving if the speed is 0.

Example of form can be as followed:

Car Demo Make: Year Accelerate Brake Speed:

Solution

// Below is the Car class implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Car_Class_BBrantley
{
class Car
{
private int year;
private string make;
private int speed;

public Car()
{
       // Defualt values
this.year = 2016;
this.make = \"Nano\";
this.speed = 0;
}

public Car(string make, int year, int speed)
{
this.year = year;
this.make = make;
this.speed = speed;
}

public string Make
{
get { return make; }
set { make = value; }
}

public int Year
{
get { return Year; }
set { Year = value; }
}

public int Speed
{
get { return speed; }
set { speed = value; }
}

public void AccSpeed(int SpdIncrement)
{
Speed += SpdIncrement;
}

public void DecSpeed(int SdDecrement)
{
Speed -= SdDecrement;
           if(Speed < 0)
           Speed =0;
}
}
}

// Below is the Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Car_Class_BBrantley
{
public partial class Form1 : Form
{
private Car myCar;

public Form1()
{
myCar = new Car();

InitializeComponent();
}

private void GetCarData()
{
try {
myCar.Make = txtMake.Text;

myCar.Year = int.Parse(txtModel.Text);

myCar.Speed = 0;

}
catch (Exception ex)
{
MessageBox.Show(string.Concat(\"Must enter a valid make and year model for the car. \", ex.Message, \"\ \ \", ex.StackTrace));
}
}

private void btnAcc_Click(object sender, EventArgs e)
{
GetCarData();
myCar.AccSpeed(5);
MessageBox.Show(\" Your car is a \" + myCar.Year + myCar.Make + \" and it is traveling \" + myCar.Speed + \" mph. \");
}

private void btnBrake_Click(object sender, EventArgs e)
{
GetCarData();
myCar.DecSpeed(5);
MessageBox.Show(\" Your car is a \" + myCar.Year + myCar.Make + \" and it is traveling \" + myCar.Speed + \" mph. \");
}
}
}

In C# Car Class Create a class named Car that has the following properties: Year - The Year Property holds the car’s year model Make – The Make property holds t
In C# Car Class Create a class named Car that has the following properties: Year - The Year Property holds the car’s year model Make – The Make property holds t
In C# Car Class Create a class named Car that has the following properties: Year - The Year Property holds the car’s year model Make – The Make property holds t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site