Add a method to Building

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// 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. 
public class BuildingDemo2 {   
  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:
  " + 
                      house.floors + " floors
  " + 
                      house.occupants + " occupants
  " + 
                      house.area + " total area"); 
    house.areaPerPerson(); 
 
    Console.WriteLine(); 
 
    Console.WriteLine("office has:
  " + 
                      office.floors + " floors
  " + 
                      office.occupants + " occupants
  " + 
                      office.area + " total area"); 
    office.areaPerPerson(); 
  }   
}