You are on page 1of 38

Assignment C sharp

Q-3 TestClasses.cs

Source Code
using System;

namespace TestCircles
{
class Circle
{ //Declare variable
public double dbDiameter;
//Declare variable
public double dbArea;
//Declare variable
public double dbRadius = 1;
//Getter and Setter Radius
public double Radius
{//getter
get
{
return dbRadius;
}
//Setter
set
{
dbRadius = value;
//Call CalculateArea method
CalculateArea();
//Call CalculateDiamter method
CalculateDiameter();
}
}

//This method will calculate the Diameter of Circle


private void CalculateDiameter()
{
//Calculate diameter of radius
dbDiameter = dbRadius * 2;
}

//This method will calculate the Area of Circle


private void CalculateArea()
{
//Calculate area of radius
dbArea = Math.PI * (dbRadius * dbRadius);
}
//Constructor of Class which take one argument
public Circle(double radius)
{
Radius = radius;
}
//Constructor of Circle class
public Circle()
{
//Call CalculateArea Method
CalculateArea();
//Call CalculateDiameter Method
CalculateDiameter();
}
}
//TestCircle Class
class TestCircles
{
//Main Method
static void Main(string[] args)
{
//Three object of circle class which take three different values of radius
Circle ObjCircle1 = new Circle(2.1);
Circle ObjCircle2 = new Circle(5.0);
Circle ObjCircle3 = new Circle();
//Print radius of circle 1
Console.WriteLine("The radius of Circle 1 is: {0}" , ObjCircle1.Radius);
//Print radius of circle 2
Console.WriteLine("The radius of Circle 2 is: {0}", ObjCircle2.Radius);
//Print radius of circle 3
Console.WriteLine("The radius of Circle 3 is: {0}", ObjCircle3.Radius);
//Print area of circle 1
Console.WriteLine("The area of Circle 1 is: {0}",
Math.Round(ObjCircle1.dbArea, 2));
//Print area of circle 2
Console.WriteLine("The area of Circle 2 is: {0}",
Math.Round(ObjCircle2.dbArea, 2));
//Print area of circle 3
Console.WriteLine("The area of Circle 3 is: {0}",
Math.Round(ObjCircle3.dbArea, 2));
//Print Diameter of circle 1
Console.WriteLine("The diameter of Circle 1 is: {0}", ObjCircle1.dbDiameter);
//Print Diameter of circle 2
Console.WriteLine("The diameter of Circle 2 is: {0}", ObjCircle2.dbDiameter);
//Print Diameter of circle 3
Console.WriteLine("The diameter of Circle 3 is: {0}", ObjCircle3.dbDiameter);

Console.ReadLine();
}
}
}

Output
Q-4 DemoSquares.cs

Source Code
using System;
namespace DemoSquares
{
class Square
{ //declare variables
public double dbLength;
public double dbArea;
//constructor of square class
public Square()
{
//intializing to 1 because its from 1 to 10
Length = 1;
}
// length getter and setter
public double Length
{
//Getter that return length
get
{
return dbLength;
}//Setter that call area method
set
{
dbLength = value;
//Call area method
calculateArea();
}
}
//This will return area
public double Area
{
get
{
//return area
return dbArea;
}
}
//This nethod will calculate Area of Sqaure
private void calculateArea()
{
//Calculate Area
dbArea = dbLength * dbLength;
}
}
public class DemoSquares
{
public static void Main()
{
//Square Array
Square[] arrSquare = new Square[10];
//This loop will initialize the array elements
for (int i = 0; i < arrSquare.Length; ++i)
{
arrSquare[i] = new Square();
}
//Initialize length using array
arrSquare[1].Length = 2;
//Initialize length using array
arrSquare[2].Length = 3;
//Initialize length using array
arrSquare[3].Length = 4;
//Initialize length using array
arrSquare[4].Length = 5;
//Initialize length using array
arrSquare[5].Length = 6;
//Initialize length using array
arrSquare[6].Length = 7;
//Initialize length using array
arrSquare[7].Length = 8;
//Initialize length using array
arrSquare[8].Length = 9;
//Initialize length using array
arrSquare[9].Length = 10;
//Prrint length and area of square from 1 to 10
Console.WriteLine(" Square 1 length is {0},and area is {1}",
arrSquare[0].dbLength, arrSquare[0].dbArea);
Console.WriteLine(" Square 2 length is {0},and area is {1}",
arrSquare[1].dbLength, arrSquare[1].dbArea);
Console.WriteLine(" Square 3 length is {0},and area is {1}",
arrSquare[2].dbLength, arrSquare[2].dbArea);
Console.WriteLine(" Square 4 length is {0},and area is {1}",
arrSquare[3].dbLength, arrSquare[3].dbArea);
Console.WriteLine(" Square 5 length is {0},and area is {1}",
arrSquare[4].dbLength, arrSquare[4].dbArea);
Console.WriteLine(" Square 5 length is {0},and area is {1}",
arrSquare[5].dbLength, arrSquare[5].dbArea);
Console.WriteLine(" Square 5 length is {0},and area is {1}",
arrSquare[6].dbLength, arrSquare[6].dbArea);
Console.WriteLine(" Square 5 length is {0},and area is {1}",
arrSquare[7].dbLength, arrSquare[7].dbArea);
Console.WriteLine(" Square 5 length is {0},and area is {1}",
arrSquare[8].dbLength, arrSquare[8].dbArea);
Console.WriteLine(" Square 5 length is {0},and area is {1}",
arrSquare[9].dbLength, arrSquare[9].dbArea);
Console.ReadLine();
}
}
}
Output

