• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to construct copy constructors for array objects

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Can someone please post an exemple of a copy constructor for an array object in one class which is called in the main class to make exact copies of the existing array objects.
example to make copies of book[] which is in the class UsingBooks and the copy constructor would be in the class Books.
I have been searching for 2 days and i can not find this.

thank you.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alain Fallada wrote:
Can someone please post an exemple of a copy constructor for an array object in one class which is called in the main class to make exact copies of the existing array objects.
example to make copies of book[] which is in the class UsingBooks and the copy constructor would be in the class Books.
I have been searching for 2 days and i can not find this.



Since array definitions are created by the compiler implicitly, there is no way for you to create a specific "constructor" -- you will have to create a method, that will take an array and create a new instance.

Henry
 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. can you show me just a simple example of a constructor method that would do just that.
I would really appreciate this.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alain Fallada wrote:Ok. can you show me just a simple example of a constructor method that would do just that.
I would really appreciate this.



There is no such a thing as a "constructor method" -- just create a regular method that does this. Or if you don't want to, just put the code inline (as the code is pretty short).

Henry
 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please give me just an exemple of a regular method that would do this with an array object. i just want to have a model to work with.
thanks.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alain Fallada wrote:Can you please give me just an exemple of a regular method that would do this with an array object. i just want to have a model to work with.
thanks.



You want an example on how to copy an array? No offense, but isn't it just creating another array of the same type and size, and coping it via a loop?

Okay, here is your example -- but only because it is silly request.



Also, you should look into the System.arraycopy() method, which can be used instead of the loop.

Henry
 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry if this is too silly. I have an assignment and this is what they are asking me. To create a copy constructor method to create a couple of copies of an array object.



Maybe I should post the whole assignment so that it is easier to understand what they are asking us to do.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alain Fallada wrote:I am sorry if this is too silly. I have an assignment and this is what they are asking me. To create a copy constructor method to create a couple of copies of an array object.



Sorry. I didn't say or even meant to imply that the assignment is silly, or that the task is silly, or that anything indirectly related as silly.

What I meant was that, first, what you are asking for is incredibly easy, and second, there isn't really a catch-all way to copy instances (since it is constructor specific). So, using an example is pretty useless here. And waiting for an example, when you can code the loop in a few minutes is silly.


Alain Fallada wrote:



Well, obviously, the code doesn't compile -- and fixing the compile errors may be a good place to start. After that, take a look at the loop -- notice that your condition is on something that doesn't change. This means that the loop doesn't run, or is an endless loop. This may be another place to examine.


Alain Fallada wrote:
Maybe I should post the whole assignment so that it is easier to understand what they are asking us to do.



Well, that is up to you. If you feel uncomfortable with your interpretation of the assignment, then by all means, post it.

Henry
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be a good idea to post more of the assignment. We often see assignments which are difficult to understand.
There might be another way to copy an array, which you can find out about in the Java Language Specification.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alain Fallada wrote:Can someone please post an exemple of a copy constructor for an array object in one class which is called in the main class to make exact copies of the existing array objects.


I think our confusion comes from the fact that you keep talking about a constructor, and - as has already been pointed out - you can't write a constructor for an array; copy or otherwise.

What you can do is to write a copy constructor for an object that contains an array - and that constructor may indeed be required to copy the internal array. If that's what you want, and your array is one-dimensional, my advice would be to use array.clone(). Since 1.5 (I think), this is by far the easiest (and most readable, IMO) way of copying an array, since it no longer involves casting; however, you should be aware that it does a shallow copy - ie, if the array contains objects, it will copy their references, NOT the objects themselves.

If the array is multi-dimensional, you will have to write a method to do it yourself.

If none of the above covers what you want, I suggest you do as Campbell said, and provide us with a bit more code/information.

Winston
 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, here is the assignment:

A book object has three attributes, a title (String), an ISBN (long), and a price (double).
Part 1:
You are required to design and implement the Book class according to the following specifications:
- Upon the creation of a book object, the object must immediately be initialized with valid values; that is title, ISBN and price. You may assume that ISBN numbers do NOT start with 0 (that is they can start only with digits from 1 to 9).
- Your class should have a copy constructor, so new book objects can be created as exact copies of existing ones.
- The design should allow enough flexibility so that the value of any of these attributes can be modified later on. For example, it should be possible to create a book object with a given price then change its price later on.
- The design should allow the user of the class to obtain the value of any of the attributes individually (i.e. getPrice()).
- The design should also allow the user to view all book information at once using the System.out.print() method. That is, a statement such as: System.out.println(b1); would display all information of book b1.
Hint: Use toString() function.

Part 2:
In this part, you are required to write a public class called BookSearch, which will utilize the Book class created in part 0. In the main method of this class the following must be done:
Create an array of 10 books. All these books must be initialized with proper values; that is: title, price & ISBN. You must use the copy constructor to create some of these objects.
- Ask the user to enter three pieces of information: a book title, a book price, and either Yes or No for a search combination (see details below).
- Read the entered information.
- Depending on whether the user has entered Yes or No for matching combinations, your program will search the array for matching books based on the following rules:
 If the user enters Yes (case insensitive) for matching combinations, then your program will search the array and displays all books that have both the title and the price entered by the user.
 If the user enters No (or anything different than Yes) for matching combinations, then your program will search the array and displays all books that have either the title entered by the user or the price entered by the user.
