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 8 of 8

Thread: No idea what to do for this

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default No idea what to do for this

    Here is the prompt I got-
    The code below shows some of the text for a class called City. As you can see, a City has two instance variables, name and population. Write a complete public constructor (including header line) with no parameters, which sets the population to an initial value of -1, and the name to an initial value of "unknown".

    public class City {
     
      private String name;
      private int population;

    thats all i know haha


  2. #2
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: No idea what to do for this

    I believe what you're being asked to do is something like this:

    public City(String _name, int _population)
    {
    population = _population;
    name = _name;
    }

    And then create a "City":

    City NewCity = new City("New York", 100);

    See how I use the constructors I created in making a new "City"?
    I'm a beginner but I'm pretty sure that's right.

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: No idea what to do for this

    Then what is the actual answer? because all i have so far is

    public class City {
     
      private String name;
      private int population;

    so im not sure what follows it

  4. #4
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: No idea what to do for this

    It would be:

    public class City {
    public static void main(String args[]){
      private String name;
      private int population;
      public City(String _name, int _population)
     {
        population = _population;
        name = _name;
     }
    City NewCity = new City("New York", 100);
    }
    }

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: No idea what to do for this

    Yeah, that didn't work.

    I tried substituting "unknown" for "New York" and -1 for 100 because thats what the prompt said but that didnt work either

  6. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: No idea what to do for this

    I figured it out.

    public City()
    {
    name = "unknown";
    population = -1;
    }
    is what has to be put in

  7. #7
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: No idea what to do for this

    Uhmmmm, that's not a constructor method but sure that will work, I didn't realize you wanted it that simple lol.

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: No idea what to do for this

    That is a constructor. It's what's known as a "default constructor", i.e. it intializes all the values to some pre-determined initial value, in this case -1 and "unknown".

    As a second note, if you are going to be providing other constructors that create an initial value (say, a constructor which also sets the city name), I would recommend calling that constructor from the the default constructor. This reduces the amount of code you have to check for problems.

    public City()
    {
        // call the other constructor that actually assigns values
        this("unknown", -1);
    }
     
    public City(String name, int population)
    {
        this.name = name;
        this.population = population;
    }
    Last edited by helloworld922; July 18th, 2010 at 07:19 PM.

Similar Threads

  1. help with problem havr no idea what it means
    By mdstrauss in forum Exceptions
    Replies: 4
    Last Post: July 27th, 2009, 12:41 AM