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: Creating an exact replica of an original instance?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Creating an exact replica of an original instance?

    Hi guys,
    I'm working on a homework set. Here is the question I am having trouble with.

    Suppose that you want to make a copy of a City instance. This can be done with a public constructor that takes a City instance as a parameter, and makes an exact replica of the orginal instance. It constructs this copy using getName and getPopulation methods.

    The new City instance should have the same name and population values as the City instance that is passed to the constructor as a parameter. The City object that is passed as a parameter should not be modified.

    Here is my code so far:
    public class City {
     
      private String name;
      private int population;
     
      public String getName () {
        return name;
      }
     
      public int getPopulation () {
        return population;
      }
     
      public Object copy(City) {
         return new City(getName(), getPopulation());
    }

    Am I even using the right method? How do I write this?

    Thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an exact replica of an original instance?

    done with a public constructor that takes a City instance as a parameter
    Where is the constructor that takes a City object as a parameter?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Creating an exact replica of an original instance?

      public Object copy(City) {
         return new City(getName(), getPopulation());
    }

    well I think that's what I'm trying to create here.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Creating an exact replica of an original instance?

    So now I have:
    public cityCopy(City){
    name=getName();
    population=getPopulation();
    }

    and it's still wrong.

    It keeps telling me I need a return type. But it's a constructor...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an exact replica of an original instance?

    Why do you think its a constructor? Read the doc at the link I gave.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Creating an exact replica of an original instance?

    I didn't ignore your answer. A constructor is what creates a new object in a certain class. It defines it's parameters so when they are given a value, you know which object it belongs to. I wrote my constructor for cityCopy which takes a City as a parameter and uses the getName and getPopulation methods to copy the attributes from the original city.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an exact replica of an original instance?

    I wrote my constructor for cityCopy
    From the tutorial:
    Constructor declarations look like method declarations—except that they use the name of the class and have no return type.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Insert original title here: Hello there!
    By Leetment in forum Member Introductions
    Replies: 0
    Last Post: May 9th, 2012, 02:48 PM
  2. Resume Application in Original State
    By WhiteSoup12 in forum Android Development
    Replies: 0
    Last Post: November 22nd, 2011, 08:11 PM
  3. Creating new instance
    By vluong in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2009, 11:35 PM