I am trying to teach myself C# and I have run into a problem with one of the chapter assignments. The problem is as follows:
Create a class named City. Objects will be instantiated from this class. Include fields that hold the City's name, its state, and its population. Include a constructor that accepts arguments to assign to all three fields. Also include methods to get and set each field. Save the class as City.cs.
Create a class named MovingHistory. Instantiate two City objects---myCity and myFriendsCity. When you declare each object, provide constructor parameters that set appropriate City values for the birthplaces of you and your friend. Display the City objects. Then use the set methods to change each City object and reflect a City to which you relocated later in life. Display the City objects again. Save the class as MovingHistory.cs.
After 3 days, this is what I have come up with. Can someone help me?
using System;
using CityNamespace;
public class City
{
private string name;
private int population;
private string state;
public City(string city, int pop, string st)
{
public string GetName()
{
return city;
}
public int GetPopulation()
{
return population;
}
public string GetState()
{
return State;
}
public void SetName(string city)
{
stringCity = city;
}
public void SetPopulation(int pop)
{
popNumber = population;
}
public void SetState(string st)
{
stringState = state;
}
}
using System;
using CityNamespace;
public class MovingHistory
{
public static void Main()
{
CitymyCity = new City("Stafford",92000,"VA");
CtiymyFriendsCity = new City("Athens",100000,"GA");
Console.WriteLine("I was born in {0},{1}. Population: {2}",
myCity.GetName(),mycity.GetState(),
myCity.GetPopulation().ToString("n0"));
Console.WriteLine("My friend was born in {0},{1}. Population: {2}",
myFriendsCity.GetName(),myfriendsCity.GetState(),
myFriendsCity.GetPopulation().ToString("n0"));
myCity.SetName("Stafford");
myCity.SetPopulation(92000");
myCity.SetState("VA")'
myFriendsCity.SetName("Athens");
myFriendsCity.SetPopulation(100000);
myFriendsCity.SetState("GA");
Console.WriteLine("Now I live in {0},{1}. Population: {2}",
myCity.GetName(),myCity.GetState(),
myCity.GetPopulation().ToString("n0"));
Console.WriteLine("Now my friend lives in {0},{1}. Population: {2}",
myFriendsCity.GetName(),myFriendsCity.GetState(),
myFriendsCity.GetPopulation().ToString("n0"));
}
}