I was practising the past year question just now. I wanted the program to show this:
Continent name: Australia
Number of Countries: 1
Country name: Australia
Population: 22118256
But my code makes the program to show the first 2 lines.
package jan2012q5b;
import java.util.*;
class Continent
{
protected String continentName;
protected int countriesNo;
public Continent()
{
continentName = "??";
countriesNo = 0;
}
public Continent(String continentName, int countriesNo) {
this.continentName = continentName;
this.countriesNo = countriesNo;
}
public String getContinentName() {
return continentName;
}
public void setContinentName(String continentName) {
this.continentName = continentName;
}
public int getCountriesNo() {
return countriesNo;
}
public void setCountriesNo(int countriesNo) {
this.countriesNo = countriesNo;
}
@Override
public String toString()
{
return "Continent name: " + continentName + "\n" +
"Number of countries: " + countriesNo + "\n";
}
}
class Country extends Continent
{
private String countryName;
private int population;
public Country()
{
countryName = "???";
population = 0;
}
public Country(String countryName, int population)
{
this.countryName = countryName;
this.population = population;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public Country(String count_name, int p, String continentName, int countriesNo)
{
super(continentName, countriesNo);
countryName = count_name;
population = p;
}
@Override
public String toString()
{
return "Country name: " + countryName + "\n" +
"Population: " + population;
}
}
public class Jan2012q5b
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Country cnt;
cnt = new Country ("Australia", 1, "Australia", 22118256);
System.out.println(cnt.toString());
}
}