Problem 1 The City class is defined as follows public class
Problem 1. The City class is defined as follows:
public class City {
public String name;
private int population;
public static int totalCities = 0;
public static final int maxBudget = 20;
public City(String theName, int thePopulation) {
name = theName;
population = thePopulation;
}
public void display() { System.out.println(‘‘Name: ’’ + name + ‘‘ Population: ’’ + population); } }
Which of the following are considered valid statements that can appear in the main method of the driver for this class. Write Valid or Invalid next to each entry.
(a) City.name = null;
(b) City.population = 10;
(c) City.totalCities = 100;
(d) City c1 = City();
(e) City c2 = new City();
(f) City c3 = new City(‘‘Philadelphia’’, 3);
(g) City.display();
(h) c2.name = ‘‘NYC’’;
(i) c2.population = 10;
(j) City.maxBudget = 30;
Solution
a) Invalid Statment in main method //City.name = null;
Error Occurs: non-static variable name cannot be referenced from a static context
b) Invalid Statment in main method //City.population = 10;
Error Occurs: non-static variable name cannot be referenced from a static context
c) Valid Statment in main method //City.totalCities = 100;
Reason for valid : static variable name can be referenced from a static context
d) Invalid Statment in main method //City c1 = City();
Error occured cannot find symbol City()
e) Invalid Statment in main method //City c2 = new City();
Constructor City () can not overide the already defined constructor City with two argumnets
f) Valid Statment // City c3 = new City(‘‘Philadelphia’’, 3);
Reason :Defined constructor will be called
g ) Invalid Statment in main method //City.display();
Reason : non-static method display() cannot be referenced from a static context
h) Invalid Statment in main method //c2.name = ‘‘NYC’’;
Reason : c2 Object can not ceated
i) Invalid Statment in main method //c2.population = 10;
Reason : c2 Object can not ceated
j) Invalid Statment in main method //City.maxBudget = 30;
Reason : Error: cannot assign a value to final variable maxBudget again

