[RESOLVED] why two classes?
The book uses two classes. One class has main in it and is being used to create objects from another class. Or I believe this is what is happening. My question is why is the example using two? Could not one class have main and create objects of that class? The book does not explain why it does this. Maybe someone has a moment to look this over and break it down for me.
Code:
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();
}
}
Why couldn't we just do it this way?
Code:
using System;
// Use the areaPerPerson() method.
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");
}
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();
}
}
Thank you in advance.