Constructors is a method that should (generally) be public, have no type and have their names correspond to the name of the class (City in this case).
So you would begin like this:
public City()
{
}
Now you can do the rest of the requirements including:
1. insert two parameters including type, for example String cityName, and int cityPop (they should be different from those of the state variables.
2. Assign the values of these parameters to the state variable name and population.
And, voilĂ , the constructor will be complete.
The code below shows some of the text for a class called City.
Now write a complete public constructor with two formal parameters:
a String parameter called cityName, and an int parameter called cityPop.
Your code should initialize the name attribute of the class to the value of
cityName, and it should intitialize the population attribute to the value
of cityPop.
public class City {
private String name;
private int population;
1 answer