12 Replies - 416 Views - Last Post: 28 December 2012 - 03:01 PM Rate Topic: -----

#1 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

i cant understand what i return in methods

Posted 28 December 2012 - 05:19 AM

Hellow i have to make a methods of an interface Appendable.
I have make a class with a constractor which make an char array and use the methods in this array but i cant what i reaturn of methods Appendable i confused beacuse the tell in home work i have a char array in my class ArrayAppendable and the methods use this array ok, but in the return i cant put the array ?

hear is the code
public class ArrayAppendable implements Appendable{
   
	static char[] array;
	
	public ArrayAppendable(char[] myArray){
		 array = new char[myArray.length];
		for(int i=0;i<myArray.length;i++)
		array[i]=myArray[i];
	}
	
	public static void toChar(){
		for(int i=0;i<array.length;i++)
			System.out.println(i+" : "+array[i]);
	}
	
	
    static Appendable append(char c) throws IOException{
    	int i=0;
    	while(array.length<i){
        	   if(array[i]=='\0'){
        	   array[i]=c;
        	   break;}
        	   i++;
           }
    	
    	
    	return ;} 
	
	static Appendable append(CharSequence csq){
		
		
		return null;}
}


Is This A Good Question/Topic? 0
  • +

Replies To: i cant understand what i return in methods

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: i cant understand what i return in methods

Posted 28 December 2012 - 05:24 AM

Can you post the assignment? At least the part that describes what the methods are supposed to do. Well, maybe the whole thing if it's not too long. In English, if possible.

This post has been edited by GregBrannon: 28 December 2012 - 05:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: i cant understand what i return in methods

Posted 28 December 2012 - 06:03 AM

The first problem is the static. Not only should you NOT have it, but it's not possible to implement an interface with a static method.

Assuming that Appendable append(char c) throws IOException is part of the Appendable interface, your implementation should look something like:
public Appendable append(char c) throws IOException {
	// check for space, throw exception if full?
	
	// WTF is \0?  This isn't C.
	// maybe
	this.array[this.size++] = c;
	
	// always
	return this;
} 


This post has been edited by baavgai: 28 December 2012 - 10:50 AM
Reason for edit:: oops, missed a tag

Was This Post Helpful? 0
  • +
  • -

#4 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: i cant understand what i return in methods

Posted 28 December 2012 - 07:42 AM

In 1rst function takes as argument a character and puts him on the array, but if it is full then you must have IOException.
and 2 to take a string and place it at the end of array and if it will fit IOException throws, both will return a reference to what I do.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i cant understand what i return in methods

Posted 28 December 2012 - 07:49 AM

1) the array will always be full because the new one size is based on the one passed as parameter
array = new char[myArray.length];

2) this is not C so there is no \0. In Java a String has a length which is part of its property, it is not terminated by a \0 as in C

3) the method that appends cannot be static as it access the array which is an instance variable
Was This Post Helpful? 0
  • +
  • -

#6 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: i cant understand what i return in methods

Posted 28 December 2012 - 10:19 AM

View Postpbl, on 28 December 2012 - 07:49 AM, said:

1) the array will always be full because the new one size is based on the one passed as parameter
array = new char[myArray.length];

2) this is not C so there is no \0. In Java a String has a length which is part of its property, it is not terminated by a \0 as in C

3) the method that appends cannot be static as it access the array which is an instance variable


The size array of class is 100.
this is the code of homework and i have to make the methods.
public class ArrayAppendable implements Appendable{
    char[] array= new char[100];
	
	
	
    Appendable append(char c) {
    	
    	return ;} 
	
   Appendable append(CharSequence csq){
		
		
		return ;}
}


This post has been edited by Vagosm: 28 December 2012 - 10:22 AM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i cant understand what i return in methods

Posted 28 December 2012 - 11:34 AM

Not really understand what you try to do
First you based you array size on an array passed as parameter now you hardcode it's value to 100.
You will need an index to hold how many char are in the array

Your second method can loop on the CharSequence length() and call the firt method passing to it the charAt() of each element

   for(int i = 0; i < csq.length(); ++i) 
      append(csq.charAt(i));



Your method declaration should be something different than Appendable unless you

    return this;


which seems completly useless to me
Was This Post Helpful? 0
  • +
  • -

#8 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: i cant understand what i return in methods

Posted 28 December 2012 - 11:34 AM

I try to make it its work but i think is false.
public class ArrayAppendable implements Appendable{
   
	
	private char[] array = new char[100];
	private int size;
	
	public int getsize(){return size;}
	
    public  ArrayAppendable (String str){
   		array=str.toCharArray(); 
	}
	
    public void ArrayAppendable(){
    	
    }

	
	
	public ArrayAppendable(char[] myArray){
		for(int i=0;i<myArray.length;i++)
		array[i]=myArray[i];
		size=myArray.length;
	}
	
	
	
     public Appendable append(char c)throws IOException{
    	
    	if(getsize()<100){
    		this.array[getsize()]=c;
    	}
    	
    	return this;}
	
	

	public Appendable append(CharSequence csq){
		String str = " ";
		if(getsize()<100){
			CharSequence seq = new String(str);
			char a[]=str.toCharArray(); 
			for(int i=0;i<a.length;i++)
			this.array[getsize()+i]=a[i];
    	}
		
		return this;}


Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i cant understand what i return in methods

Posted 28 December 2012 - 11:39 AM

You variable size is completly useless. It will always be == to array.size() so no need to stock that value.

You need something to tell you how many char are actually used in your array. Call it size if you want but it has nothing to do with the length of the array
Was This Post Helpful? 0
  • +
  • -

#10 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: i cant understand what i return in methods

Posted 28 December 2012 - 12:21 PM

and how i find how many characters have the array, i can't find samething how to realize this.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i cant understand what i return in methods

Posted 28 December 2012 - 01:01 PM

You initialize count at 0
You increment count by 1 each time you add a character inside the array
You can add a character inside the array until count == array.length
Was This Post Helpful? 1
  • +
  • -

#12 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: i cant understand what i return in methods

Posted 28 December 2012 - 02:12 PM

Thanks a lot for every one i make it and its work fine...
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: i cant understand what i return in methods

Posted 28 December 2012 - 03:01 PM

View Postpbl, on 28 December 2012 - 01:39 PM, said:

You variable size is completly useless. It will always be == to array.size() so no need to stock that value.


I would disagree. Buffers can have a max size. It is dreadfully inconvenient.

View Postpbl, on 28 December 2012 - 01:34 PM, said:

Your method declaration should be something different than Appendable unless you

return this;

which seems completly useless to me


Chaining calls? It's fun, really. Why are you hating on the java.util.Appendable?

Given the buffer limitation, I do something like this:
class ArrayAppendable implements java.lang.Appendable {
	public static final int MAX_BUFF = 100;
	private final char[] array;
	private int size;
	
	public ArrayAppendable() {
		this.array = new char[MAX_BUFF];
		this.size = 0;
	}

	public  ArrayAppendable (String str) throws IOException {
		this();
		append(str.subSequence(0, str.length()-1));
	}
	
	public Appendable append(CharSequence cs) throws IOException {
		for(int i=0; i<cs.length(); i++) { append(cs.charAt(i)); }
		return this;
	}

	public Appendable append(CharSequence cs, int begin, int end) throws IOException {
		return append(cs.subSequence(begin, end));
	}

	public Appendable append(char c) throws IOException {
		if(size==MAX_BUFF){ throw new IOException("Max Size error."); }
		this.array[this.size++] = c;
		return this;
	}
}



Granted, you'll want a getSize and a charAt to even be useful. The static size is unfortunate and I'd probably make it grow dynamically. Implementing Iterable would also be cool.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1