8 Replies - 553 Views - Last Post: 10 March 2011 - 09:48 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Interfaces and test input class.

Posted 10 March 2011 - 07:08 PM

This is my assignment: Write the app that takes CharacterInterface (the interface returns a character from a stream) and outputs how many times a word has been read. You should use the TestReader class (that returns characters from a string) as a test input, and print the correct output in the console.

Here are the interface and the class that i have been provided with.

package demo;
import java.io.EOFException;

public interface CharacterInterface
{
	char GetNextChar() throws EOFException;
	void Dispose();
}






package demo;
import java.io.EOFException;
import java.util.Random;

public class TestReader implements CharacterInterface
{
	private int position = 0;

	public static final char lf = '\n';

	private String string = "It was good, it was bad," + 
	lf +
	"he was sweet, i was sweet," + 
	lf +
	"he was young i was young";

	Random random = new Random();

	public char GetNextChar() throws EOFException {

		if (position >= string.length()) {
			throw new EOFException();
		}

		return string.charAt(position++);
	}

	public void Dispose() {
		// do nothing
	}
}



The thing is that if i was NOT given the above classes i would have no problems solving the problem since its ver easy to do but i just do not understand the code my lecturer gave me. I am ver confused.

First lets talk about the interface: he says: "it returns a character from the stream" but is it not up to me how i implement the methods that appear in the interface? Anyway.. next thing.. He says that: "you should use TestReader as test input but he already has implemented these two interface methods in this class so am i supposed to use his class? (he says use it for testing so why did he implement this interface???). Moreover, he says write the app that takes the CharacterInterface etc etc - does he mean by "taking" that my class will implement this interface? If so why did he already define these classes in TestReader class.

I am sooo confused. I would have no problems with implementing the solution from scratch but my lecturer really made it confusing for me this time. Can anyone clear for me how on earth i am meant to use his classes?

Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Interfaces and test input class.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Interfaces and test input class.

Posted 10 March 2011 - 07:17 PM

It sounds like your instructor is providing you with sample code and telling you to play around with it. Just like classes can be types for variables, so can interfaces. So the following is completely legal:
CharacterInterface myChar = new TestReader();



What your instructor is asking you to do, it sounds like, is have a CharcaterInterface instance variable in your test class, which is initialized by a corresponding parameter in the constructor.
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Interfaces and test input class.

Posted 10 March 2011 - 07:24 PM

But what the point of having:

CharacterInterface myChar = new TestReader();



The whole point of interfaces is the whole "contract" idea. I have used interfaces on many occasions before but never the way you suggested. Im confused.
Was This Post Helpful? 0
  • +
  • -

#4 MaverickDavidian  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 67
  • Joined: 15-April 09

Re: Interfaces and test input class.

Posted 10 March 2011 - 07:36 PM

View Postdarek9576, on 10 March 2011 - 06:24 PM, said:

But what the point of having:

CharacterInterface myChar = new TestReader();



The whole point of interfaces is the whole "contract" idea. I have used interfaces on many occasions before but never the way you suggested. Im confused.


Let's say, for example, you had an ArrayList<CharacterInterface>. That way, you could include a number of (more or less subtly) different classes that implement CharacterInterface in this ArrayList.

Perhaps you had an interface Color. Classes implementing this interface could include, say, Red, Blue, Green, etc. In another class, you could declare an instance Color favoriteColor. That way, favoriteColor could be an instance of class Red, Blue, Green, or whatever. Interfaces are good for organizing classes that don't share a superclass.
Was This Post Helpful? 1
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Interfaces and test input class.

Posted 10 March 2011 - 07:42 PM

So should i put my code in the TestReader class or create my own class to do it and then access the string variable (although he did not provide a getter for it).
Was This Post Helpful? 0
  • +
  • -

#6 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: Interfaces and test input class.

Posted 10 March 2011 - 09:01 PM

View Postdarek9576, on 10 March 2011 - 09:24 PM, said:

But what the point of having:

CharacterInterface myChar = new TestReader();



The whole point of interfaces is the whole "contract" idea. I have used interfaces on many occasions before but never the way you suggested. Im confused.

CharacterInterface implements TestReader so what is the problem ?
Perfectly legal as macosxnerd101 mentionned
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Interfaces and test input class.

Posted 10 March 2011 - 09:29 PM

You will want to use a separate class. And your instructor did provide an accessor method: the GetNextChar() method. You'll have to rebuild the String char by char.
Was This Post Helpful? 1
  • +
  • -

#8 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Interfaces and test input class.

Posted 10 March 2011 - 09:43 PM

View Postmacosxnerd101, on 10 March 2011 - 09:29 PM, said:

You will want to use a separate class. And your instructor did provide an accessor method: the GetNextChar() method. You'll have to rebuild the String char by char.


Ok. that is true since this method refers to an instance variable. Sweet. That does make perfect sense to me. Thx for getting me started. I had no idea how to tackle this one :).
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Interfaces and test input class.

Posted 10 March 2011 - 09:48 PM

japanir also has a good tutorial on Abstract Classes vs. Interfaces that you may also find helpful!

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1