Q-3 a) OrderDemo.cs

Source Code
using System;
namespace OrderDemo
{
public class Order
{
//Declare Variables
int intOrderNumber;
string strCustomerName;
int intQuantityOrdered;
double dbTotalPrice;
//Default Constructor
public Order()
{ //Assign 0 to order number
OrderNumber = 0;
//Assign 0 to quantity order
QuantityOrder = 0;
//Assign empty string
CustomerName = string.Empty;
}
//Getter and Setter for OrderNumber
public int OrderNumber
{
get { return intOrderNumber; }
set { intOrderNumber = value; }
}
//Getter and Setter for QuantityOrder
public int QuantityOrder
{
get { return intQuantityOrdered; }
set { intQuantityOrdered = value;
//Calculate Total Price
dbTotalPrice = intQuantityOrdered * 19.95;
}
}
//Getter for OrderNumber
public double TotalPrice
{
get { return dbTotalPrice; }

}
//Getter and Setter for CustomerName
public string CustomerName
{
get { return strCustomerName; }
set { strCustomerName = value; }
}
//Override method to return order number
public override int GetHashCode()
{
return intOrderNumber;
}
//This method will determine if two orders are same
public void Equals(Order obj)
{
//Condition
if (this.OrderNumber == obj.OrderNumber)
{//Print message
Console.WriteLine("Equal orders");
}
else
{//Print message
Console.WriteLine("Orders are not Equal");
}
}
//This method will print Order Details
public override string ToString()
{
return CustomerName + " " + GetHashCode() + " " + QuantityOrder + " " +
TotalPrice;

}
}
class Program
{
static void Main(string[] args)
{
//Three object of Order Class
Order order1 = new Order();
Order order2 = new Order();
Order order3 = new Order();
//Assign Order Number
order1.OrderNumber = 1;
//Put Customer Name
order1.CustomerName = "David";
//Assign Quantity Order
order1.QuantityOrder = 2;
//Print Order 1
Console.WriteLine("Order Details: {0}", order1);
//Assign Order Number
order2.OrderNumber = 5;
//Put Customer Name
order2.CustomerName = "Jack";
//Assign Quantity Order
order2.QuantityOrder = 4;
//Print Order 2
Console.WriteLine("Order Details: {0}", order2);
//Assign Order Number
order3.OrderNumber = 1;
//Put Customer Name
order3.CustomerName = "John";
//Assign Quantity Order
order3.QuantityOrder = 2;
//Print Order 3
Console.WriteLine("Order Details: {0}", order3);
//Check two orders are equal
order1.Equals(order3);
Console.ReadLine();
}
}
}

Output

Q-3 b) OrderDemo2.cs

