C
C#2y ago
Darma

❔ Trouble with modifiers

Working with modifiers/getters and setters now, and unfortunately I'm having trouble with my modifiers returning the right input once I make a change with a setter via the driver class.
7 Replies
Darma
DarmaOP2y ago
class BuildingBlueprint
{
private int numOfStories;
private int numofApts;
private float occupancy;
private bool fullyOccupied;

public BuildingBlueprint()
{
numOfStories = 10;
numofApts = 20;
occupancy = 1.0f;
fullyOccupied = true;
}

public BuildingBlueprint(int nS, int nA, float o)
{
numOfStories = nS;
numofApts = nA;
occupancy = o;

if (occupancy == 1.0f)
{
fullyOccupied = true;
}
else
{
fullyOccupied = false;
}


}
public float Occupancy
{
get { return occupancy; }
set { occupancy = value; }

}

public int Stories
{
get { return numOfStories; }
}

public int Apts
{
get { return numofApts; }
}

public bool FullyOccupied
{
get { return fullyOccupied; }
}

}
class BuildingBlueprint
{
private int numOfStories;
private int numofApts;
private float occupancy;
private bool fullyOccupied;

public BuildingBlueprint()
{
numOfStories = 10;
numofApts = 20;
occupancy = 1.0f;
fullyOccupied = true;
}

public BuildingBlueprint(int nS, int nA, float o)
{
numOfStories = nS;
numofApts = nA;
occupancy = o;

if (occupancy == 1.0f)
{
fullyOccupied = true;
}
else
{
fullyOccupied = false;
}


}
public float Occupancy
{
get { return occupancy; }
set { occupancy = value; }

}

public int Stories
{
get { return numOfStories; }
}

public int Apts
{
get { return numofApts; }
}

public bool FullyOccupied
{
get { return fullyOccupied; }
}

}
class Lab13A
{
public static void Main(string[] args)
{
BuildingBlueprint buildingOne = new BuildingBlueprint();
BuildingBlueprint buildingTwo = new BuildingBlueprint(30, 30, .75f);

Console.WriteLine("Year 2020");
Console.WriteLine("Building 1 has " + buildingOne.Stories + " floors, " + buildingOne.Apts + " apartments, and is " + buildingOne.Occupancy * 100 + "% occupied. Full? " + buildingOne.FullyOccupied);
Console.WriteLine("Building 2 has " + buildingTwo.Stories + " floors, " + buildingTwo.Apts + " apartments, and is " + buildingTwo.Occupancy * 100 + "% occupied. Full? " + buildingTwo.FullyOccupied);

Console.WriteLine("Many years pass.");
buildingOne.Occupancy = 0.0f;
buildingTwo.Occupancy = 1.0f;
//Console.WriteLine("Building 1 has " + buildingOne.Stories + " floors, " + buildingOne.Apts + " apartments, and is " + buildingOne.Occupancy * 100 + "% occupied. Full? " + buildingOne.FullyOccupied) ;
//Console.WriteLine("Building 2 has " + buildingTwo.Stories + " floors, " + buildingTwo.Apts + " apartments, and is " + buildingTwo.Occupancy * 100 + "% occupied. Full? " + buildingTwo.FullyOccupied);
Console.WriteLine(buildingOne.Occupancy);
Console.WriteLine(buildingOne.FullyOccupied);
}
}
class Lab13A
{
public static void Main(string[] args)
{
BuildingBlueprint buildingOne = new BuildingBlueprint();
BuildingBlueprint buildingTwo = new BuildingBlueprint(30, 30, .75f);

Console.WriteLine("Year 2020");
Console.WriteLine("Building 1 has " + buildingOne.Stories + " floors, " + buildingOne.Apts + " apartments, and is " + buildingOne.Occupancy * 100 + "% occupied. Full? " + buildingOne.FullyOccupied);
Console.WriteLine("Building 2 has " + buildingTwo.Stories + " floors, " + buildingTwo.Apts + " apartments, and is " + buildingTwo.Occupancy * 100 + "% occupied. Full? " + buildingTwo.FullyOccupied);

Console.WriteLine("Many years pass.");
buildingOne.Occupancy = 0.0f;
buildingTwo.Occupancy = 1.0f;
//Console.WriteLine("Building 1 has " + buildingOne.Stories + " floors, " + buildingOne.Apts + " apartments, and is " + buildingOne.Occupancy * 100 + "% occupied. Full? " + buildingOne.FullyOccupied) ;
//Console.WriteLine("Building 2 has " + buildingTwo.Stories + " floors, " + buildingTwo.Apts + " apartments, and is " + buildingTwo.Occupancy * 100 + "% occupied. Full? " + buildingTwo.FullyOccupied);
Console.WriteLine(buildingOne.Occupancy);
Console.WriteLine(buildingOne.FullyOccupied);
}
}
Darma
DarmaOP2y ago
The bools aren't changing despite the occupancy changing
Jiodi
Jiodi2y ago
Your first problem is making the class mutable in the first place
undisputed world champions
you only set fullyOccupied inside the constructor you either want to re-evaluate it when 'Occupancy' is set or have FullyOccupieds getter as return occupancy == 1.0f; to evaluate it any time FullyOccupied is accessed, i guess 😉
Jiodi
Jiodi2y ago
Or just create a static method that returns a new BuildingBlueprint class with the desired values upon change So you dont have to mutate at all BuildingBlueprint.Create(blueprint1.Stories, blueprint1.Apartments, [newVal]) public static BuildingBlueprint Create(int stories, int apartments, float occupancy) => new BuildingBlueprint(stories,apartments,occupancy); Privatize ur ctors
Darma
DarmaOP2y ago
ah thank you guys
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?