Friday, 28 September 2012

Setup and Deployment Project in windows




-The final stage in the development of an application is deploying the project on client machines, which should be managed carefully. To organize the things in a proper fashion and install required files on the required folders we were provided with Setup and Deployment project under .net. This has to be added under the same solution where we developed our application.

-To add Setup and Deployment Project, open “Add New Project” window and in LHS panel expand the option "Other Project Types" and select Setup and Deployment, now in RHS panel choose "Setup Project" for any type of application or "Web Setup Project" for web applications.


When Setup Project is selected and opened it shows the options as following:
File System on Target Machine
-Application Folder
                                -User's Desktop
                                -User's Programs Menu

-File System on Target Machine in sense the target system where the project is being installed referring to folders on that machine. Application Folder refers to the project installation folder, which has to be specified while installing. User's Desktop refers to the desktop folder of target machine. User's Programs Menu refers to the programs menu folder of target machine.

-We can still add other folders referring to the target machine like Program Files, Fonts, GAC Folders etc. To add a new folder right click on "File System on Target Machine" and select "Add Special Folder" and then choose from the list of folders displayed:

-Now copy the appropriate content into the appropriate folders so that they get’s installed on the target machine in appropriate locations.

-Under Application Folder copy the assemblies (exe’s, dll’s) and config file that has to be installed on the target machine, to do this right click on the Application Folder & select the option Add -> Project Output:

-This opens a window showing the list of projects, select the exe project from it

-This will add the necessary exe's, dll's and config file

-Apart from Project Output we can also choose Folder or File or Assembly and add them under the Application Folder. Add Folder is used for adding a new folder for storing any images. Add File is used for adding any help documents. Add Assembly is used for adding any assemblies that are created outside of the solution



-If we want any shortcuts to be created for our application and place them either on desktop or added to programs menu do the following: Right click on the exe assembly (item of type output) under the application folder and select "Create Shortcut"


-This will create a shortcut specify a name to it. 

-For a shortcut we need to bind an display image of type Icon (.ico), to add an icon image go to the properties of shortcut -> select icon property and select browse from the list, which opens a window -> click on browse -> select application folder -> Images folder -> click on the add file button -> select the image from its physical location -> click on ok button -> again ok button.

-Now to place the short cut on desktop or program's menu folder, go to properties of shortcut again and select the property "Folder" -> click on the button beside it which opens a window, from it select user's desktop or user's programs menu folder which copies the shortcut to the selected folder.
-Installation user interface dialog boxes: setup project provides a number of predefined dialog boxes that you can use to display information or gather input during an installation. The following is a list of available dialog boxes. Not all dialog boxes are available for all deployment project types or for Admin installers. To view the current interfaces in the setup go to view menu -> Editor -> Select User Interface.

-You can still add new user interfaces like Splash, License Agreement, Register User, Read Me, Customer Information etc. To add a new user interface right click on the node Start -> select add dialog which displays the list of interface, choose what u require. e.g.: Splash, License Agreement.

-After adding required user interfaces, we can order them by right clicking on them and select MoveUp and MoveDown options.


-Splash requires a bitmap image to be set that has to be displayed, to set it go to the properties of splash interface -> under SplashBitmap property -> select browse -> choose an .bmp or .jpg image from its physical location same as we selected the .ico image previously for the shortcut.

-License Agreement & ReadMe requires any .rtf file to be set for displaying, which needs to be added same as above using the LicenseFile property and ReadMeFile properties of the interfaces.

-Customer Information will prompt for Name, Organization & Serial Number options; by default Serial Number Options will not be visible to make it visible set the ShowSerialNumber property as true:


-Setting the SerialNumberTemplate property to "<### - %%%%>" creates two text boxes separated by a dash surrounded by spaces. Validation for the first box (###) simply verifies that the user has entered three digits. The second box (%%%%) is validated by an algorithm that adds the digits together and divides the sum by 7. If the remainder is 0, validation succeeds; otherwise, it fails.

After configuring all the things under the Setup project, now in the top of the Visual Studio we find a ComboBox showing the options Debug & Release, default will be Debug change it as Release and then right click on the SetUp Project in the solution explorer and Select Build, which will compile all the Projects and prepare's the SetUp File which u can find them under the SetUp Projects, Release Folder which can be copied on to a CD or a DVD, which is carried to the client system for installing.


Note: Before installing the Setup on the Client Machine make sure that the .Net Framework is installed on it.

Tuesday, 25 September 2012

A general console application programs Day-5

Day5- Console application Programs




listing 1
// A program that uses the Building class.

using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants
}
 
// This class declares an object of type Building.
class BuildingDemo {
  public static void Main() {
    Building house = new Building(); // create a Building object
    int areaPP; // area per person
 
    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;
 
    // compute the area per person
    areaPP = house.area / house.occupants;
 
    Console.WriteLine("house has:\n  " +
                      house.floors + " floors\n  " +
                      house.occupants + " occupants\n  " +
                      house.area + " total area\n  " +
                      areaPP + " area per person");
  }
}

listing 2
// This program creates two Building objects.


using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants
}
 
// This class declares two objects of type Building.
class BuildingDemo {
  public static void Main() {
    Building house = new Building();
    Building office = new Building();

    int areaPP; // area per person
 
    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;

    // assign values to fields in office
    office.occupants = 25;
    office.area = 4200;
    office.floors = 3;
 
    // compute the area per person in house
    areaPP = house.area / house.occupants;
 
    Console.WriteLine("house has:\n  " +
                      house.floors + " floors\n  " +
                      house.occupants + " occupants\n  " +
                      house.area + " total area\n  " +
                      areaPP + " area per person");

    Console.WriteLine();

    // compute the area per person in office
    areaPP = office.area / office.occupants;

    Console.WriteLine("office has:\n  " +
                      office.floors + " floors\n  " +
                      office.occupants + " occupants\n  " +
                      office.area + " total area\n  " +
                      areaPP + " area per person");
  }
}


listing 3
// Add a method to Building.

