Advertisement
Guest User

OOP First six tasks

a guest
Feb 2nd, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.63 KB | None | 0 0
  1. /*1. Define a class that holds information about a mobile phone device:
  2.  * model, manufacturer, price, owner, battery characteristics (model, hours idle and hours talk) and
  3.  * display characteristics (size and number of colors).
  4.  * Define 3 separate classes (class GSM holding instances of the classes Battery and Display).*/
  5.  
  6. /*2. Define several constructors for the defined classes
  7.  * that take different sets of arguments (the full information for the class or part of it).
  8.  * Assume that model and manufacturer are mandatory (the others are optional). All unknown data fill with null.*/
  9.  
  10. /*3. Add an enumeration BatteryType (Li-Ion, NiMH, NiCd, …) and use it as a new field for the batteries.*/
  11.  
  12. /*4. Add a method in the GSM class for displaying all information about it. Try to override ToString().*/
  13.  
  14. /*5. Use properties to encapsulate the data fields inside the GSM, Battery and Display classes.
  15.  * Ensure all fields hold correct data at any given time.*/
  16.  
  17. /*6. Add a static field and a property IPhone4S in the GSM class to hold the information about iPhone 4S.*/
  18.  
  19. namespace _01.DefineClassesGSM_Battery_Display
  20. {
  21.     using System;
  22.     using System.Text;
  23.  
  24.     public class MainClass
  25.     {
  26.         static void Main()
  27.         {
  28.         }
  29.     }
  30.  
  31.     public class GSM
  32.     {
  33.         private string model;
  34.         private string manufacturer;
  35.         private double? price;
  36.         private string owner;
  37.         private static string iPhone4S;
  38.  
  39.         public GSM(string model, string manufacturer)
  40.         {
  41.             this.Model = model;
  42.             this.Manufacturer = manufacturer;
  43.         }
  44.  
  45.         public GSM(string model, string manufacturer, double? price)
  46.             : this(model, manufacturer)
  47.         {
  48.             this.Price = price;
  49.         }
  50.  
  51.         public GSM(string model, string manufacturer, double? price, string owner)
  52.             : this(model, manufacturer, price)
  53.         {
  54.             this.Owner = owner;
  55.         }
  56.  
  57.         public string Model
  58.         {
  59.             get { return model; }
  60.             set { model = value; }
  61.         }
  62.  
  63.         public string Manufacturer
  64.         {
  65.             get { return manufacturer; }
  66.             set { manufacturer = value; }
  67.         }
  68.  
  69.         public double? Price
  70.         {
  71.             get { return price; }
  72.             set { price = value; }
  73.         }
  74.  
  75.         public string Owner
  76.         {
  77.             get { return owner; }
  78.             set { owner = value; }
  79.         }
  80.  
  81.         public string IPhone4S
  82.         {
  83.             get { return iPhone4S; }
  84.             set { iPhone4S = value; }
  85.         }
  86.  
  87.         Battery newBattery = new Battery("Li-Ion 3100 mAh", 980, 35, BatteryType.LiIon);
  88.  
  89.         Display newDisplay = new Display(5.55, 16000000);
  90.  
  91.         public override string ToString()
  92.         {
  93.             StringBuilder sb = new StringBuilder();
  94.  
  95.             sb.AppendLine("Model: " + this.Model);
  96.             sb.AppendLine("Manufacturer: " + this.Manufacturer);
  97.             sb.AppendLine("Price: " + this.Price + "lv");
  98.             sb.AppendLine("Owner: " + this.Owner);
  99.             sb.AppendLine("Battery model: " + newBattery.Model);
  100.             sb.AppendLine("Battery type: " + newBattery.Type);
  101.             sb.AppendLine("Hours idle: " + newBattery.HoursIdle);
  102.             sb.AppendLine("Hours talk: " + newBattery.HoursTalk);
  103.             sb.AppendLine("Display size: " + newDisplay.Size);
  104.             sb.AppendLine("Display colors: " + newDisplay.NumberOfColors);
  105.  
  106.             return sb.ToString();
  107.         }
  108.     }
  109.  
  110.     public enum BatteryType
  111.     {
  112.         LiIon,
  113.         NiMH,
  114.         NiCd
  115.     }
  116.  
  117.     public class Battery
  118.     {
  119.         private string model;
  120.         private double? hoursIdle;
  121.         private double? hoursTalk;
  122.         private BatteryType type;
  123.  
  124.         public Battery()
  125.         {
  126.  
  127.         }
  128.  
  129.         public Battery(string model)
  130.         {
  131.             this.Model = model;
  132.         }
  133.  
  134.         public Battery(string model, double? hoursIdle)
  135.             : this(model)
  136.         {
  137.             this.HoursIdle = hoursIdle;
  138.         }
  139.  
  140.         public Battery(string model, double? hoursIdle, double? hoursTalk)
  141.             : this(model, hoursIdle)
  142.         {
  143.             this.HoursTalk = hoursTalk;
  144.         }
  145.  
  146.         public Battery(string model, double? hoursIdle, double? hoursTalk, BatteryType type)
  147.             : this(model, hoursIdle, hoursTalk)
  148.         {
  149.             this.Type = type;
  150.         }
  151.  
  152.         public string Model
  153.         {
  154.             get { return model; }
  155.             set { model = value; }
  156.         }
  157.  
  158.         public double? HoursIdle
  159.         {
  160.             get { return hoursIdle; }
  161.             set { hoursIdle = value; }
  162.         }
  163.  
  164.         public double? HoursTalk
  165.         {
  166.             get { return hoursTalk; }
  167.             set { hoursTalk = value; }
  168.         }
  169.  
  170.         public BatteryType Type
  171.         {
  172.             get { return type; }
  173.             set { type = value; }
  174.         }
  175.     }
  176.  
  177.     public class Display
  178.     {
  179.         private double? size;
  180.         private int? numberOfColors;
  181.  
  182.         public Display()
  183.         {
  184.  
  185.         }
  186.  
  187.         public Display(double? size)
  188.         {
  189.             this.Size = size;
  190.         }
  191.  
  192.         public Display(double? size, int? numberOfColors)
  193.             : this(size)
  194.         {
  195.             this.NumberOfColors = numberOfColors;
  196.         }
  197.  
  198.         public double? Size
  199.         {
  200.             get { return size; }
  201.             set { size = value; }
  202.         }
  203.  
  204.         public int? NumberOfColors
  205.         {
  206.             get { return numberOfColors; }
  207.             set { numberOfColors = value; }
  208.         }
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement