Results 1 to 12 of 12
  1. #1
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default array in constructor.

    Here is the code

    Java 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);
    	}
    }
    and the error messages

    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
    what am i doing wrong here.

  2. #2
    scd250 is offline Member
    Join Date
    Oct 2011
    Posts
    34
    Rep Power
    0

    Default Re: array in constructor.

    wrong thread. :P

  3. #3
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default Re: array in constructor.

    Quote Originally Posted by scd250 View Post
    wrong thread. :P
    where should i post ?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: array in constructor.

    Quote Originally Posted by Juukamen View Post
    where should i post ?
    Here is fine; w.r.t. your problem: you do create a new left array but you're trying to use the 'right' array while you haven't allocated a new array for it. The compiler complains about it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default 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:
    Java Code:
    G:\Coding\Skole\Øving 9\tabeller.java:22: error: variable right might not have been initialized 
                right[i] = left[i];
    and for the client class i got the

    Java Code:
    shoes test1 = new shoes("Nike", "Black", 300, 35,30);
    whom is arguing with me on this error.

    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
    [/CODE]

    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])

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: array in constructor.

    I commented the code in your constructor:

    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.     
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default Re: array in constructor.

    Quote Originally Posted by JosAH View Post
    I commented the code in your constructor:

    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.     
    }
    kind regards,

    Jos
    Java 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];
    		}
    moved the
    Java Code:
    this.right = new int[right.length];
    just above the copy of the left.

    Java Code:
    G:\Coding\Skole\Øving 9\tabeller.java:21: error: variable right might not have been initialized
    		this.right = new int[right.length];
    can that be a reason since it's not in the constructor
    Java Code:
    public shoes(String brand, String colour, int price, int minSize, int[] left)

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: array in constructor.

    What is right.length supposed to mean if there is no array 'right' yet?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default 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:

    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. 
        }
    What you should try is this:

    Java Code:
    this.left = left; // Now your field array, left, has the same value as the array passed in.
    then i'm not telling you the rest, because this looks liek a homework assignment.

    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. #10
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default 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.

    Constructor
    Java Code:
    public shoes(String brand, String colour, int price, int minSize, int[] left, int[] right)
    and 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.

    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 ^^

  11. #11
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default 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.

  12. #12
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default Re: array in constructor.

    sorry for the language slangs. I'm norwegian so that might not help the case.

    are you thinking something like
    Java 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

  1. array copy in class constructor.
    By Juukamen in forum New To Java
    Replies: 2
    Last Post: 10-29-2011, 12:07 AM
  2. So Confused (array/arrayList in constructor)
    By thesinter in forum New To Java
    Replies: 3
    Last Post: 12-01-2009, 09:53 PM
  3. Problem with using an array in a constructor
    By planesinspace in forum New To Java
    Replies: 3
    Last Post: 08-28-2009, 09:17 AM
  4. Sending an array in a constructor?
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 09-14-2008, 09:59 PM
  5. Array Constructor
    By Javanoob828282 in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •