• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Constructor Understanding

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

back again on another project and with another questions.

Project includes a PhoneBook with different classes which I need to access.
The solution which should be implemented should work according to the code below.



Don't really understand the last PhoneBook line that's why am struggling at the moment...

So what's the question, I implemented a Class Entry and now want to have a class PhoneBook which contains an array entries.
The Array entries contains objects from the class entry (firstName, lastName etc.)

I know want to write a Constructor according to the code above that accepts arrays from Entry and initializes the Object.
How can I write the Constructor so he takes the array from Entry and puts it in the Phone Book?

Something like:


Thanks for any help!
 
Marshal
Posts: 66637
251
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to have two separate questions.
  • If you are returning a phone number, is that going to be equal to a contact?
  • Then you are asking about writing a constructor. What do you know about constructors already? What does your book say about constructors? Search for defensive copies; there is a section about that in Effective Java by Joshua Bloch.
  •  
    Michael Grünau
    Ranch Hand
    Posts: 53
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, I already wrote the Search Method for the lower Part,  my problem is that I don't really understand the Code from above.

    What does the following line mean?


    That I have a some sort of PhoneBook from the beginning and that I creat a new one called "phoneBook" based on the PhoneBook Method/ Class and my Array Entries?

    I can implement something like


    Where Entry stands for my Class Entry which should hand-over some Entries in a Array called "entries".
    But what is the use of PhoneBook here?
     
    Campbell Ritchie
    Marshal
    Posts: 66637
    251
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, I was saying contact when I should have said entry.
    Your phone book object contains an array of entries; it can be used to search the entries, add new entries, etc. At this point you will probably find that an array isn't as good a data structure as we thought at first, and classes from the Collections Framework would be much easier to use. But you can change the implementation as long as the field in line 1 has private access without anybody knowing anything about it. You can still pass an Entry[] as an argument to the constructor even if the implementation doesn't use an array.
     
    Michael Grünau
    Ranch Hand
    Posts: 53
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well , I have a another project including the same topic with three different classes and now it does not work again.

    First of all I have my Animal Class



    Afterwards the Vivarium Class



    And finaly a Zoo Class


    Why does my code work for Vivarium but when I want to use the same style for my Zoo like:
    Vivarium[] vivaria; it shows me that Vivarium cannot be resolved to a Type?
     
    Sheriff
    Posts: 6456
    172
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When you're asking about a error message, please include the entire error message.  It may contain clues that will help us.

    What does your project's directory layout look like?  Can Zoo access Vivarium?  You may need an include statement to tell Zoo where Vivarium is.
     
    Michael Grünau
    Ranch Hand
    Posts: 53
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the quick reply.

    The Error Message is only "Vivarium" cannot be resolved to a type"

    The idea behind the classes is basically that you creat an animal which gets listed in an array for Vivarium where you have informations about the vivarium and than the class Zoo contains a array containing all Vivarium with their animals.
    I thought the Arrays can simply be moved by something like:

    Animals [] a Name;

    and

    Vivarium [] a Name;

    but it is only working inside the Vivarium Class and not longer in Zoo...
    Why is that?
     
    Knute Snortum
    Sheriff
    Posts: 6456
    172
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    I wrote:What does your project's directory layout look like?  Can Zoo access Vivarium?  You may need an include statement to tell Zoo where Vivarium is.

     
    Michael Grünau
    Ranch Hand
    Posts: 53
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry but I don't really understand what you mean/ meant...

    Zoo needs to access Vivarium to get the created Vivarium and calculate the costs for each Vivaria to Sum up the total costs of the Zoo.

     
    Knute Snortum
    Sheriff
    Posts: 6456
    172
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are Zoo and Vivarium in different directories?  How are you running the project?  From a command line?  From an IDE?
     
    Campbell Ritchie
    Marshal
    Posts: 66637
    251
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What's a MiniJava? Can you really say that an Animal IS‑A MiniJava? Can you really say that a Zoo IS‑A MiniJava? Can you really say that a Vivarium IS‑A MiniJava?
    Unless you can confidently answer YES to my three questions, are you right to use the extends keyword for those three classes? Or is MiniJava some sort of general type, rather like java.lang.Object?
     
    A timing clock, fuse wire, high explosives and a tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!