Question

How do I make this: public class Country {     private String name;     private double area;     private...

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?

Homework Answers

Answer #1

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:

  • javac Country.java
  • java Country
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a pseudocode for the following java programs: public class Item {    private String name;...
Write a pseudocode for the following java programs: public class Item {    private String name;    private double cost;    public Item(String name, double cost) {        this.name = name;        this.cost = cost;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getCost() {        return cost;    }    public void setCost(double cost) {...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
Q:Could you fill in the blank。 class Employee{ // field private String name; private (1) _float__...
Q:Could you fill in the blank。 class Employee{ // field private String name; private (1) _float__ salary; // constructor (2) ________________________________________ // method public (3) __String____ getName( ){ return name; } public float getSalary( ){ return Salary; } public (4) ________ compare( (5) _________________ { (6) ____________________________________ } }// class Employee public class EmployeeUI{ public static void main(String[ ] args){ Employee e1 = new Employee(“William”); Employee e2 = new Employee(“Rchard”); if(Employee.compare(e1, e2)) System.out.println(“same employee”); else System.out.println(“different employee”); } }//class EmployeeUI
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private boolean isBalancedByNodeCount() { /**************************************************************************** Implement this method and replace the return statement below with your code. * Definition of Balance tree (On page 372 of book): * An unbalanced tree is created when most of the nodes are on one side of the root or the other. ****************************************************************************/    return false; }       public static void main(String args[]) { int[] values1 = {50,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...