Question
From Gaddis Starting Out With Visual C#, chpt 9 pg 602, prb. 2 'Car Class'. Must code the rest of the solution in Visual C#
From Gaddis Starting Out With Visual C#, chpt 9 pg 602, prb. 2 'Car Class'.
Must code the rest of the solution in Visual C# using these constraints using Visual Studio:
This is the form so far:
There is included a Car.cs code file:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Program9_2 { class Car { private int _year; private string _model; private string _make; private double _speed;
public int Year { get { return _year; } set { _year = value; } } public string Make { get { return _make; } set { _make = value; } }
public void Accelerate() { Speed += 5; }
} }
and the Form1.cs code is:
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 Program9_2 { public partial class Form1 : Form { //Declare an instance of the car class passing in 3 arguments for the contructor
public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { label1.Text = "The speed of " + myCar.Year + " " + myCar.Make + " " + myCar.Model + " is " + myCar.Speed + " mph."; }
private void button2_Click(object sender, EventArgs e) { label1.Text = "The speed of " + myCar.Year + " " + myCar.Make + " " + myCar.Model + " is " + myCar.Speed + " mph."; } } }
and.. what I have put together so far:
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 Program9_2 { public partial class Form1 : Form { //Declare an instance of the car class passing in 3 arguments for the contructor class Car {
private int_speed private string_model; public int Speed { get { return _speed; } set { _speed = value; } } public string Model { get { return _model; } set { _model = value; } } public Car(int Year, string Make) _year = year; _make = make; _model = model; _speed = 0; public void Brake() { _speed = _speed - 5; }
public Form1() { InitializeComponent(); } private Car = new myCar(2009, "Ford", "Escape");
private void button1_Click(object sender, EventArgs e) { Car.Accelerate(); label1.Text = "The speed of " + myCar.Year + " " + myCar.Make + " " + myCar.Model + " is " + myCar.Speed + " mph."; }
private void button2_Click(object sender, EventArgs e) { Car.Brake(); label1.Text = "The speed of " + myCar.Year + " " + myCar.Make + " " + myCar.Model + " is " + myCar.Speed + " mph."; }
private void Form1_Load(object sender, EventArgs e) { label1.Text = Car.year.ToString(); label1.Text = Car.make.ToString(); label1.Text = Car.model.ToString(); label1.Text = Car.speed.ToString(); } } }
Thanks-
a. Add the Model and Speed property procedures to the exsting Car class. b. Create a parameterized constructor using the following parameter names: year as integer, make as a string, model as string. Assign the backing field variables to these parameter variables. Make sure the backing field for speed is set to 0. Add a void method and name it Brake. It will decrement the speed by 5 miles every time it is called BUT make sure that the speed does not become a negative value. c. View the code of the Form1.cs class. Define a field variable and name it myCar. It should define as an instance of the Car class passing in the following values 2009, "Ford", "Escape". Finish coding the Accelerate and Brake button click events to call the correct Car method. a. bStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started