Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Couple questions on my code for a setters and getters

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Couple questions on my code for a setters and getters

    Here's what I have so far.....a few errors that i cant figure out. I'm still really green, so sorry everyone....

    //country file
    public class Country
    {
       // fields
    	private String name;
    	private int population; 
    	private int area; // in square miles
     
    	// constructors
     
    	// methods
    	public String getName()
    	{
    		return name;
    	}
     
    	public int getPopulation()
    	{
    		return population;
    	}	
     
    	public int getArea()
    	{
    		return area;
    	}	
     
    	public void setName(String newName)
    	{
    		name = newName;
    	}
     
    	public void setPopulation(int newPopulation)
    	{
    		population = newPopulation;
    	}
     
    	public void setArea(int newArea)
    	{
    		area = newArea;
    	}
     
    	// population per square mile
    	public int populationDensity()
    	{
    		int density;
    		density = population / area; 
    		return density;
    	}
    }
     
    //countryApp file
     
    public class CountryApp
    System.out.println("Testing class Country");
          Country Country1 = new Country();
          System.out.println();
     
          Country1.setName("Macau");
          Country1.setPopulation(453000 %,d);  //I would like my output to have the commas in the right locations in this int//
          Country1.setArea(6);
     
          System.out.printf("Name: %s%n", Country1.getName());
          System.out.printf("Population: %,d%n", Country1.getPopulation());
          System.out.printf("Area: %d%n", Country1.getArea());
          System.out.printf("Population Density: %,d", populationDensity(density)); //What am i doing wrong here to get my output to read population density, with commas?//




    }
    }
    Last edited by dustinvincen; September 23rd, 2014 at 11:42 PM.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Couple questions on my code for a setters and getters

    Do you have any questions?
    If you are getting errors perhaps you should show us the entire error text.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Couple questions on my code for a setters and getters

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    Now ask questions, post errors, let us know exactly what you need help with.
    What am i doing wrong here to get my output to read population density, with commas?
    Not sure what this means. Give an example; post the results of a sample run, if possible.

  4. #4
    Junior Member
    Join Date
    May 2013
    Location
    Charleston SC
    Posts
    21
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default Re: Couple questions on my code for a setters and getters

     System.out.printf("Population Density: %s", NumberFormat.getNumberInstance(Locale.US).format(populationDensity(density)));
    //this should convert your integer to a String representation of that integer with commas

  5. The Following User Says Thank You to jbarke12 For This Useful Post:

    dustinvincen (September 25th, 2014)

  6. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Couple questions on my code for a setters and getters

    jbarke12 thank you. I got it fixed.

Similar Threads

  1. Setters and getters
    By maths94 in forum What's Wrong With My Code?
    Replies: 33
    Last Post: December 11th, 2013, 08:28 PM
  2. help with setters and getters
    By takira in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2013, 12:34 PM
  3. help with setters and getters
    By takira in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2013, 04:10 PM
  4. Encapsulation (getters and setters) Tips?
    By Robertgif in forum Java Theory & Questions
    Replies: 4
    Last Post: March 2nd, 2013, 06:36 AM
  5. Replies: 6
    Last Post: October 31st, 2012, 06:16 AM