Frank Posted July 15, 2015 Share Posted July 15, 2015 Object-oriented programming is based on scheduling classes; unlike the structured programming, which is centered on the functions. A class is a mold that then create multiple objects with similar characteristics. A class is a template (mold), which defines attributes (variables) and methods (functions) The class defines the attributes and methods common to all objects of that type, but then every object will have its own values and share the same functions. We must create a class before you can create objects (instances) of that class. When you create an object of a class, it is said that an instance of the class or object itself is created. The structure of a class is: class [class' name] {  [class' variables]  [class' functions]  [main] } Problem 1: Make a class load that allows the name and age of a person. Show the data loaded. Print a message if you are elderly (age >= 18) Program: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestClase1 {   class Person   {     private string name;     private int age;     public void Init()     {       Console.Write("Input the Name:");       name = Console.ReadLine();       string line;       Console.Write("Input the Age:");       line = Console.ReadLine();       age = int.Parse(line);     }     public void PPrint()     {       Console.Write("Name:");       Console.WriteLine(name);       Console.Write("Age:");       Console.WriteLine(age);     }     public void Adult()     {       if (age >= 18)       {         Console.Write("Adult");       }       else       {         Console.Write("Not Adult");       }       Console.ReadKey();     }     static void Main(string[] args)     {       Person per1 = new Person();       per1.Init();       per1.PPrint();       per1.Adult();     }   } } } The name of the class must refer to the concept (in this case we named Person): class Person The attributes define them within the class but outside the main: private string nombre; private int edad; We shall see that an attribute is normally defined with the private clause (this does not allow access to the attribute from other classes) Those attributes are accessed from any function or class method (except main) After defining the attributes of the class we must declare methods or functions of the class. The syntax is similar to the main (without the static clause): public void Init()     {       Console.Write("Input the Name:");       name = Console.ReadLine();       string line;       Console.Write("Input the Age:");       line = Console.ReadLine();       age = int.Parse(line);     } In the INIT method (which is the first thing we call in the main) keyboard loaded attributes name and age. As we can see the initialize method may make access to two attributes of the Person class. The second method aims to print the contents of the attributes name and age (data attributes previously loaded to run the initialize method): Console.Write("Name:");       Console.WriteLine(name);       Console.Write("Age:");       Console.WriteLine(age); The third method is intended to display a message if the person is adult or not: public void Adult()     {       if (age >= 18)       {         Console.Write("Adult");       }       else       {         Console.Write("Not Adult");       }       Console.ReadKey();     } Finally in the main declare an object of the Person class and call methods in a proper order: Person per1 = new Person();       per1.Init();       per1.PPrint();       per1.Adult(); Person per1 = new Person(); //Declaration and object creation per1.Init(); //Calling a method Problem 2: Develop a program that loads the sides of a triangle and implement the following methods: initialize the attributes, print the value of the long side and another method that shows whether or not equilateral. Program: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestClase2 {   class Triangle   {     private int side1, side2, side3;     public void Init()     {       string line;       Console.Write("Measure Side 1:");       line = Console.ReadLine();       side1 = int.Parse(line);       Console.Write("Measure Side 2:");       line = Console.ReadLine();       side2 = int.Parse(line);       Console.Write("Measure Side 3:");       line = Console.ReadLine();       side3 = int.Parse(line);     }     public void BiggerSide()     {       Console.Write("Bigger Side:");       if (side1 > side2 && side1 > side3)       {         Console.WriteLine(side1);       }       else       {         if (side2 > side3)         {           Console.WriteLine(side2);         }         else         {           Console.WriteLine(side3);         }       }     }     public void IsEquilateral()     {       if (side1==side2 && side1==side3)       {         Console.Write("It is equilateral");       }       else       {         Console.Write("It is not equilateral");            }     }       static void Main(string[] args)     {       Triangle triangle1 = new Triangle();       triangle1.Init();       triangle1.BiggerSide();       triangle1.IsEquilateral();       Console.ReadKey();     }   } } This problem requires three attributes define where we store integer values of the sides of the triangle: private int side1, side2, side3; The first method to be called from the main Initialize is where we load the three key attributes: public void Init()     {       string line;       Console.Write("Measure Side 1:");       line = Console.ReadLine();       side1 = int.Parse(line);       Console.Write("Measure Side 2:");       line = Console.ReadLine();       side2 = int.Parse(line);       Console.Write("Measure Side 3:");       line = Console.ReadLine();       side3 = int.Parse(line);     } The BiggerSide method shows the highest value of the three integers admitted: public void BiggerSide()     {       Console.Write("Bigger Side:");       if (side1 > side2 && side1 > side3)       {         Console.WriteLine(side1);       }       else       {         if (side2 > side3)         {           Console.WriteLine(side2);         }         else         {           Console.WriteLine(side3);         }       }     } As we can see when a problem becomes more complex and it is easier to separate the different algorithms ordered in various methods and not encode everything in the main. The final method in this class checks whether the three admitted integers are equal: public void IsEquilateral()     {       if (side1==side2 && side1==side3)       {         Console.Write("It is equilateral");       }       else       {         Console.Write("It is not equilateral");            }     } In the main we create an object of the Triangle class and call the respective methods: static void Main(string[] args)     {       Triangle triangle1 = new Triangle();       triangle1.Init();       triangle1.BiggerSide();       triangle1.IsEquilateral();       Console.ReadKey();     } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now