Sei sulla pagina 1di 20

Visual Programming

Lecture 1

Visual Studio & C#


Outline
• Declaring Variables and Assigning Values
• Initialize and use variables in your code
• code comments — essentially lines of code that are ignored by the compiler when an
assembly is built.
• Accepting Input in Command Line Applications
• The if Decision Statement
• Switch Statement and Conditional Operator
• while Iterations and Reading Data from a Text File
• Arrays
• For Iterations
• Foreach iteration
• Understanding Instantiation with the new Operator
– creating a new instance of a class, often referred to as “instantiation
– difference between a class and an object, or rather, one instance of a class
– explaining how a variable is just a reference to an object stored in the computer’s heap
(he
• Accessibility Modifiers
– “private” and
– “public”
– Methods, Method with return values, Method with input variables
if statement
• #region
• Console.WriteLine("Enter 1 or 2 or 3 only");

• string myVaule = Console.ReadLine();

• 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";

• for (int i = 0; i < myArray.Length; i++)


• {
• Console.WriteLine(myArray[i]);

• }

• foreach (string anyThingIwant in myArray)


• {
• Console.WriteLine(anyThingIwant);
• }
• Console.ReadLine();

• #endregion
Creating a new class
• public class Automobile
• {
• public string Make;
• public string Modle;
• public string Color;
• public int Price;
• public int Speed;

• public void methodFromAutoMobile()


• {
• Console.WriteLine("This is from Class Automobile");
• Console.ReadLine();
• }
• }
The creation of an instance is called instantiation. In class-based
programming, objects are created from classes . An object is an
instance of a class, and may be called a class instance
or class object; instantiation is then also known as construction.

Automobile FirstCar = new Automobile();


FirstCar.Make = "Toyota";
FirstCar.Modle = "2017";
FirstCar.Price = 1800000;
FirstCar.Color = "Black";
FirstCar.Speed = 240;
Console.WriteLine("Make " + FirstCar.Make + " Modle " + FirstCar.Modle +
" Price " + FirstCar.Price + " Speed " + FirstCar.Speed + "\n");
Console.ReadLine();
// Calling a method from class automobile
FirstCar.methodFromAutoMobile();
Creating another method and name it as
“Accelerate’ in Automobile class

public void Accelerate()


{
Speed++;
Console.WriteLine("Speed is increasing.....”);
}
Calling this new method in Program.cs

Console.WriteLine("Increasing speed...\n");

Console.ReadLine();
FirstCar.Accelerate();
FirstCar.Accelerate();
FirstCar.Accelerate();

Console.WriteLine("Make: {0} Modle: {1} Price: {2} New


Speed:{3}",FirstCar.Make,FirstCar.Modle,FirstCar.Price,FirstCar.Speed);

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.WriteLine("Make: {0} Modle: {1} Price: {2} New


Speed: {3}", FirstCar.Make, FirstCar.Modle, FirstCar.Price,
FirstCar.Speed);

Console.ReadLine();
Creating two overload methods name
“Discount”in Automobile class
public int Discount(int discount)
{
int price = Price - ((Price /100)* discount);
return price;

public int Discount(int discount, int specialOff)


{
int price = Price-((Price/100)*discount)-specialOff;
return price;
}
Calling method “Discount”in Program.cs

int newPrice = FirstCar.Discount(4);

Console.WriteLine("The Discounted Price is {0} ", newPrice);

Console.ReadLine();
Calling method “Discount”in Program.cs

Console.WriteLine("The Discounted Price is {0} ", FirstCar.Discount(4));

Console.WriteLine("The Discounted Price is {0} ", FirstCar.Discount(4,50000));

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

Automobile[] AutoArray = new Automobile[2];


AutoArray[0] = FirstCar;
AutoArray[1] = secondCar;

foreach (Automobile auto in AutoArray)


{
Console.WriteLine("Make: {0} Modle: {1} Price: {2}
Speed: {3}\n", auto.Make, auto.Modle, auto.Price,
auto.Speed);

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.

Potrebbero piacerti anche