using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants

  // Display the area per person.
  public void areaPerPerson() {
    Console.WriteLine("  " + area / occupants +
                      " area per person");
  }
}

// Use the areaPerPerson() method.
class BuildingDemo {
  public static void Main() {
    Building house = new Building();
    Building office = new Building();


    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;

    // assign values to fields in office
    office.occupants = 25;
    office.area = 4200;
    office.floors = 3;
 

    Console.WriteLine("house has:\n  " +
                      house.floors + " floors\n  " +
                      house.occupants + " occupants\n  " +
                      house.area + " total area");
    house.areaPerPerson();

    Console.WriteLine();

    Console.WriteLine("office has:\n  " +
                      office.floors + " floors\n  " +
                      office.occupants + " occupants\n  " +
                      office.area + " total area");
    office.areaPerPerson();
  }
}

listing 4
public void myMeth() {
  int i;

  for(i=0; i<10; i++) {
    if(i == 5) return; // stop at 5
    Console.WriteLine();
  }
}

listing 5
// Return a value from areaPerPerson().

using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants

  // Display the area per person.
  public int areaPerPerson() {
    return area / occupants;
  }
}
 
// Use the return value from areaPerPerson().
class BuildingDemo {
  public static void Main() {
    Building house = new Building();
    Building office = new Building();
    int areaPP; // area per person

    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;

    // assign values to fields in office
    office.occupants = 25;
    office.area = 4200;
    office.floors = 3;
 
    // obtain area per person for house
    areaPP = house.areaPerPerson();

    Console.WriteLine("house has:\n  " +
                      house.floors + " floors\n  " +
                      house.occupants + " occupants\n  " +
                      house.area + " total area\n  " +
                      areaPP + " area per person");


    Console.WriteLine();

    // obtain area per person for office
    areaPP = office.areaPerPerson();

    Console.WriteLine("office has:\n  " +
                      office.floors + " floors\n  " +
                      office.occupants + " occupants\n  " +
                      office.area + " total area\n  " +
                      areaPP + " area per person");
  }
}

listing 6
// A simple example that uses a parameter.

using System;

class ChkNum {
  // Return true if x is prime.
  public bool isPrime(int x) {
    for(int i=2; i < x/2 + 1; i++)
      if((x %i) == 0) return false;

    return true;
  }
}

class ParmDemo {
  public static void Main() {
    ChkNum ob = new ChkNum();

    for(int i=1; i < 10; i++)
      if(ob.isPrime(i)) Console.WriteLine(i + " is prime.");
      else Console.WriteLine(i + " is not prime.");

  }
}

listing 7
// Add a method that takes two arguments.

using System;

class ChkNum {
  // Return true if x is prime.
  public bool isPrime(int x) {
    for(int i=2; i < x/2 + 1; i++)
      if((x %i) == 0) return false;

    return true;
  }

  // Return the least common denominator.
  public int lcd(int a, int b) {
    int max;

    if(isPrime(a) | isPrime(b)) return 1;

    max = a < b ? a : b;

    for(int i=2; i < max/2 + 1; i++)
      if(((a%i) == 0) & ((b%i) == 0)) return i;

    return 1;
  }
}

class ParmDemo {
  public static void Main() {
    ChkNum ob = new ChkNum();
    int a, b;

    for(int i=1; i < 10; i++)
      if(ob.isPrime(i)) Console.WriteLine(i + " is prime.");
      else Console.WriteLine(i + " is not prime.");

    a = 7;
    b = 8;
    Console.WriteLine("Least common denominator for " +
                      a + " and " + b + " is " +
                      ob.lcd(a, b));

    a = 100;
    b = 8;
    Console.WriteLine("Least common denominator for " +
                      a + " and " + b + " is " +
                      ob.lcd(a, b));

    a = 100;
    b = 75;
    Console.WriteLine("Least common denominator for " +
                      a + " and " + b + " is " +
                      ob.lcd(a, b));

  }
}

listing 8
/*
   Add a parameterized method that computes the
   maximum number of people that can occupy a
   buiding assuming each needs a specified
   minimum space.
*/

using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants

  // Display the area per person.
  public int areaPerPerson() {
    return area / occupants;
  }

  /* Return the maximum number of occupants if each
     is to have at least the specified minimum area. */
  public int maxOccupant(int minArea) {
    return area / minArea;
  }
}

// Use maxOccupant().
class BuildingDemo {
  public static void Main() {
    Building house = new Building();
    Building office = new Building();

    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;

    // assign values to fields in office
    office.occupants = 25;
    office.area = 4200;
    office.floors = 3;
 
    Console.WriteLine("Maximum occupants for house if each has " +
                      300 + " square feet: " +
                      house.maxOccupant(300));

    Console.WriteLine("Maximum occupants for office if each has " +
                      300 + " square feet: " +
                      office.maxOccupant(300));
  }
}

listing 9
// A simple constructor.

using System;

class MyClass {
  public int x;

  public MyClass() {
    x = 10;
  }
}
 
class ConsDemo {
  public static void Main() {
    MyClass t1 = new MyClass();
    MyClass t2 = new MyClass();

    Console.WriteLine(t1.x + " " + t2.x);
  }
}

listing 10
// A parameterized constructor.

using System;

class MyClass {
  public int x;

  public MyClass(int i) {
    x = i;
  }
}
 
class ParmConsDemo {
  public static void Main() {
    MyClass t1 = new MyClass(10);
    MyClass t2 = new MyClass(88);

    Console.WriteLine(t1.x + " " + t2.x);
  }
}

listing 11
// Add a constructor to Building.

using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants


  public Building(int f, int a, int o) {
    floors = f;
    area = a;
    occupants = o;
  }

  // Display the area per person.
  public int areaPerPerson() {
    return area / occupants;
  }

  /* Return the maximum number of occupants if each
     is to have at least the specified minum area. */
  public int maxOccupant(int minArea) {
    return area / minArea;
  }
}
 
// Use the parameterized Building constructor.
class BuildingDemo {
  public static void Main() {
    Building house = new Building(2, 2500, 4);
    Building office = new Building(3, 4200, 25);

    Console.WriteLine("Maximum occupants for house if each has " +
                      300 + " square feet: " +
                      house.maxOccupant(300));

    Console.WriteLine("Maximum occupants for office if each has " +
                      300 + " square feet: " +
                      office.maxOccupant(300));
  }
}

listing 12
// Use new with a value type.

using System;

class newValue {
  public static void Main() {
    int i = new int(); // initialize i to zero

    Console.WriteLine("The value of i is: " + i);
  }
}

listing 13
// Demonstrate a destructor.

using System;

class Destruct {
  public int x;

  public Destruct(int i) {
    x = i;
  }  

  // called when object is recycled
  ~Destruct() {
    Console.WriteLine("Destructing " + x);
  }
 
  // generates an object that is immediately destroyed
  public void generator(int i) {
    Destruct o = new Destruct(i);
  }

}  
 
class DestructDemo {  
  public static void Main() {  
    int count;

    Destruct ob = new Destruct(0);

    /* Now, generate a large number of objects.  At
       some point, garbage collection will occur.
       Note: you might need to increase the number
       of objects generated in order to force
       garbage collection. */

    for(count=1; count < 100000; count++)
      ob.generator(count);

    Console.WriteLine("Done");
  }  
}

listing 14
using System;

class Rect {
  public int width;
  public int height;

  public Rect(int w, int h) {
    width = w;
    height = h;
  }

  public int area() {
    return width * height;
  }
}

class UseRect {
  public static void Main() {
    Rect r1 = new Rect(4, 5);
    Rect r2 = new Rect(7, 9);

    Console.WriteLine("Area of r1: " + r1.area());

    Console.WriteLine("Area of r2: " + r2.area());

  }
}

listing 15
using System;

class Rect {
  public int width;
  public int height;

  public Rect(int w, int h) {
    this.width = w;
    this.height = h;
  }

  public int area() {
    return this.width * this.height;
  }
}

class UseRect {
  public static void Main() {
    Rect r1 = new Rect(4, 5);
    Rect r2 = new Rect(7, 9);

    Console.WriteLine("Area of r1: " + r1.area());

    Console.WriteLine("Area of r2: " + r2.area());

  }
}

listing 16
public Rect(int width, int height) {
  this.width = width;
  this.height = height;
}

A general console application programs Day-4

Day4-Console application Programs

listing 1
// Determine if a value is positive or negative.
using System;

class PosNeg {
  public static void Main() {
    int i;

    for(i=-5; i <= 5; i++) {
      Console.Write("Testing " + i + ": ");

      if(i < 0) Console.WriteLine("negative");
      else Console.WriteLine("positive");
    }

  }
}

listing 2
// Determine if a value is positive, negative, or zero.
using System;

class PosNegZero {
  public static void Main() {
    int i;

    for(i=-5; i <= 5; i++) {

      Console.Write("Testing " + i + ": ");

      if(i < 0) Console.WriteLine("negative");
      else if(i == 0) Console.WriteLine("no sign");
        else Console.WriteLine("positive");
    }

  }
}

listing 3
// Determine smallest single-digit factor.
using System;

class Ladder {  
  public static void Main() {  
    int num;

    for(num = 2; num < 12; num++) {
      if((num % 2) == 0)
        Console.WriteLine("Smallest factor of " + num + " is 2.");
      else if((num % 3) == 0)
        Console.WriteLine("Smallest factor of " + num + " is 3.");
      else if((num % 5) == 0)
        Console.WriteLine("Smallest factor of " + num + " is 5.");
      else if((num % 7) == 0)
        Console.WriteLine("Smallest factor of " + num + " is 7.");
      else
        Console.WriteLine(num + " is not divisible by 2, 3, 5, or 7.");
    }
  }
}

listing 4
// Demonstrate the switch.

using System;

class SwitchDemo {
  public static void Main() {
    int i;

    for(i=0; i<10; i++)
      switch(i) {
        case 0:
          Console.WriteLine("i is zero");
          break;
        case 1:
          Console.WriteLine("i is one");
          break;
        case 2:
          Console.WriteLine("i is two");
          break;
        case 3:
          Console.WriteLine("i is three");
          break;
        case 4:
          Console.WriteLine("i is four");
          break;
        default:
          Console.WriteLine("i is five or more");
          break;
      }
   
  }
}

listing 5
// Use a char to control the switch.

using System;

class SwitchDemo2 {
  public static void Main() {
    char ch;

    for(ch='A'; ch<= 'E'; ch++)
      switch(ch) {
        case 'A':
          Console.WriteLine("ch is A");
          break;
        case 'B':
          Console.WriteLine("ch is B");
          break;
        case 'C':
          Console.WriteLine("ch is C");
          break;
        case 'D':
          Console.WriteLine("ch is D");
          break;
        case 'E':
          Console.WriteLine("ch is E");
          break;
      }  
  }
}

listing 6
// Empty cases can fall through.

using System;

class EmptyCasesCanFall {
  public static void Main() {
    int i;

    for(i=1; i < 5; i++)
      switch(i) {
        case 1:
        case 2:
        case 3: Console.WriteLine("i is 1, 2 or 3");
          break;
        case 4: Console.WriteLine("i is 4");
          break;
      }

  }
}

listing 7
// A negatively running for loop.

using System;

class DecrFor {  
  public static void Main() {  
    int x;

    for(x = 100; x > -100; x -= 5)
      Console.WriteLine(x);
  }
}

listing 8
/*
   Determine if a number is prime.  If it is not,
   then display its largest factor.
*/

using System;

class FindPrimes {  
  public static void Main() {  
    int num;
    int i;
    int factor;
    bool isprime;


    for(num = 2; num < 20; num++) {
      isprime = true;
      factor = 0;

      // see if num is evenly divisible
      for(i=2; i <= num/2; i++) {
        if((num % i) == 0) {
          // num is evenly divisible -- not prime
          isprime = false;
          factor = i;
        }
      }

      if(isprime)
        Console.WriteLine(num + " is prime.");
      else
        Console.WriteLine("Largest factor of " + num +
                          " is " + factor);
    }
  }  
}

listing 9
// Use commas in a for statememt.  

using System;

class Comma {  
  public static void Main() {  
    int i, j;

    for(i=0, j=10; i < j; i++, j--)
      Console.WriteLine("i and j: " + i + " " + j);
  }
}

listing 10
/*
   Use commas in a for statememt to find
   the largest and smallest factor of a number.
*/

using System;

class Comma {  
  public static void Main() {  
    int i, j;
    int smallest, largest;
    int num;

    num = 100;
 
    smallest = largest = 1;

    for(i=2, j=num/2; (i <= num/2) & (j >= 2); i++, j--) {

      if((smallest == 1) & ((num % i) == 0))
        smallest = i;

      if((largest == 1) & ((num % j) == 0))
        largest = j;

    }

    Console.WriteLine("Largest factor: " + largest);
    Console.WriteLine("Smallest factor: " + smallest);
  }
}

listing 11
// Loop condition can be any bool expression.
using System;

class forDemo {  
  public static void Main() {  
    int i, j;
    bool done = false;

    for(i=0, j=100; !done; i++, j--) {

      if(i*i >= j) done = true;

      Console.WriteLine("i, j: " + i + " " + j);
    }

  }  
}

listing 12
// Parts of the for can be empty.

using System;

class Empty {
  public static void Main() {
    int i;

    for(i = 0; i < 10; ) {
      Console.WriteLine("Pass #" + i);
      i++; // increment loop control var
    }
  }
}

listing 13
// Move more out of the for loop.

using System;

class Empty2 {
  public static void Main() {
    int i;

    i = 0; // move initialization out of loop
    for(; i < 10; ) {
      Console.WriteLine("Pass #" + i);
      i++; // increment loop control var
    }
  }
}

listing 14
// The body of a loop can be empty.

using System;

class Empty3 {
  public static void Main() {
    int i;
    int sum = 0;

    // sum the numbers through 5
    for(i = 1; i <= 5; sum += i++) ;

    Console.WriteLine("Sum is " + sum);
  }
}

listing 15
// Declare loop control variable inside the for.

using System;

class ForVar {
  public static void Main() {
    int sum = 0;
    int fact = 1;

    // compute the factorial of the numbers through 5
    for(int i = 1; i <= 5; i++) {
      sum += i;  // i is known throughout the loop
      fact *= i;
    }

    // but, i is not known here.

    Console.WriteLine("Sum is " + sum);
    Console.WriteLine("Factorial is " + fact);
  }
}

listing 16
// Compute the order of magnitude of an integer

using System;

class WhileDemo {
  public static void Main() {
    int num;
    int mag;

    num = 435679;
    mag = 0;

    Console.WriteLine("Number: " + num);

    while(num > 0) {
      mag++;
      num = num / 10;
    };

    Console.WriteLine("Magnitude: " + mag);
  }
}

listing 17
// Compute integer powers of 2.

using System;

class Power {
  public static void Main() {
    int e;
    int result;

    for(int i=0; i < 10; i++) {
      result = 1;
      e = i;

      while(e > 0) {
        result *= 2;
        e--;
      }

      Console.WriteLine("2 to the " + i +
                         " power is " + result);      
    }
  }
}

listing 18
// Display the digits of an integer in reverse order.

using System;

class DoWhileDemo {
  public static void Main() {
    int num;
    int nextdigit;

    num = 198;

    Console.WriteLine("Number: " + num);

    Console.Write("Number in reverse order: ");

    do {
      nextdigit = num % 10;
      Console.Write(nextdigit);
      num = num / 10;
    } while(num > 0);

    Console.WriteLine();
  }
}

listing 19
// Using break to exit a loop.

using System;

class BreakDemo {
  public static void Main() {

    // use break to exit this loop
    for(int i=-10; i <= 10; i++) {
      if(i > 0) break; // terminate loop when i is positive
      Console.Write(i + " ");
    }
    Console.WriteLine("Done");
  }
}

listing 20
// Using break to exit a do-while loop.

using System;

class BreakDemo2 {
  public static void Main() {
    int i;

    i = -10;  
    do {
      if(i > 0) break;
      Console.Write(i + " ");
      i++;
    } while(i <= 10);

    Console.WriteLine("Done");
  }
}

listing 21
// Find the smallest factor of a value.

using System;

class FindSmallestFactor {
  public static void Main() {
    int factor = 1;
    int num = 1000;
   
    for(int i=2; i < num/2; i++) {
      if((num%i) == 0) {
        factor = i;
        break; // stop loop when factor is found
      }
    }
    Console.WriteLine("Smallest factor is " + factor);
  }
}

listing 22
// Using break with nested loops.

using System;

class BreakNested {
  public static void Main() {

    for(int i=0; i<3; i++) {
      Console.WriteLine("Outer loop count: " + i);
      Console.Write("    Inner loop count: ");

      int t = 0;          
      while(t < 100) {
        if(t == 10) break; // terminate loop if t is 10
        Console.Write(t + " ");
        t++;
      }
      Console.WriteLine();
    }
    Console.WriteLine("Loops complete.");
  }
}

listing 23
// Use continue.

using System;

class ContDemo {
  public static void Main() {
    // print even numbers between 0 and 100
    for(int i = 0; i <= 100; i++) {
      if((i%2) != 0) continue; // iterate
      Console.WriteLine(i);
    }
  }
}

listing 24
// Use goto with a switch.

using System;

class SwitchGoto {
  public static void Main() {

    for(int i=1; i < 5; i++) {
      switch(i) {
        case 1:
          Console.WriteLine("In case 1");
          goto case 3;
        case 2:
          Console.WriteLine("In case 2");
          goto case 1;
        case 3:
          Console.WriteLine("In case 3");
          goto default;
        default:
          Console.WriteLine("In default");
          break;
      }

      Console.WriteLine();
    }

//    goto case 1; // Error! Can't jump into a switch.
  }
}

listing 25
// Demonstrate the goto.

using System;

class Use_goto {  
  public static void Main() {  
    int i=0, j=0, k=0;

    for(i=0; i < 10; i++) {
      for(j=0; j < 10; j++ ) {
        for(k=0; k < 10; k++) {
          Console.WriteLine("i, j, k: " + i + " " + j + " " + k);
          if(k == 3) goto stop;
        }
      }
    }

stop:
    Console.WriteLine("Stopped! i, j, k: " + i + ", " + j + " " + k);
 
  }
}

A general console application programs Day-3

Day3-Console applications Programs

listing 1
// Demonstrate the % operator.

using System;

class ModDemo {  
  public static void Main() {  
    int iresult, irem;
    double dresult, drem;

    iresult = 10 / 3;
    irem = 10 % 3;

    dresult = 10.0 / 3.0;
    drem = 10.0 % 3.0;

    Console.WriteLine("Result and remainder of 10 / 3: " +
                       iresult + " " + irem);
    Console.WriteLine("Result and remainder of 10.0 / 3.0: " +
                       dresult + " " + drem);
  }  
}

listing 2
/*
   Demonstrate the difference between prefix
   postfix forms of ++.
*/
using System;

class PrePostDemo {
  public static void Main() {  
    int x, y;
    int i;

    x = 1;
    Console.WriteLine("Series generated using y = x + x++;");
    for(i = 0; i < 10; i++) {

      y = x + x++; // postfix ++

      Console.WriteLine(y + " ");
    }
    Console.WriteLine();

    x = 1;
    Console.WriteLine("Series generated using y = x + ++x;");
    for(i = 0; i < 10; i++) {

      y = x + ++x; // prefix ++

      Console.WriteLine(y + " ");
    }
    Console.WriteLine();
 
  }
}

listing 3
// Demonstrate the relational and logical operators.

using System;

class RelLogOps {  
  public static void Main() {  
    int i, j;
    bool b1, b2;

    i = 10;
    j = 11;
    if(i < j) Console.WriteLine("i < j");
    if(i <= j) Console.WriteLine("i <= j");
    if(i != j) Console.WriteLine("i != j");
    if(i == j) Console.WriteLine("this won't execute");
    if(i >= j) Console.WriteLine("this won't execute");
    if(i > j) Console.WriteLine("this won't execute");

    b1 = true;
    b2 = false;
    if(b1 & b2) Console.WriteLine("this won't execute");
    if(!(b1 & b2)) Console.WriteLine("!(b1 & b2) is true");
    if(b1 | b2) Console.WriteLine("b1 | b2 is true");
    if(b1 ^ b2) Console.WriteLine("b1 ^ b2 is true");
  }  
}

listing 4
// Create an implication operator in C#.
using System;

class Implication {
  public static void Main() {  
    bool p=false, q=false;
    int i, j;

    for(i = 0; i < 2; i++) {
      for(j = 0; j < 2; j++) {
        if(i==0) p = true;
        if(i==1) p = false;
        if(j==0) q = true;
        if(j==1) q = false;
       
        Console.WriteLine("p is " + p + ", q is " + q);
        if(!p | q) Console.WriteLine(p + " implies " + q +
                    " is " + true);
        Console.WriteLine();
      }
    }
  }
}

listing 5
// Demonstrate the short-circuit operators.

using System;

class SCops {  
  public static void Main() {  
    int n, d;

    n = 10;
    d = 2;
    if(d != 0 && (n % d) == 0)
      Console.WriteLine(d + " is a factor of " + n);

    d = 0; // now, set d to zero

    // Since d is zero, the second operand is not evaluated.
    if(d != 0 && (n % d) == 0)
      Console.WriteLine(d + " is a factor of " + n);
   
    /* Now, try the same thing without short-circuit operator.
       This will cause a divide-by-zero error.  */
    if(d != 0 & (n % d) == 0)
      Console.WriteLine(d + " is a factor of " + n);
  }  
}

listing 6
// Side-effects can be important.

using System;

class SideEffects {  
  public static void Main() {  
    int i;

    i = 0;

    /* Here, i is still incremented even though
       the if statement fails. */
    if(false & (++i < 100))
       Console.WriteLine("this won't be displayed");
    Console.WriteLine("if statement executed: " + i); // displays 1

    /* In this case, i is not incremented because
       the short-circuit operator skips the increment. */
    if(false && (++i < 100))
      Console.WriteLine("this won't be displayed");
    Console.WriteLine("if statement executed: " + i); // still 1 !!
  }  
}

listing 7
//  Use bitwise AND to make a number even.
using System;

class MakeEven {
  public static void Main() {
    ushort num;
    ushort i;  

    for(i = 1; i <= 10; i++) {
      num = i;

      Console.WriteLine("num: " + num);

      num = (ushort) (num & 0xFFFE); // num & 1111 1110

      Console.WriteLine("num after turning off bit zero: "
                        +  num + "\n");
    }
  }
}

listing 8
//  Use bitwise AND to determine if a number is odd.
using System;

class IsOdd {
  public static void Main() {
    ushort num;

    num = 10;

    if((num & 1) == 1)
      Console.WriteLine("This won't display.");

    num = 11;

    if((num & 1) == 1)
      Console.WriteLine(num + " is odd.");

  }
}

listing 9
// Display the bits within a byte.
using System;

class ShowBits {
  public static void Main() {
    int t;
    byte val;

    val = 123;
    for(t=128; t > 0; t = t/2) {
      if((val & t) != 0) Console.Write("1 ");
      if((val & t) == 0) Console.Write("0 ");
    }
  }
}

listing 10
//  Use bitwise OR to make a number odd.
using System;

class MakeOdd {
  public static void Main() {
    ushort num;
    ushort i;  

    for(i = 1; i <= 10; i++) {
      num = i;

      Console.WriteLine("num: " + num);

      num = (ushort) (num | 1); // num | 0000 0001

      Console.WriteLine("num after turning on bit zero: "
                        +  num + "\n");
    }
  }
}

listing 11
// Use XOR to encode and decode a message.

using System;

class Encode {
  public static void Main() {
    char ch1 = 'H';
    char ch2 = 'i';
    char ch3 = '!';

    int key = 88;

    Console.WriteLine("Original message: " +
                      ch1 + ch2 + ch3);

    // encode the message
    ch1 = (char) (ch1 ^ key);
    ch2 = (char) (ch2 ^ key);
    ch3 = (char) (ch3 ^ key);

    Console.WriteLine("Encoded message: " +
                      ch1 + ch2 + ch3);

    // decode the message
    ch1 = (char) (ch1 ^ key);
    ch2 = (char) (ch2 ^ key);
    ch3 = (char) (ch3 ^ key);
 
    Console.WriteLine("Decoded message: " +
                      ch1 + ch2 + ch3);
  }
}

listing 12
// Demonstrate the bitwise NOT.
using System;

class NotDemo {
  public static void Main() {
    sbyte b = -34;
    int t;

    for(t=128; t > 0; t = t/2) {
      if((b & t) != 0) Console.Write("1 ");
      if((b & t) == 0) Console.Write("0 ");
    }
    Console.WriteLine();

    // reverse all bits
    b = (sbyte) ~b;

    for(t=128; t > 0; t = t/2) {
      if((b & t) != 0) Console.Write("1 ");
      if((b & t) == 0) Console.Write("0 ");
    }
  }
}

listing 13
// Demonstrate the shift << and >> operators.
using System;

class ShiftDemo {
  public static void Main() {
    int val = 1;
    int t;
    int i;

    for(i = 0; i < 8; i++) {
      for(t=128; t > 0; t = t/2) {
        if((val & t) != 0) Console.Write("1 ");
        if((val & t) == 0) Console.Write("0 ");
      }
      Console.WriteLine();
      val = val << 1; // left shift
    }
    Console.WriteLine();

    val = 128;
    for(i = 0; i < 8; i++) {
      for(t=128; t > 0; t = t/2) {
        if((val & t) != 0) Console.Write("1 ");
        if((val & t) == 0) Console.Write("0 ");
      }
      Console.WriteLine();
      val = val >> 1; // right shift
    }
  }
}

listing 14
// Use the shift operators to multiply and divide by 2.
using System;

class MultDiv {
  public static void Main() {
    int n;

    n = 10;

    Console.WriteLine("Value of n: " + n);

    // multiply by 2
    n = n << 1;
    Console.WriteLine("Value of n after n = n * 2: " + n);

    // multiply by 4
    n = n << 2;
    Console.WriteLine("Value of n after n = n * 4: " + n);

    // divide by 2
    n = n >> 1;
    Console.WriteLine("Value of n after n = n / 2: " + n);

    // divide by 4
    n = n >> 2;
    Console.WriteLine("Value of n after n = n / 4: " + n);
    Console.WriteLine();

    // reset n
    n = 10;
    Console.WriteLine("Value of n: " + n);

    // multiply by 2, 30 times
    n = n << 30; // data is lost
    Console.WriteLine("Value of n after left-shifting 30 places: " + n);

  }
}

listing 15
// Prevent a division by zero using the ?.
using System;

class NoZeroDiv {
  public static void Main() {
    int result;
    int i;

    for(i = -5; i < 6; i++) {
      result = i != 0 ? 100 / i : 0;
      if(i != 0)
        Console.WriteLine("100 / " + i + " is " + result);
    }
  }
}

listing 16
// Prevent a division by zero using the ?.
using System;

class NoZeroDiv2 {
  public static void Main() {
    int i;

    for(i = -5; i < 6; i++)
      if(i != 0 ? true : false)
        Console.WriteLine("100 / " + i +
                           " is " + 100 / i);
  }
}

A general console application programs Day-2

Day 2 - Console application programs

listing 1
// Compute the distance from the Earth to the sun, in inches. 
using System; 
class Inches {    
  public static void Main() {    
    long inches; 
    long miles; 
   
    miles = 93000000; // 93,000,000 miles to the sun 
    // 5,280 feet in a mile, 12 inches in a foot 
    inches = miles * 5280 * 12; 
   
    Console.WriteLine("Distance to the sun: " + 
                      inches + " inches."); 
   
  }    
}

listing 2
// Use byte. 
using System; 
class Use_byte {    
  public static void Main() {    
    byte x; 
    int sum; 
    sum = 0; 
    for(x = 1; x <= 100; x++) 
      sum = sum + x; 
    Console.WriteLine("Summation of 100 is " + sum); 
  }    
}

listing 3
// Find the radius of a circle given its area. 
using System; 
class FindRadius {    
  public static void Main() {    
    Double r; 
    Double area; 
    area = 10.0; 
    r = Math.Sqrt(area / 3.1416); 
    Console.WriteLine("Radius is " + r); 
  }    


listing 4
//  Demonstrate Math.Sin(), Math.Cos(), and Math.Tan(). 
using System; 
class Trigonometry {    
  public static void Main() {    
    Double theta; // angle in radians 
     
    for(theta = 0.1; theta <= 1.0; theta = theta + 0.1) { 
      Console.WriteLine("Sine of " + theta + "  is " + 
                        Math.Sin(theta)); 
      Console.WriteLine("Cosine of " + theta + "  is " + 
                        Math.Cos(theta)); 
      Console.WriteLine("Tangent of " + theta + "  is " + 
                        Math.Tan(theta)); 
      Console.WriteLine(); 
    } 
  }    
}

listing 5
// Use the decimal type to compute a discount. 
  
using System;  
  
class UseDecimal {     
  public static void Main() {     
    decimal price;  
    decimal discount; 
    decimal discounted_price;  
  
    // compute discounted price 
    price = 19.95m;  
    discount = 0.15m; // discount rate is 15% 
    discounted_price = price - ( price * discount);  
  
    Console.WriteLine("Discounted price: $" + discounted_price);  
  }     
}

listing 6
/*   
   Use the decimal type to compute the future value 
   of an investment. 
*/   
   
using System;   
   
class FutVal {      
  public static void Main() {      
    decimal amount;   
    decimal rate_of_return;  
    int years, i;   
   
    amount = 1000.0M; 
    rate_of_return = 0.07M; 
    years = 10; 
    Console.WriteLine("Original investment: $" + amount); 
    Console.WriteLine("Rate of return: " + rate_of_return); 
    Console.WriteLine("Over " + years + " years"); 
    for(i = 0; i < years; i++) 
      amount = amount + (amount * rate_of_return); 
    Console.WriteLine("Future value is $" + amount);  
  }      
}

listing 7
// Demonstrate bool values. 
using System; 
class BoolDemo { 
  public static void Main() { 
    bool b; 
    b = false; 
    Console.WriteLine("b is " + b); 
    b = true; 
    Console.WriteLine("b is " + b); 
    // a bool value can control the if statement 
    if(b) Console.WriteLine("This is executed."); 
    b = false; 
    if(b) Console.WriteLine("This is not executed."); 
    // outcome of a relational operator is a bool value 
    Console.WriteLine("10 > 9 is " + (10 > 9)); 
  } 
}

listing 8
// Use format commands.  
using System; 
class DisplayOptions {    
  public static void Main() {    
    int i; 
    Console.WriteLine("Value\tSquared\tCubed"); 
    for(i = 1; i < 10; i++) 
      Console.WriteLine("{0}\t{1}\t{2}",  
                        i, i*i, i*i*i); 
  }    
}

listing 9
/*  
   Use the C format spedifier to output dollars and cents.  
*/  
  
using System;  
  
class UseDecimal {     
  public static void Main() {     
    decimal price;  
    decimal discount; 
    decimal discounted_price;  
  
    // compute discounted price 
    price = 19.95m;  
    discount = 0.15m; // discount rate is 15% 
    discounted_price = price - ( price * discount);  
  
    Console.WriteLine("Discounted price: {0:C}", discounted_price);  
  }     
}

listing 10
// Demonstrate escape sequences in strings. 
using System; 
class StrDemo {    
  public static void Main() {    
    Console.WriteLine("Line One\nLine Two\nLine Three"); 
    Console.WriteLine("One\tTwo\tThree"); 
    Console.WriteLine("Four\tFive\tSix"); 
    // embed quotes 
    Console.WriteLine("\"Why?\", he asked."); 
  }    
}

listing 11
// Demonstrate verbatim literal strings. 
using System; 
class Verbatim {    
  public static void Main() {    
    Console.WriteLine(@"This is a verbatim 
string literal 
that spans several lines. 
"); 
    Console.WriteLine(@"Here is some tabbed output: 
1 2 3
5 6 7
"); 
    Console.WriteLine(@"Programmers say, ""I like C#."""); 
  }    
}

listing 12
// Demonstrate dynamic initialization.  
  
using System;  
  
class DynInit {  
  public static void Main() {  
    double s1 = 4.0, s2 = 5.0; // length of sides 
  
    // dynamically initialize hypot  
    double hypot = Math.Sqrt( (s1 * s1) + (s2 * s2) ); 
  
    Console.Write("Hypotenuse of triangle with sides " + 
                  s1 + " by " + s2 + " is "); 
    Console.WriteLine("{0:#.###}.", hypot); 
  }  
}

listing 13
// Demonstrate block scope. 
using System; 
class ScopeDemo { 
  public static void Main() { 
    int x; // known to all code within Main() 
    x = 10; 
    if(x == 10) { // start new scope
      int y = 20; // known only to this block 
      // x and y both known here. 
      Console.WriteLine("x and y: " + x + " " + y); 
      x = y * 2; 
    } 
    // y = 100; // Error! y not known here  
    // x is still known here. 
    Console.WriteLine("x is " + x); 
  } 
}

listing 14
// Demonstrate lifetime of a variable. 
using System; 
class VarInitDemo { 
  public static void Main() { 
    int x;  
    for(x = 0; x < 3; x++) { 
      int y = -1; // y is initialized each time block is entered 
      Console.WriteLine("y is: " + y); // this always prints -1 
      y = 100;  
      Console.WriteLine("y is now: " + y); 
    } 
  } 
}

listing 15
/*  
   This program attempts to declared a variable 
   in an inner scope with the same name as one 
   defined in an outer scope. 
   *** This program will not compile. *** 
*/  
using System; 
class NestVar {  
  public static void Main() {  
    int count;  
    for(count = 0; count < 10; count = count+1) { 
      Console.WriteLine("This is count: " + count);  
     
      int count; // illegal!!! 
      for(count = 0; count < 2; count++) 
        Console.WriteLine("This program is in error!"); 
    } 
  }  
}

listing 16
// Demonstate automatic conversion from long to double. 
using System; 
class LtoD {    
  public static void Main() {    
    long L; 
    double D; 
   
    L = 100123285L; 
    D = L; 
   
    Console.WriteLine("L and D: " + L + " " + D); 
  }    
}

listing 17
// *** This program will not compile. *** 
using System; 
class LtoD {    
  public static void Main() {    
    long L; 
    double D; 
   
    D = 100123285.0; 
    L = D; // Illegal!!! 
   
    Console.WriteLine("L and D: " + L + " " + D); 
   
  }    
}

listing 18
// Demonstrate casting. 
using System; 
class CastDemo {    
  public static void Main() {    
    double x, y; 
    byte b; 
    int i; 
    char ch; 
    uint u; 
    short s; 
    long l; 
    x = 10.0; 
    y = 3.0; 
    // cast an int into a double 
    i = (int) (x / y); // cast double to int, fractional component lost 
    Console.WriteLine("Integer outcome of x / y: " + i); 
    Console.WriteLine(); 
     
    // cast an int into a byte, no data lost 
    i = 255; 
    b = (byte) i;  
    Console.WriteLine("b after assigning 255: " + b + 
                      " -- no data lost."); 
    // cast an int into a byte, data lost 
    i = 257; 
    b = (byte) i;  
    Console.WriteLine("b after assigning 257: " + b + 
                      " -- data lost."); 
    Console.WriteLine(); 
    // cast a uint into a short, no data lost 
    u = 32000; 
    s = (short) u; 
    Console.WriteLine("s after assigning 32000: " + s + 
                      " -- no data lost.");  
    // cast a uint into a short, data lost 
    u = 64000; 
    s = (short) u; 
    Console.WriteLine("s after assigning 64000: " + s + 
                      " -- data lost.");  
    Console.WriteLine(); 
    // cast a long into a uint, no data lost 
    l = 64000; 
    u = (uint) l; 
    Console.WriteLine("u after assigning 64000: " + u + 
                      " -- no data lost.");  
    // cast a long into a uint, data lost 
    l = -12; 
    u = (uint) l; 
    Console.WriteLine("u after assigning -12: " + u + 
                      " -- data lost.");  
    Console.WriteLine(); 
    // cast an int into a char 
    b = 88; // ASCII code for X 
    ch = (char) b; 
    Console.WriteLine("ch after assigning 88: " + ch);  
  }    
}

listing 19
// A promotion surprise!  
  
using System;  
  
class PromDemo {     
  public static void Main() {     
    byte b;  
    
    b = 10;  
    b = (byte) (b * b); // cast needed!!  
  
    Console.WriteLine("b: "+ b);  
  }     
}

listing 20
// Using casts in an expression.  
  
using System;  
  
class CastExpr {     
  public static void Main() {     
    double n;  
  
     for(n = 1.0; n <= 10; n++) {  
       Console.WriteLine("The square root of {0} is {1}",  
                         n, Math.Sqrt(n));  
  
       Console.WriteLine("Whole number part: {0}" ,   
                         (int) Math.Sqrt(n));  
   
       Console.WriteLine("Fractional part: {0}",   
                         Math.Sqrt(n) - (int) Math.Sqrt(n) );  
       Console.WriteLine(); 
    }  
  
  }     
}

Sunday, 23 September 2012

A general console application programs



Day 1- Try to practice

listing 1
/*
   This is a simple C# program.

   Call this program Example.cs.
*/

using System;

class Example {

  // A C# program begins with a call to Main().
  public static void Main() {
    Console.WriteLine("A simple C# program.");
  }
}

listing 2
// This version does not include the using System statement.

class Example {

  // A C# program begins with a call to Main().
  public static void Main() {

    // Here, Console.WriteLine is fully qualified.
    System.Console.WriteLine("A simple C# program.");
  }
}

listing 3
// This program demonstrates variables.

using System;

class Example2 {
  public static void Main() {
    int x; // this declares a variable
    int y; // this declares another variable

    x = 100; // this assigns 100 to x

    Console.WriteLine("x contains " + x);

    y = x / 2;

    Console.Write("y contains x / 2: ");
    Console.WriteLine(y);
  }
}

listing 4
/*
   This program illustrates the differences
   between int and double.
*/

using System;

class Example3 {
  public static void Main() {
    int ivar;     // this declares an int variable
    double dvar;  // this declares a floating-point variable

    ivar = 100;   // assign ivar the value 100
 
    dvar = 100.0; // assign dvar the value 100.0

    Console.WriteLine("Original value of ivar: " + ivar);
    Console.WriteLine("Original value of dvar: " + dvar);

    Console.WriteLine(); // print a blank line

    // now, divide both by 3
    ivar = ivar / 3;
    dvar = dvar / 3.0;

    Console.WriteLine("ivar after division: " + ivar);
    Console.WriteLine("dvar after division: " + dvar);
  }
}

listing 5
// Compute the area of a circle.

using System;
 
class Circle {
  static void Main() {
    double radius;
    double area;

    radius = 10.0;
    area = radius * radius * 3.1416;

    Console.WriteLine("Area is " + area);
  }
}

listing 6
// Demonstrate the if.

using System;

class IfDemo {
  public static void Main() {
    int a, b, c;

    a = 2;
    b = 3;

    if(a < b) Console.WriteLine("a is less than b");

    // this won't display anything
    if(a == b) Console.WriteLine("you won't see this");

    Console.WriteLine();

    c = a - b; // c contains -1

    Console.WriteLine("c contains -1");
    if(c >= 0) Console.WriteLine("c is non-negative");
    if(c < 0) Console.WriteLine("c is negative");

    Console.WriteLine();

    c = b - a; // c now contains 1
    Console.WriteLine("c contains 1");
    if(c >= 0) Console.WriteLine("c is non-negative");
    if(c < 0) Console.WriteLine("c is negative");

  }
}

listing 7
// Demonstrate the for loop.

using System;

class ForDemo {
  public static void Main() {
    int count;

    for(count = 0; count < 5; count = count+1)
      Console.WriteLine("This is count: " + count);

    Console.WriteLine("Done!");
  }
}

listing 8
// Demonstrate a block of code.

using System;

class BlockDemo {
  public static void Main() {
    int i, j, d;

    i = 5;
    j = 10;

    // the target of this if is a block
    if(i != 0) {
      Console.WriteLine("i does not equal zero");
      d = j / i;
      Console.WriteLine("j / i is " + d);
    }
  }
}

listing 9
// Compute the sum and product of the numbers from 1 to 10.

using System;
 
class ProdSum {
  static void Main() {
    int prod;
    int sum;
    int i;

    sum = 0;
    prod = 1;

    for(i=1; i <= 10; i++) {
      sum = sum + i;
      prod = prod * i;    
    }
    Console.WriteLine("Sum is " + sum);
    Console.WriteLine("Product is " + prod);

  }
}

listing 10
// Demonstrate an @ identier.

using System;
 
class IdTest {
  static void Main() {
    int @if; // use if as an identifier

    for(@if = 0; @if < 10; @if++)
      Console.WriteLine("@if is " + @if);
  }
}



SharePoint tenant opt-out for modern lists is retiring in 2019

We're making some changes to how environments can opt out of modern lists in SharePoint. Starting April 1, 2019, we're going to be...