If no matches were found, the program must indicate that to the user.
As a general rule, displaying of any prices must be formatted using printf.
Guideline: For part 1 and 2, you will need to create one .java file, called BookSearch.java, which includes the Book class as well as the BookSearch class (this is the public class), which will have the main() function.

Part 3:
For this part, you are required to write a class called ModifyBooks, which is going to utilize the Book class that you have created in part 1. In that part, you required to write a public class, the ModifyBooks class. In the main() method of this class the following must be done:
- Create an array of 10 books; all books in the arrays must be initialized with proper values; that is title, price & ISBN. At least 4 of these books should be created using the copy constructor.
- Change the price of all the objects. You can simply use a for loop and set the prices using any formula (for instance, the prices can be set to i*5 + 100), where i is the loop controller variable. You can use the Random class if you wish.
- Ask the user to enter a title, and ISBN of a book.
Read the title and the ISBN, then go through the entire array looking for books with this title and the same ISBN, and keep track of all of their locations.
- Now you need to adjust the prices of the books. All books that were found with the same title and the same ISBN must be adjusted to have the smallest price that any of them has. An indication must be displayed for every object that is modified.
Here is an example to illustrate the requirements. Assume Arr is the name of the array, and that:
Arr[2] has a book with the following information: “Java 3, ISBN 900700, 112$”, Arr[5] has a book with the following information: “Java 3, ISBN 230680, 56$”, Arr[7] has a book with the following information: “Java 3, ISBN 900700, 83$” , and Arr[9] has a book with the following information: “Java 3, ISBN 900700, 92$”.
If the user enters “Java 3” for the title and 900700 for ISBN, then the program must first keep track of the locations of all books with these information; in this example indices 2, 7 & 9. The program must then modify the prices of these objects to the smallest that any of them has; in this case 83$. Consequently the program will modify Arr[2] & Arr[9]. An indication must be displayed to indicate these modifications, similar to the following: Price of book at index 2 has been modified from 112$ to 82$.
As a general rule, displaying of any prices must be formatted using printf.
Guideline: For part 3, you will need to create one .java file, called ModifyBooks.java, which includes an exact copy of the Book class defined in Part 1, as well as the ModifyBooks class (this is the public class), which will have the main() function.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alain Fallada wrote:Hello, here is the assignment:


Right. Well nowhere in there do I see it asking you to create a copy constructor for an array. It's asking you to create one for your Book class, and it's also asking you to use that constructor to create elements in an array.

Winston
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alain Fallada wrote:- Your class should have a copy constructor, so new book objects can be created as exact copies of existing ones.


This means that in your class Book you should make a constructor that takes one parameter, which is also a Book. The Book being initialized should copy its values from the Book that is passed in as an argument.

Alain Fallada wrote:Create an array of 10 books. All these books must be initialized with proper values; that is: title, price & ISBN. You must use the copy constructor to create some of these objects.


That means that you should use the copy constructor to initialize some of the books in the array. It doesn't say you have to copy the array.

Note that a constructor is not a method - the assignment is asking you to create a constructor, not a method.
 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give me an example, it does not have to be related to the assignment. Just so that I can have a clear idea of what is asked. It is my first time working in JAVA and this is not really clear for me.
The teacher does not give a lot of details and as a result most of the class is failling.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would look like this:

See Providing Constructors for Your Classes and Passing Information to a Method or a Constructor in Oracle's Java Tutorials.
 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this right.

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes! Just a few remarks: make isbn a long, not a Long (Java is case-sensitive; Long is something different than long). Also, make the member variables private. Also, you do not need to initialize the member variables with values in lines 4, 5 and 6.
 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. Thank you very much.



now when I want to call it in the main to make some of the objects, how do I do that.
I am not sure.



 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose that you have a Book object (or an array of Book objects), then you can make a new Book by using the copy constructor when you create a new Book.

 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I really appreciate your help.

Now I am working on the section were I need to search for some particular books.
I input the Title and the Price of the books searched and then for now I have just done the code for searching the book. However it does not work. It does not print out anything wether it found title in the search or not.

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Well, here you have demonstrated the standard reason why comparing boolean values to the literals true and false is a bad practice. Namely, it's easy to make a mistake, and instead of comparing something to false, you assign the value false to it. And that's what you did.

Comparing to false would be this:



But as I said, you shouldn't do that. You should just say "if not found then...", like this:

 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this what i need to do?



Now. it always prints that it did not find the value that I am searching for.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alain Fallada wrote:Is this what i need to do?
...
Now. it always prints that it did not find the value that I am searching for.


Then plainly it isn't.

It sounds to me like you're just trying pegs out to see if they fit in the hole. This is called the Jean-Paul Sartres School of Programming; and I'm afraid to say it has many alumni, but not a great academic record.

My advice: StopCoding (read carefully).

Winston
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alain Fallada wrote:Now. it always prints that it did not find the value that I am searching for.



I think with more testing you might find that it doesn't print that message if the last book in the list is the one you're searching for.
 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

I think with more testing you might find that it doesn't print that message if the last book in the list is the one you're searching for.



I did test with all the books in the search and it always does that. Say that it did not find it.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at line 4 in your code:

What is bookTitleSearch? Is it a String that contains the title of a book you're looking for?

Note that in this line you are comparing a Book object with a String. This will always be false. You need to compare the title of the book to bookTitleSearch, not the book itself.
 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must have done something wrong yesterday night because it worked in the afternoon. This is the code.
Yes, bookTitleSearch is a String containing the title of the book.

 
Alain Fallada
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
works fine now, my eclipse program had a problem.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic