How do I make this:
public class Country {
private String name;
private double area;
private int population;
public Country(String name, double area, int population) {
this.name = name;
this.area = area;
this.population = population;
}
public double getPopulationDensity() {
return population / area;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public static void main(String[] args) {
Country country = new Country("United States of America", 3797000, 327000000);
System.out.println("Population density of " + country.getName() + " is " + country.getPopulationDensity() + " per square mile");
}
}
Into a java file?
In order to make this a java file, create a file named "Country.java" in any text editor like Notepad, SUblime text, etc and paste the following content:
public class Country {
private String name;
private double area;
private int population;
public Country(String name, double area, int population) {
this.name = name;
this.area = area;
this.population = population;
}
public double getPopulationDensity() {
return population / area;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public static void main(String[] args) {
Country country = new Country("United States of America", 3797000, 327000000);
System.out.println("Population density of " + country.getName() + " is " + country.getPopulationDensity() + " per square mile");
}
}
Then go to a terminal and run the following commands:
Get Answers For Free
Most questions answered within 1 hours.