Source Code
using System;
namespace OrderDemo
{
public class Order
{
//Declare Variables
int intOrderNumber;
string strCustomerName;
int intQuantityOrdered;
double dbTotalPrice;
//Default Constructor
public Order()
{ //Assign 0 to order number
OrderNumber = 0;
//Assign 0 to quantity order
QuantityOrder = 0;
//Assign empty string
CustomerName = string.Empty;
}
//Getter and Setter for OrderNumber
public int OrderNumber
{
get { return intOrderNumber; }
set { intOrderNumber = value; }
}
//Getter and Setter for QuantityOrder
public int QuantityOrder
{
get { return intQuantityOrdered; }
set { intQuantityOrdered = value;
//Calculate Total Price
dbTotalPrice = intQuantityOrdered * 19.95;
}
}
//Getter for OrderNumber
public double TotalPrice
{
get { return dbTotalPrice; }

}
//Getter and Setter for CustomerName
public string CustomerName
{
get { return strCustomerName; }
set { strCustomerName = value; }
}
//Override method to return order number
public override int GetHashCode()
{
return intOrderNumber;
}
//This method will determine if two orders are same
public bool Equals(Order[] obj)
{ //Declare variable
int temp = 0;
//Use foreach to check multiple orders
foreach(Order a in obj)
{ //if OrderNumber are same
if (this.GetHashCode() == a.GetHashCode())
{//increment
temp++;
}

}
//if temp greater than 1 return true
if(temp>1)
{ return true; }
else
{ return false; }

}
//This method will print Order Details
public override string ToString()
{
return CustomerName + " " + GetHashCode() + " " + QuantityOrder + " " +
TotalPrice;

}
}
class OrderDemo
{
static void Main(string[] args)
{ //Array of 5 order objects
Order[] arrOrder= new Order[5];
//This loop will make 5 objects
for(int i = 0; i<5; i++)
{
arrOrder[i] = new Order();
}
//This loop ask user to enter details about the order
for (int i = 0; i < 5; i++)
{ //Ask the user to enter the order number
Console.WriteLine("Enter Order Number");
arrOrder[i].OrderNumber = Convert.ToInt32(Console.ReadLine());
//if two order orders number are same
if (arrOrder[i].Equals(arrOrder))
{//show error message
Console.WriteLine("Order Already Exist");
//decrement
i--;
}

else
{ //Ask the user to enter the ocustomer name
Console.WriteLine("Enter Customer Name");
arrOrder[i].CustomerName = Console.ReadLine();
//Ask the user to enter the quantity number
Console.WriteLine("Enter Quantity Number");
arrOrder[i].QuantityOrder = Convert.ToInt32(Console.ReadLine());
}
}
//print the line
Console.WriteLine();
//This loop will print order details
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Order Details: {0}", arrOrder[i]);
}
//declare variable
double count = 0;
//This loop will calculate the Total price of orders
for (int i = 0; i < 5; i++)
{
count = count + arrOrder[i].TotalPrice;
}
//Print Total Price
Console.WriteLine("Total of all Orders: {0}", count);

Console.ReadLine();
}
}
}
Output

Q-3 c) OrderDemo3.cs

Source Code
using System;
namespace OrderDemo
{
public class Order
{
//Declare Variables
public int intOrderNumber;
public string strCustomerName;
public int intQuantityOrdered;
public double dbTotalPrice;
//Default Constructor
public Order()
{ //Assign 0 to order number
OrderNumber = 0;
//Assign 0 to quantity order
QuantityOrder = 0;
//Assign empty string
CustomerName = string.Empty;
}
//Getter and Setter for OrderNumber
public int OrderNumber
{
get { return intOrderNumber; }
set { intOrderNumber = value; }
}
//Getter and Setter for QuantityOrder
public virtual int QuantityOrder
{
get { return intQuantityOrdered; }
set { intQuantityOrdered = value;
//Calculate Total Price
dbTotalPrice = intQuantityOrdered * 19.95;
}
}
//Getter for OrderNumber
public double TotalPrice
{
get { return dbTotalPrice; }

}
//Getter and Setter for CustomerName
public string CustomerName
{
get { return strCustomerName; }
set { strCustomerName = value; }
}
//Override method to return order number
public override int GetHashCode()
{
return intOrderNumber;
}
//This method will determine if two orders are same
public bool Equals(Order[] obj)
{ //Declare variable
int temp = 0;
//Use foreach to check multiple orders
foreach(Order a in obj)
{ //if OrderNumber are same
if (this.GetHashCode() == a.GetHashCode())
{//increment
temp++;
}
}
//if temp greater than 1 return true
if(temp>1)
{ return true; }
else
{ return false; }

}
//This method will print Order Details
public override string ToString()
{
return CustomerName + " " + GetHashCode() + " " + QuantityOrder + " " +
TotalPrice;

}
}
class ShippedOrder: Order
{
public override int QuantityOrder { get { return intOrderNumber; }
set {
intQuantityOrdered = value;
//Calculate Total Price with shipped charges
dbTotalPrice = intQuantityOrdered * 19.95+4;
} }
}
class OrderDemo4
{
static void Main(string[] args)
{ //Array of 5 shipped order objects
ShippedOrder[] arrOrder= new ShippedOrder[5];
//This loop will make 5 objects
for(int i = 0; i<5; i++)
{
arrOrder[i] = new ShippedOrder();
}
//This loop ask user to enter details about the order
for (int i = 0; i < 5; i++)
{ //Ask the user to enter the order number
Console.WriteLine("Enter Order Number");
arrOrder[i].OrderNumber = Convert.ToInt32(Console.ReadLine());
//if two order orders number are same
if (arrOrder[i].Equals(arrOrder))
{//show error message
Console.WriteLine("Order Already Exist");
//decrement
i--;
}
else
{ //Ask the user to enter the ocustomer name
Console.WriteLine("Enter Customer Name");
arrOrder[i].CustomerName = Console.ReadLine();
//Ask the user to enter the quantity number
Console.WriteLine("Enter Quantity Number");
arrOrder[i].QuantityOrder = Convert.ToInt32(Console.ReadLine());
}
}
//print the line
Console.WriteLine();
//This loop will print order details
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Order Details: {0}", arrOrder[i]);
}
//declare variable
double count = 0;
//This loop will calculate the Total price of orders
for (int i = 0; i < 5; i++)
{
count = count + arrOrder[i].TotalPrice;
}
//Print Total Price
Console.WriteLine("Total of all Orders: {0}", count);

Console.ReadLine();
}
}
}
Output

Q-3 d) OrderDemo3.cs
Source Code
using System;
namespace OrderDemo
{
public class Order
{
//Declare Variables
public int intOrderNumber;
public string strCustomerName;
public int intQuantityOrdered;
public double dbTotalPrice;
//Default Constructor
public Order()
{ //Assign 0 to order number
OrderNumber = 0;
//Assign 0 to quantity order
QuantityOrder = 0;
//Assign empty string
CustomerName = string.Empty;
}
//Getter and Setter for OrderNumber
public int OrderNumber
{
get { return intOrderNumber; }
set { intOrderNumber = value; }
}
//Getter and Setter for QuantityOrder
public virtual int QuantityOrder
{
get { return intQuantityOrdered; }
set { intQuantityOrdered = value;
//Calculate Total Price
dbTotalPrice = intQuantityOrdered * 19.95;
}
}
//Getter for OrderNumber
public double TotalPrice
{
get { return dbTotalPrice; }

}
//Getter and Setter for CustomerName
public string CustomerName
{
get { return strCustomerName; }
set { strCustomerName = value; }
}
//Override method to return order number
public override int GetHashCode()
{
return intOrderNumber;
}
//This method will determine if two orders are same
public bool Equals(Order[] obj)
{ //Declare variable
int temp = 0;
//Use foreach to check multiple orders
foreach(Order a in obj)
{ //if OrderNumber are same
if (this.GetHashCode() == a.GetHashCode())
{//increment
temp++;
}

}
//if temp greater than 1 return true
if(temp>1)
{ return true; }
else
{ return false; }

}
//This method will print Order Details
public override string ToString()
{
return CustomerName + " " + GetHashCode() + " " + QuantityOrder + " " +
TotalPrice;

}
}
class ShippedOrder: Order
{
public override int QuantityOrder { get { return intOrderNumber; }
set {
intQuantityOrdered = value;
//Calculate Total Price with shipped charges
dbTotalPrice = intQuantityOrdered * 19.95+4;
} }
}
class OrderDemo3
{
static void Main(string[] args)
{ //Array of 5 shipped order objects
ShippedOrder[] arrOrder= new ShippedOrder[5];
//This loop will make 5 objects
for(int i = 0; i<5; i++)
{
arrOrder[i] = new ShippedOrder();
}
//This loop ask user to enter details about the order
for (int i = 0; i < 5; i++)
{ //Ask the user to enter the order number
Console.WriteLine("Enter Order Number");
arrOrder[i].OrderNumber = Convert.ToInt32(Console.ReadLine());
//if two order orders number are same
if (arrOrder[i].Equals(arrOrder))
{//show error message
Console.WriteLine("Order Already Exist");
//decrement
i--;
}

else
{ //Ask the user to enter the ocustomer name
Console.WriteLine("Enter Customer Name");
arrOrder[i].CustomerName = Console.ReadLine();
//Ask the user to enter the quantity number
Console.WriteLine("Enter Quantity Number");
arrOrder[i].QuantityOrder = Convert.ToInt32(Console.ReadLine());
}
}
//print the line
Console.WriteLine();
//This loop will print order details
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Order Details: {0}", arrOrder[i]);
}
//object of Shipped Order Class
ShippedOrder objtemp = new ShippedOrder();

//These loops will sort the order details


for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5-1; j++)
{
if (arrOrder[j].OrderNumber > arrOrder[j + 1].OrderNumber)
{
objtemp = arrOrder[j + 1];
arrOrder[j + 1] = arrOrder[j];
arrOrder[j] = objtemp;
}
}
}

//Display Sorted Order


Console.WriteLine("Sorted Order Details");
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Order Details: {0}", arrOrder[i]);
}

//declare variable
double count = 0;
//This loop will calculate the Total price of orders
for (int i = 0; i < 5; i++)
{
count = count + arrOrder[i].TotalPrice;
}
//Print Total Price
Console.WriteLine("Total of all Orders: {0}", count);

Console.ReadLine();
}
}
}

Output
Q-5 a) PatientDemo.cs

Source Code
using System;

namespace Patient
{
public class Patient
{
//Declare Variables
public int intIDNumber;
public string strPatientName;
public int intAge;
public double dbAmountDue;
//Default Constructor
public Patient()
{ //Assign 0 to id number
IDNumber = 0;
//Assign 0 to age
Age = 0;
//Assign empty string
PatientName = string.Empty;
}
//Getter and Setter for IDNumber
public int IDNumber
{
get { return intIDNumber; }
set { intIDNumber = value; }
}
//Getter and Setter for Age
public virtual int Age
{
get { return intAge; }
set
{
intAge = value;

}
}
//Getter and setter for Amount Due
public double AmountDue
{
get { return dbAmountDue; }
set
{
dbAmountDue= value;

}
}
//Getter and Setter for Name
public string PatientName
{
get { return strPatientName; }
set { strPatientName = value; }
}
//Override method to return ID number
public override int GetHashCode()
{
return intIDNumber;
}
//This method will determine if two ID numbers are same
public bool Equals(Patient[] obj)
{ //Declare variable
int temp = 0;
//Use foreach to check multiple same id numbers
foreach (Patient a in obj)
{ //if IDNumber are same
if (this.GetHashCode() == a.GetHashCode())
{//increment
temp++;
}

}
//if temp greater than 1 return true
if (temp > 1)
{ return true; }
else
{ return false; }

}
//This method will print Patient Details
public override string ToString()
{
return PatientName + " " + GetHashCode() + " " + Age + " " + AmountDue;

}
}

class Program
{
static void Main(string[] args)
{
//Array of 5 Patient objects
Patient[] arrPatient = new Patient[5];
//This loop will make 5 objects
for (int i = 0; i < 5; i++)
{
arrPatient[i] = new Patient();
}
//This loop ask user to enter details about the patient
for (int i = 0; i < 5; i++)
{ //Ask the user to enter the ID Number
Console.Write("Enter Patient ID Number: ");
arrPatient[i].IDNumber = Convert.ToInt32(Console.ReadLine());
//if two patient number are same
if (arrPatient[i].Equals(arrPatient))
{//show error message
Console.WriteLine("Patient Already Exist");
//decrement
i--;
}

else
{ //Ask the user to enter the patient name
Console.Write("Enter Patient Name: ");
arrPatient[i].PatientName = Console.ReadLine();
//Ask the user to enter the age
Console.Write("Enter Age: ");
arrPatient[i].Age = Convert.ToInt32(Console.ReadLine());
//Ask the user to enter the Amount Due
Console.Write("Enter Amount Due: ");
arrPatient[i].AmountDue = Convert.ToDouble(Console.ReadLine());
}
}
//print the line
Console.WriteLine();
//This loop will print Patient details
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Patient Details: {0}", arrPatient[i]);
}
//object of Patient Class
Patient objtemp = new Patient();

//These loops will sort the patient details


for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - 1; j++)
{
if (arrPatient[j].IDNumber > arrPatient[j + 1].IDNumber)
{
objtemp = arrPatient[j + 1];
arrPatient[j + 1] = arrPatient[j];
arrPatient[j] = objtemp;
}
}
}

//Display Sorted Details


Console.WriteLine("Sorted Patient Details");
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Patient Details: {0}", arrPatient[i]);
}

Console.ReadLine();

}
}
}
Output
Q-5 b) PatientDemo2.cs

Source Code
using System;
using System.Collections.Generic;
using System.Text;

