Asked by Natalie
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;
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;
Answers
Answered by
MathMate
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.
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.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.