Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 1
• if (myVaule == "1")
• {
• Console.WriteLine("You Enter 1");
• Console.WriteLine("Good you have got it");
• }
• else if (myVaule == "2")
• Console.WriteLine("You Enter 2");
• else if (myVaule == "3")
• Console.WriteLine("You Enter 3");
• else
• Console.WriteLine("You Enter unknownnumber");
• Console.ReadLine();
#endregion
switch condition
• #region
• Console.WriteLine("Enter 1 or 2 or 3 only");
• string myVaule = Console.ReadLine();
• switch (myVaule)
• {
• case "1":
• Console.WriteLine("You Enter 1");
• break;
• case "2":
• Console.WriteLine("You Enter 2");
• break;
• case "3":
• Console.WriteLine("You Enter 3");
• break;
• default:
• Console.WriteLine("You Enter unknown #");
• break;
• }
• Console.ReadLine();
• #endregion
While condition
• #region
• StreamReader myReader = new StreamReader("Data.txt");
• string line = "";
• while(line!=null)
• {
• line = myReader.ReadLine();
• if (line != null)
• Console.WriteLine(line);
• }
• Console.ReadLine();
• #endregion
Arrary , for , foreach
• #region
• string[] myArray = new string[3];
• myArray[0] = "Computer";
• myArray[1] = "Software";
• myArray[2] = "Class";
• #endregion
Creating a new class
• public class Automobile
• {
• public string Make;
• public string Modle;
• public string Color;
• public int Price;
• public int Speed;
Console.WriteLine("Increasing speed...\n");
Console.ReadLine();
FirstCar.Accelerate();
FirstCar.Accelerate();
FirstCar.Accelerate();
Console.ReadLine();
Creating an overload method for “Accelerate”
in Automobile class
public void Accelerate(int increase)
{
Speed = Speed + increase;
Console.WriteLine("Speed is increasing by " + increase + " km”);
}
Calling this overload version of method
“Accelerate”in Program.cs
FirstCar.Accelerate(30);
Console.ReadLine();
Creating two overload methods name
“Discount”in Automobile class
public int Discount(int discount)
{
int price = Price - ((Price /100)* discount);
return price;
Console.ReadLine();
Calling method “Discount”in Program.cs
Console.ReadLine();
Class Exercise
• Write an overload method for DiscountPrice
that would also take a third argument an
integer value. This third value is the tax
percentage. Calculate the tax on Price. Final
price should be calculate by subtracting
discount, subtracting special discount and
adding tax amount.
• Call this method in Program.cs
Add another car object
Automobile secondCar = new Automobile();
secondCar.Make = "Suzuki";
secondCar.Modle = "2016";
secondCar.Price = 100000;
secondCar.Color = "Blue";
secondCar.Speed = 210;
We can use our class Automobile just like another data type
Console.ReadLine();
More on Data types
We can create any type of class and use it as a data type in our code.
One predefine data type in ‘bool’. It has two values. true or false.
Example:
bool start = false;
if (start)
{
Console.WriteLine("true value");
}
else if (!start)
{
Console.WriteLine("false value");
}
Console.ReadLine();
Class exercise
1 Write code in main program that take as
input car Make and print on screen the
Make, Modle and Price from available stock.
If Make is not available a message should
print saying that particular make is not
available. Car stock is stored in an ARRAY of
Type Automobile.
2 Rewrite same program in the form of a
method in Automobile class and call this
method in Program.cs that’s output should
be same.