1 Replies - 382 Views - Last Post: 15 November 2012 - 04:39 AM Rate Topic: -----

#1 Waj3627  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-November 12

method populationDensity in class City cannot be applied to given type

Posted 14 November 2012 - 08:48 PM

I am trying to get my program to print the population density, however it keeps returning an error message that says "method populationDensity in class City cannot be applied to given types".

What i can't figure out is if there is an error with my method or if it is an error in trying to call it to the print statement.

public class Project4
{
    public static void main (String [] args)
    {
        City c1 = new City ("Wilmington", 106476, 41.5);
        System.out.println("The City name is: " + c1.getName());
        System.out.println("The population is: "  + c1.getPopulation());
        System.out.println("The area is: " + c1.getArea());
        System.out.println("The population density is: " + c1.populationDensity(c1));
        
        System.out.println();
        
        City c2 = new City ();
        System.out.println("The City name is: " + c2.getName());
        System.out.println("The population is: "  + c2.getPopulation());
        System.out.println("The area is: " + c2.getArea());
        
        System.out.println();
        
        c2.setName("Charlotte");
        c2.setPopulation(751087);
        c2.setArea(280.5);
        
        System.out.println("The City name is: " + c2.getName());
        System.out.println("The population is: "  + c2.getPopulation());
        System.out.println("The area is: " + c2.getArea());
        
        System.out.println();
        
        City c3 = new City ("Gastonia", 72068, 46.3);
        System.out.println("The City name is: " + c3.getName());
        System.out.println("The population is: "  + c3.getPopulation());
        System.out.println("The area is: " + c3.getArea());
    }
    
}



public class City
{
    private String name;
    private int population;
    private double area;
    
    public City (String n, int p, double a)
    {
        this.name = n;
        this.population = p;
        this.area = a;
    }
    public City()
    {
        name = "No data was entered";
        population = 0;
        area = 0;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getPopulation()
    {
        return population;
    }
    public void setPopulation(int population)
    {
        this.population = population;
    }
    public double getArea()
    {
        return area;
    }
    public void setArea(double area)
    {
        this.area = area;
    }
    public double populationDensity(int population, double area)
    {   
        population = getPopulation();
        area = getArea();
        double density = population / area;
        
        return density;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: method populationDensity in class City cannot be applied to given type

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: method populationDensity in class City cannot be applied to given type

Posted 15 November 2012 - 04:39 AM

Quote

System.out.println("The population density is: " + c1.populationDensity(c1));
needs to be

System.out.println("The population density is: " + c1.populationDensity(c1.getPopulation(), c1.getArea()));

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1