Exercise: Car Class
- Under the Exercises folder add new folder name it MoreOnClass.
- Add a new Class name it TestClass.
- Add Run method.
- Call it from the application MDI.
Your new Class supposed to look like this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Northwind.Exercises.MoreOnClass
{
class TestClass
{
public void Run()
{
}
}
- Add a new class name it Car.
- Add This members to the Run :
- string carName.
- int carYear.
- int CarKM.
- char carType.
Your new Class supposed to look like this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Northwind.Exercises.MoreOnClass
{
class Car
{
public string carName;
public int carYear;
public int CarKM;
public string carType;
}
}
- In the TestClass class create 2 new instance of the Car class name it:
- mainCar.
- weekendCar.
- Set the value of your two new cars to fit your dream cars.
- Add message box to show the two cars info.
Your Class supposed to look like this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Northwind.Exercises.MoreOnClass
{
class TestClass
{
public void Run()
{
Car mainCar = new Car();
Car weekendCar = new Car();
mainCar.carName = "BMW X6";
mainCar.carYear = 2015;
mainCar.CarKM = 20000;
mainCar.carType = "Main";
weekendCar.carName = "Ford mustang";
weekendCar.carYear = 1965;
weekendCar.CarKM = 160000;
weekendCar.carType = "Weekend";
MessageBox.Show("My "+ mainCar.carType + " car is " + mainCar.carYear +" " + mainCar.carName + "\n" +
" I drove it for " + mainCar.CarKM + " KM");
MessageBox.Show("My " + weekendCar.carType + " car is " + weekendCar.carYear + " " + weekendCar.carName + "\n" +
" I drove it for " + weekendCar.CarKM + " KM");
}
}
}
- Build and test.
- In the Car class Add a new method CarInfo.
- add a message box to the new method to show the car info.
- In the TestClass class replace the message box to use the new method.
- Build and test.
Your Car Class supposed to look like this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Northwind.Exercises.MoreOnClass
{
class Car
{
public string carName;
public int carYear;
public int CarKM;
public string carType;
public void CarInfo()
{
MessageBox.Show("My " + this.carType + " car is " + this.carYear + " " + this.carName + "\n" +
" I drove it for " + this.CarKM + " KM");
}
}
}
Your TestClass Class supposed to look like this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Northwind.Exercises.MoreOnClass
{
class TestClass
{
public void Run()
{
Car mainCar = new Car();
Car weekendCar = new Car();
mainCar.carName = "BMW X6";
mainCar.carYear = 2015;
mainCar.CarKM = 20000;
mainCar.carType = "Main";
weekendCar.carName = "Ford mustang";
weekendCar.carYear = 1965;
weekendCar.CarKM = 160000;
weekendCar.carType = "Weekend";
- MessageBox.Show("My "+ mainCar.carType + " car is " + mainCar.carYear +" " + mainCar.carName + "\n" +
- " I drove it for " + mainCar.CarKM + " KM");
-
- MessageBox.Show("My " + weekendCar.carType + " car is " + weekendCar.carYear + " " + weekendCar.carName + "\n" +
- " I drove it for " + weekendCar.CarKM + " KM");
mainCar.CarInfo();
weekendCar.CarInfo();
}
}
}
Help us improve, Edit this page on GitHub
or email us at info@fireflymigration.com