Using C# create a new backing field called _speedIncrement. Inside the construct
ID: 3701530 • Letter: U
Question
Using C# create a new backing field called _speedIncrement. Inside the constructor initialize this backing field to 5. Adjust other parts of the code for this new dynamic variable. Then create a new property method called "SetIncrement", to allow the user to change the increment in the class. Incremint has to be an int value and cant be less than 1 or greater than 20. If the value the user enters is not less than 1 or greater than 20, issue a messagebox from within the class which tells the user to re enter a current number. In Form1 add a labelbox, textbox, and a button to allow the user to change the speed increment. In the class' Speed property, before changing the backing speed to its new value, check and see if it exceeds 100mph. If it does, set the value to 100. Issue a messagebox message, from within the class, stating the you have exceeded the maximum speed of the car.
Code to build off of:
Form1:
namespace speedtest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
labelMake.Text = car.Make;
labelYear.Text = car.Year.ToString();
labelSpeed.Text = car.Speed.ToString();
}
private Car car = new Car(2018, "Wrangler");
private void button2_Click(object sender, EventArgs e)
{
car.Brake();
labelSpeed.Text = car.Speed.ToString();
}
private void buttonAccelerate_Click(object sender, EventArgs e)
{
car.Accelerate();
labelSpeed.Text = car.Speed.ToString();
}
}
}
Car Class
namespace speedtest
{
class Car
{
private string _Make;
private int _Year;
private int _Speed;
public Car(int Year, string Make)
{
_Year = Year;
_Make = Make;
_Speed = 0;
}
public int Year
{
get
{
return _Year;
}
set
{
_Year = value;
}
}
public string Make
{
get
{
return _Make;
}
set
{
_Make = value;
}
}
public void Accelerate()
{
_Speed = _Speed + 5;
}
public void Brake()
{
_Speed = _Speed - 5;
}
public int Speed
{
get
{
return _Speed;
}
set
{
_Speed = value;
}
}
}
}
Explanation / Answer
The modified classes are as below:
Form Class
namespace speedtest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
labelMake.Text = car.Make;
labelYear.Text = car.Year.ToString();
labelSpeed.Text = car.Speed.ToString();
labelIncrementSpeed.Text = "Set Increment Speed";
}
private Car car = new Car(2018, "Wrangler");
private void button2_Click(object sender, EventArgs e)
{
car.Brake();
labelSpeed.Text = car.Speed.ToString();
}
private void buttonAccelerate_Click(object sender, EventArgs e)
{
car.Accelerate();
labelSpeed.Text = car.Speed.ToString();
}
private void buttonIncrement_Click(object sender, EventArgs e)
{
int value = int.Parse(textboxSpeed.Text);
if(value >=1 && value <=20)
{
car.SetIncrement(value);
labelIncrementSpeed.Text = car.Speed.ToString();
}
else
{
MessageBox.Show("Error! Invalid Input Please enter a speed value between 1-20 and Try Again!", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
labelIncrementSpeed.Text = car.Speed.ToString();
}
}
}
}
Car Class
namespace speedtest
{
class Car
{
private string _Make;
private int _Year;
private int _Speed;
private int _speedIncrement;
public Car(int Year, string Make)
{
_Year = Year;
_Make = Make;
_Speed = 0;
_speedIncrement = 5;
}
public int Year
{
get
{
return _Year;
}
set
{
_Year = value;
}
}
public string Make
{
get
{
return _Make;
}
set
{
_Make = value;
}
}
public void Accelerate()
{
_Speed = _Speed + _speedIncrement;
}
public void Brake()
{
_Speed = _Speed - _speedIncrement;
}
public int Speed
{
get
{
return _Speed;
}
set
{
_Speed = value;
}
}
public void SetIncrement(int value)
{
_speedIncrement = value;
}
}
}
Explanation: As per the question, new car class instance variable is created '_speedIncrement' which is set to 5 in the constructor. A new method is created 'SetIncrement' which accepts integer argument and sets the _speedIncrement value to new value. The other methods Brake and Accelerate are modified. The hardcoded 5 value is replaced with the _speedIncrement variable.
In the Form class, new label, textbox and button is added. The speed increment value entered inside the textbox is checked if the value is between 1-20 then the speed Increment variable is set to the new value upon button click else an error message box appears.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.