Click here to Skip to main content
16,021,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 files which are as follows:
ICharacterReader.java::
Java
import java.io.EOFException;

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

and another file:
SimpleCharacterReader.java::
Java
import java.io.EOFException;
import java.util.Random;

public class SimpleCharacterReader implements ICharacterReader {
  private int m_Pos = 0;

  public static final char lf = '\n';

  private String m_Content =
    "It was the best of times, it was the worst of times," +lf +
    "it was the age of wisdom, it was the age of foolishness,";

  Random m_Rnd = new Random();

  public char GetNextChar() throws EOFException {

    if (m_Pos >= m_Content.length()) {
        throw new EOFException();
    }

    return m_Content.charAt(m_Pos++);

  }

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

My task is as follows:
1.) Write a class that takes an ICharacterReader interface as an argument and returns a list of word frequencies ordered by word count and then alphabetically.

And also write a main method of a console application that exercises this class using a SimpleCharacterReader, and prints the output to the console.

For example, if the stream returns “It was the best of times, it was the worst of times” then the output will be:

it - 2;
of - 2;
the - 2;
times -2;
was - 2;
best - 1;
worst - 1;
2.) Test the answers in part 1, by writing unit test cases.

Could you help me with passing interface as an argument and what is meant by "writing unit test case"?
Posted
Comments
[no name] 19-Nov-14 19:21pm    
I don't think anyone here is keen on doing your work for you....but I'm sure everybody will help you if you stuck on a specific Problem.

1 solution

I wrote a proof of concept as a guide.

Interface:
C#
public interface IVehicle {
    int GetNumWheels();
}


Implementing class:
SQL
public class Car implements IVehicle{

    @Override
    public int GetNumWheels() {
        return 4;
    }

}


Main:
C#
public class ABC {
    public ABC(){}

    public int NumWheels(IVehicle v)
    {
        return v.GetNumWheels();
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ABC a=new ABC();
        Car c=new Car();
        String ans = String.format("%s has %d wheels", c.getClass().getName(), a.NumWheels(c));
        System.out.println(ans);
    }

}
 
Share this answer
 
v2
Comments
TorstenH. 20-Nov-14 5:14am    
+5! Nice help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900