namespace Patient1
{

// C# program to create Patient class hierarchy and test them


using System;

// base class Patient


class Patient
{
// attributes
private string id;
private string name;
private int age;
private double amountDue;

// constructor to initialize the fields


public Patient(string id, string name, int age, double amountDue)
{
this.id = id;
this.name = name;
this.age = age;
this.amountDue = amountDue;
}

// get and set properties


public string ID
{
get
{
return id;
}

set
{

id = value;
}
}

public string Name


{
get
{
return name;
}

set
{
name = value;
}
}

public int Age


{
get
{
return age;
}

set
{
age = value;
}
}

public double AmountDue


{
get
{
return amountDue;
}

set
{
amountDue = value;
}
}

// override ToString method to return String representation of the Patient


public override string ToString()
{
return "Patient ID: " + id + "\nName: " + name + "\nAge: " + age + "\nAmount
Due: $" + amountDue.ToString("0.00");
}
}

// derived class InsuredPatient


class InsuredPatient : Patient
{
// additional attributes
private string insuranceCompany;
private int percentagePaid;

// constructor to initialize the fields, calling the base class constructor


public InsuredPatient(string id, string name, int age, double amountDue, string
insuranceCompany) : base(id, name, age, amountDue)
{
this.insuranceCompany = insuranceCompany; // set the insuranceCompany

// based on insurance company, set the percentagePaid


if (this.insuranceCompany.Equals("Wrightstown Mutual"))
percentagePaid = 80;
else if (this.insuranceCompany.Equals("Red Umbrella"))
percentagePaid = 60;
else
percentagePaid = 25;
}
// get and set property
public string InsuranceCompany
{

get
{
return insuranceCompany;
}

set
{
// when updating the insurance company, update the percentagePaid
insuranceCompany = value;
if (insuranceCompany.Equals("Wrightstown Mutual"))
percentagePaid = 80;
else if (insuranceCompany.Equals("Red Umbrella"))
percentagePaid = 60;
else
percentagePaid = 25;
}
}

// override ToString method to return the String representation of InsuredPatient


public override string ToString()
{
// calculate the amount to pay after applying insurance
double netAmountDue = AmountDue - ((AmountDue * percentagePaid) / 100);

return base.ToString() + "\nInsurance Company: " + insuranceCompany +


"\nPercentage Paid: " + percentagePaid + "%"
+ "\nAmount due after insurance: $" + netAmountDue.ToString("0.00");
}

// PatientDemo class to test the Patient and InsuredPatient classes


class PatientDemo
{
static void Main()
{

// create objects of Patient and InsuredPatient class


Patient pat1 = new Patient("12", "Tom", 32, 1000);
InsuredPatient pat2 = new InsuredPatient("13", "Bob", 21, 850, "Red
Umbrella");

// display the details of the classes


Console.WriteLine(pat1.ToString());
Console.WriteLine();
Console.WriteLine(pat2.ToString());

Console.ReadLine();

}
}
}
Output

Q-5 c) PatientDemo3.cs

Source Code
using System;
using System.Collections.Generic;
using System.Text;

namespace Patient1
{

// C# program to create Patient class hierarchy and test them


using System;

// base class Patient


class Patient
{
// attributes
private string id;
private string name;
private int age;
private double amountDue;

// constructor to initialize the fields


public Patient(string id, string name, int age, double amountDue)
{
this.id = id;
this.name = name;
this.age = age;
this.amountDue = amountDue;
}

// get and set properties


public string ID
{
get
{
return id;
}

set
{

id = value;
}
}

public string Name


{
get
{
return name;
}

set
{
name = value;
}
}

public int Age


{
get
{
return age;
}

set
{
age = value;
}
}

public double AmountDue


{
get
{
return amountDue;
}

set
{
amountDue = value;
}
}

// override ToString method to return String representation of the Patient


public override string ToString()
{
return "Patient ID: " + id + "\nName: " + name + "\nAge: " + age + "\nAmount
Due: $" + amountDue.ToString("0.00");
}
}

// derived class InsuredPatient


class InsuredPatient : Patient
{
// additional attributes
private string insuranceCompany;
private int percentagePaid;

// constructor to initialize the fields, calling the base class constructor


public InsuredPatient(string id, string name, int age, double amountDue, string
insuranceCompany) : base(id, name, age, amountDue)
{
this.insuranceCompany = insuranceCompany; // set the insuranceCompany

// based on insurance company, set the percentagePaid


if (this.insuranceCompany.Equals("Wrightstown Mutual"))
percentagePaid = 80;
else if (this.insuranceCompany.Equals("Red Umbrella"))
percentagePaid = 60;
else
percentagePaid = 25;
}

// get and set property


public string InsuranceCompany
{

get
{
return insuranceCompany;
}

set
{
// when updating the insurance company, update the percentagePaid
insuranceCompany = value;
if (insuranceCompany.Equals("Wrightstown Mutual"))
percentagePaid = 80;
else if (insuranceCompany.Equals("Red Umbrella"))
percentagePaid = 60;
else
percentagePaid = 25;
}
}

// override ToString method to return the String representation of InsuredPatient


public override string ToString()
{
// calculate the amount to pay after applying insurance
double netAmountDue = AmountDue - ((AmountDue * percentagePaid) / 100);
double quartlyDue = netAmountDue / 4;
return base.ToString() + "\nInsurance Company: " + insuranceCompany +
"\nPercentage Paid: " + percentagePaid + "%"
+ "\nAmount due after insurance: $" + netAmountDue.ToString("0.00"+"\nQuartly
Due: "+quartlyDue.ToString() );
}

// PatientDemo class to test the Patient and InsuredPatient classes


class PatientDemo
{
static void Main()
{

// create objects of Patient and InsuredPatient class


Patient pat1 = new Patient("12", "Tom", 32, 1000);
InsuredPatient pat2 = new InsuredPatient("13", "Bob", 21, 850, "Red
Umbrella");

// display the details of the classes


Console.WriteLine(pat1.ToString());
Console.WriteLine();
Console.WriteLine(pat2.ToString());

Console.ReadLine();

}
}
}

Output

Example Student
CreateStudent.cs

Source Code
using System;

namespace StudentExample
{
class Student
{
private int idNumber;
private string lastName;
private double gradePointAverage;
public const double HIGHEST_GPA = 4.0;
public const double LOWEST_GPA = 0.0;
public int IdNumber
{
get
{
return idNumber;
}
set
{
idNumber = value;
}
}
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}

public double GradePointAverage


{
get
{
return gradePointAverage;
}
set
{
if (value >= LOWEST_GPA && value <= HIGHEST_GPA)
gradePointAverage = value;
else
gradePointAverage = LOWEST_GPA;
}
}

}
class CreateStudent
{
static void Main(string[] args)
{

Student first = new Student();


Student second = new Student();
first.IdNumber = 123;
first.LastName = "Anderson";
first.GradePointAverage = 3.5;
second.IdNumber = 789;
second.LastName = "Daniels";
second.GradePointAverage = 4.1;
Display(first);
Display(second);
Console.ReadLine();
}
internal static void Display(Student stu)
{
Console.WriteLine("{0,5} {1,-10}{2,6}",
stu.IdNumber, stu.LastName,
stu.GradePointAverage.ToString("F1"));
}
}
}

Ouput

CreateStudent2.cs

Source Code
using System;

namespace StudentExample
{
class Student
{
private int idNumber;
private string lastName;
private double gradePointAverage;
public const double HIGHEST_GPA = 4.0;
public const double LOWEST_GPA = 0.0;
public int IdNumber { get; set; }
public string LastName { get; set; }
public Student(int id, string name, double gpa)
{
IdNumber = id;
LastName = name;
GradePointAverage = gpa;
}
public Student() : this(0, "XXX", 0.0)
{
}
public double GradePointAverage
{
get
{
return gradePointAverage;
}
set
{
if (value >= LOWEST_GPA && value <= HIGHEST_GPA)
gradePointAverage = value;
else
gradePointAverage = LOWEST_GPA;
}
}
}
class CreateStudent2
{
static void Main(string[] args)
{
Student first = new Student();
Student second = new Student();
first.IdNumber = 123;
first.LastName = "Anderson";
first.GradePointAverage = 3.5;
second.IdNumber = 789;
second.LastName = "Daniels";
second.GradePointAverage = 4.1;
Student third = new Student(456, "Marco", 2.4);
Student fourth = new Student();

Display(third);
Display(fourth);
Display(first);
Display(second);
Console.ReadLine();
}
internal static void Display(Student stu)
{
Console.WriteLine("{0,5} {1,-10}{2,6}",
stu.IdNumber, stu.LastName,
stu.GradePointAverage.ToString("F1"));

}
}
}

Output

CreateStudent3.cs

Source code
using System;
namespace StudentExample
{
class Student : IComparable
{
private int idNumber;
private string lastName;
private double gradePointAverage;
public const double HIGHEST_GPA = 4.0;
public const double LOWEST_GPA = 0.0;
public int IdNumber { get; set; }
public string LastName { get; set; }
public Student(int id, string name, double gpa)
{
IdNumber = id;
LastName = name;
GradePointAverage = gpa;
}
public Student() : this(0, "XXX", 0.0)
{
}
public double GradePointAverage
{
get
{
return gradePointAverage;
}
set
{
if (value >= LOWEST_GPA && value <= HIGHEST_GPA)
gradePointAverage = value;
else
gradePointAverage = LOWEST_GPA;
}
}
int IComparable.CompareTo(Object o)
{
int returnVal;
Student temp = (Student)o;
if (this.IdNumber > temp.IdNumber)
returnVal = 1;
else
if (this.IdNumber < temp.IdNumber)
returnVal = -1;
else
returnVal = 0;
return returnVal;
}
}
class CreateStudent3
{
static void Main(string[] args)
{
Student[] student = new Student[3];
int x;
int id;
string name;
double gpa;
for (x = 0; x < student.Length; ++x)
{
GetData(out id, out name, out gpa);
student[x] = new Student(id, name, gpa);
}
Array.Sort(student);
Console.WriteLine("Sorted List:");
for (x = 0; x < student.Length; ++x)
Display(student[x]);
Console.ReadLine();
}
internal static void GetData(out int id, out string name,
out double gpa)
{
string inString;
Console.Write("Please enter student ID number ");
inString = Console.ReadLine();
id = Convert.ToInt32(inString);
Console.Write("Please enter last name for " +
"student {0} ", id);
name = Console.ReadLine();
Console.Write("Please enter grade point average ");
inString = Console.ReadLine();
gpa = Convert.ToDouble(inString);
}

internal static void Display(Student stu)


{
Console.WriteLine("{0,5} {1,-10}{2,6}",
stu.IdNumber, stu.LastName,
stu.GradePointAverage.ToString("F1"));

}
}
}

Output
Loan Example

DemoLoan.cs
Source Code
using System;

namespace DemoLoan
{
class Loan
{
public int LoanNumber { get; set; }
public string LastName { get; set; }

public double LoanAmount { get; set; }


}
class DemoLoan
{
static void Main(string[] args)
{
Loan aLoan = new Loan();
aLoan.LoanNumber = 2239;
aLoan.LastName = "Mitchell";
aLoan.LoanAmount = 1000.00;
Console.WriteLine("Loan #{0} for {1} is for {2}",
aLoan.LoanNumber, aLoan.LastName,
aLoan.LoanAmount.ToString("C2"));

Console.ReadLine();
}
}
}

Output

DemoCarLoan.cs

Source Code
using System;

namespace DemoLoan
{
class Loan
{
public int LoanNumber { get; set; }
public string LastName { get; set; }
public double LoanAmount { get; set; }
}
class CarLoan : Loan
{
public int Year { get; set; }
public string Make { get; set; }
}
class DemoCarLoan
{
static void Main(string[] args)
{
CarLoan aCarLoan = new CarLoan();
aCarLoan.LoanNumber = 3358;
aCarLoan.LastName = "Jansen";
aCarLoan.LoanAmount = 20000.00;
aCarLoan.Make = "Ford";
aCarLoan.Year = 2005;
Console.WriteLine("Loan #{0} for {1} is for {2}",
aCarLoan.LoanNumber, aCarLoan.LastName,
aCarLoan.LoanAmount.ToString("C2"));
Console.WriteLine(" Loan #{0} is for a {1} {2}",
aCarLoan.LoanNumber, aCarLoan.Year,
aCarLoan.Make);
Console.ReadLine();
}
}
}
Output

DemoCarLoan2.cs

Source Code
using System;

namespace DemoLoan
{
class Loan
{
public int LoanNumber { get; set; }
public string LastName { get; set; }
public double LoanAmount
{
set
{
if (value < MINIMUM_LOAN)
loanAmount = MINIMUM_LOAN;
else
loanAmount = value;
}
get
{
return loanAmount;
}
}
public const double MINIMUM_LOAN = 5000;
protected double loanAmount;
}
class CarLoan : Loan
{
private int year;
public string Make { get; set; }

private const int EARLIEST_YEAR = 2006;


private const int LOWEST_INVALID_NUM = 5000;

public int Year


{
set
{
if (value < EARLIEST_YEAR)
{
year = value;
loanAmount = 0;
}
else
year = value;
}
get
{
return year;
}
}
public new int LoanNumber
{
get
{
return base.LoanNumber;
}
set
{
if (value < LOWEST_INVALID_NUM)
base.LoanNumber = value;
else
base.LoanNumber = value % LOWEST_INVALID_NUM;
}
}
}
class DemoCarLoan2
{
static void Main(string[] args)
{
CarLoan aCarLoan = new CarLoan();
aCarLoan.LoanNumber = 3358;
aCarLoan.LastName = "Jansen";
aCarLoan.LoanAmount = 20000.00;
aCarLoan.Make = "Ford";
aCarLoan.Year = 2005;
Console.WriteLine("Loan #{0} for {1} is for {2}",
aCarLoan.LoanNumber, aCarLoan.LastName,
aCarLoan.LoanAmount.ToString("C2"));
Console.WriteLine(" Loan #{0} is for a {1} {2}",
aCarLoan.LoanNumber, aCarLoan.Year,
aCarLoan.Make);
Console.ReadLine();
}
}
}

Output

DemoCarLaon3.cs

Source Code
using System;

namespace DemoLoan
{
class Loan
{
public int LoanNumber { get; set; }
public string LastName { get; set; }
public double LoanAmount
{
set
{
if (value < MINIMUM_LOAN)
loanAmount = MINIMUM_LOAN;
else
loanAmount = value;
}
get
{
return loanAmount;
}
}
public const double MINIMUM_LOAN = 5000;
protected double loanAmount;
public Loan(int num, string name, double amount)
{
LoanNumber = num;
LastName = name;
LoanAmount = amount;
}

}
class CarLoan : Loan
{
private int year;
public string Make { get; set; }

private const int EARLIEST_YEAR = 2006;


private const int LOWEST_INVALID_NUM = 5000;
public CarLoan(int num, string name, double amount,
int year, string make) : base(num, name, amount)
{
Year = year;
Make = make;
}

public int Year


{
set
{
if (value < EARLIEST_YEAR)
{
year = value;
loanAmount = 0;
}
else
year = value;
}
get
{
return year;
}
}
public new int LoanNumber
{
get
{
return base.LoanNumber;
}
set
{
if (value < LOWEST_INVALID_NUM)
base.LoanNumber = value;
else
base.LoanNumber = value % LOWEST_INVALID_NUM;
}
}
}
class DemoCarLoan3
{
static void Main(string[] args)
{
Loan aLoan = new Loan(333, "Hanson", 7000.00);

Console.WriteLine(aLoan.LoanNumber +" "+aLoan.LastName+" "+aLoan.LoanAmount


);
Console.ReadLine();
}
}
}

Output

You might also like