Results 1 to 12 of 12
Thread: array in constructor.
- 10-30-2011, 09:13 AM #1
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
array in constructor.
Here is the code
and the error messagesJava Code:class shoes{ private final String brand; private final String colour; private final int price; private final int minSize; private final int[] left; private final int[] right; public shoes(String brand, String colour, int price, int minSize, int[] left) { this.brand = brand; this.colour = colour; this.price = price; this.minSize = minSize; this.left = new int[left.length]; for(int i=0;i<left.length;i++){ right[i] = left[i]; } this.right = new int[right.length]; // thisone shall be one deep copy of the left array. } public String getBrand() { return brand; } public String getColour(){ return colour; } public int getPrice() { return price; } private int getIndeks(int str) { int a = left[str]; return a; } public int setRightShoe(int str,int ant) { return right[str] = ant; } public int setLeftsko(int str,int ant) { return left[str] = ant; } public int[] getLeft() { int[] tabell = new int[left.length]; for(int i=0;i<left.length;i++){ tabell[i] = left[i]; } return tabell; } public int[] getRight() { int[] tabell2 = new int[right.length]; for(int i=0;i<right.length;i++){ tabell2[i] = right[i]; } return tabell2; } public int getCouple() { int couple = 0; for(int i=0;i<left.length;i++){ if(left[i] == right[i]) { couple++; } } return couple; } public int getTotalValue() { int value = 0; int couple2 = 0; for(int i=0;i<left.length;i++){ getCouple(); couple2++; } value = couple2 * price; return value; } public int getSizes() { int sizes = 0; for(int i=0;i<left.length;i++){ if(left[i] <= 1) { sizes++; } } return sizes; } public int getLowestSize() { return minSize; } } class client{ public static void main(String[] args){ shoes test1 = new shoes("Nike", "Black", 300, 35,30); } }
what am i doing wrong here.Java Code:G:\Coding\Skole\Øving 9\tabeller.java:22: error: variable right might not have been initialized right[i] = left[i]; ^ G:\Coding\Skole\Øving 9\tabeller.java:24: error: variable right might not have been initialized this.right = new int[right.length]; // thisone shall be one deep copy of the left array. ^ G:\Coding\Skole\Øving 9\tabeller.java:108: error: constructor shoes in class shoes cannot be applied to given types; shoes test1 = new shoes("Nike", "Black", 300, 35,30); ^ required: String,String,int,int,int[] found: String,String,int,int,int reason: actual argument int cannot be converted to int[] by method invocation conversion 3 errors Tool completed with exit code 1
- 10-30-2011, 09:29 AM #2
Member
- Join Date
- Oct 2011
- Posts
- 34
- Rep Power
- 0
Re: array in constructor.
wrong thread. :P
- 10-30-2011, 10:44 AM #3
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
- 10-30-2011, 11:05 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: array in constructor.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-30-2011, 11:46 AM #5
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: array in constructor.
I got 6 variables in the shoes class. only gonna declare 5 of them and the last one will be a copy from the last one. meaning i will declare the left and right will be an copy of the left.
Quite sure that got something with this error todo:
and for the client class i got theJava Code:G:\Coding\Skole\Øving 9\tabeller.java:22: error: variable right might not have been initialized right[i] = left[i];
whom is arguing with me on this error.Java Code:shoes test1 = new shoes("Nike", "Black", 300, 35,30);
[/CODE]Java Code:shoes test1 = new shoes("Nike", "Black", 300, 35,30); ^ [CODE] required: String,String,int,int,[B]int[][/B] found: String,String,int,int,[B]int[/B] reason: actual argument int cannot be converted to int[] by method invocation conversion 3 errors
where i can understand i've done something wrong in my shoes constructor.
Java Code:public shoes(String brand, String colour, int price, int minSize, [B]int[] left[/B])
- 10-30-2011, 11:53 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: array in constructor.
I commented the code in your constructor:
kind regards,Java Code:public shoes(String brand, String colour, int price, int minSize, int[] left) { this.brand = brand; // set the simple member variables this.colour = colour; // ditto this.price = price; // ditto this.minSize = minSize; // ditto this.left = new int[left.length]; // a new member array 'left' for(int i=0;i<left.length;i++) { right[i] = left[i]; // there is no new member array 'right' yet ... } // too late: this.right = new int[right.length]; // thisone shall be one deep copy of the left array. }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-30-2011, 12:07 PM #7
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: array in constructor.
moved theJava Code:public shoes(String brand, String colour, int price, int minSize, int[] left) { this.brand = brand; this.colour = colour; this.price = price; this.minSize = minSize; this.left = new int[left.length]; this.right = new int[right.length]; for(int i=0;i<left.length;i++){ right[i] = left[i]; }just above the copy of the left.Java Code:this.right = new int[right.length];
can that be a reason since it's not in the constructorJava Code:G:\Coding\Skole\Øving 9\tabeller.java:21: error: variable right might not have been initialized this.right = new int[right.length];
Java Code:public shoes(String brand, String colour, int price, int minSize, int[] left)
- 10-30-2011, 12:34 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: array in constructor.
What is right.length supposed to mean if there is no array 'right' yet?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-30-2011, 12:39 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
Re: array in constructor.
Right. In your constructor you're passing in some variables. {String brand, String colour, int price, int minSize, int[] left}. Nowhere in there are you passing in an array called right. And in your fields, the array "right[]" has no length! So you're initialising an array with no length. The code doesn't throw a semantic error because theoretically there's nothing wrong with it, but when you try to run it, you're attempting to use an array with no memory locations:
What you should try is this:Java Code:public shoes(String brand, String colour, int price, int minSize, int[] left) { // At this point we have an actual parameter, which is an array of integers called left. this.brand = brand; this.colour = colour; this.price = price; this.minSize = minSize; this.left = new int[left.length]; // You won't have a copy of your array. You'll have an empty array, with the same amount of memory as the array you've passed in. this.right = new int[right.length]; // This array has been initialized with no length. for(int i=0;i<left.length;i++){ right[i] = left[i]; // This is illegal. right[i] does not exist, and left[i] has no value. }
then i'm not telling you the rest, because this looks liek a homework assignment.Java Code:this.left = left; // Now your field array, left, has the same value as the array passed in.
Then you can soldier on with the for loop and populating the array to make your deep copy.
I strongly, strongly suggest you go and read a few tutorials too avoid something like this happening in the future :)
- 10-30-2011, 05:54 PM #10
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: array in constructor.
indeed christopherx it's schoolwork. that easy to sniff out heh =) and as i've said, i've been trying to search for answers and my book, seems to be piss poor on this case. wanna murder my teacher or the writters of the book for bad task/book :D
Is there any special sites that you want to reccomend ? I do have the new boston tutorials downloaded and will go trough them all from 1 to 84 when i'm done with the exercises since right, got 3 more to come. and then i'm up for the exam in the spring. time to prepare then :D
Back to the task at hand.
Constructorand then the right is declared. but the client will not be giving the right varibale, only the left one. since it should not be needfull to say that you got 30 left shoes and maybe 31 left shoes ? that's just wrong.Java Code:public shoes(String brand, String colour, int price, int minSize, int[] left, int[] right)
So that is the reason for the copy from left to the right.
gunna send this code in for my teacher and get her comments on it by tomorrow. I'm prolly gonna get tossed out of the course ^^
- 10-30-2011, 06:02 PM #11
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
Re: array in constructor.
Eeee no. You've missed what I meant mate! by the way, you use British slang. GO UK. Okay, "back to the task at hand". You're right, having an array of right shoes being passed too you is inefficient, as the shoes come in pairs so it's daft. So all you need to do is, as I said initialize the right array with the same length as the "left" array. That way you've got two lovely arrays.
The deep copying is also very simple, and I've even got an example of it in the slight change to your code that I suggested. I'm really doing my best to not tell you the answer, because i'd feel bad for stealing that sense of acheivement you get when you work out something for yourself, but your original constructor parameters were okay. Just think how you can create right from the data and the length of left.
- 10-30-2011, 10:14 PM #12
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: array in constructor.
sorry for the language slangs. I'm norwegian so that might not help the case.
are you thinking something likeJava Code:public shoes(String brand, String colour, int price, int minSize, int[] left){ this.brand = brand; this.colour = colour; this.price = price; this.minSize = minSize; this.left = left; this.right = new int[left.length]; for(int i=0;i<left.length;i++){ right[i] = left[i]; }
Similar Threads
-
array copy in class constructor.
By Juukamen in forum New To JavaReplies: 2Last Post: 10-29-2011, 12:07 AM -
So Confused (array/arrayList in constructor)
By thesinter in forum New To JavaReplies: 3Last Post: 12-01-2009, 09:53 PM -
Problem with using an array in a constructor
By planesinspace in forum New To JavaReplies: 3Last Post: 08-28-2009, 09:17 AM -
Sending an array in a constructor?
By dch414 in forum New To JavaReplies: 2Last Post: 09-14-2008, 09:59 PM -
Array Constructor
By Javanoob828282 in forum New To JavaReplies: 1Last Post: 04-30-2008, 10:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks