-1

I have a small java program that collects 10 words written by a user and prints them in specified orders. As it stands, the program works, but it is not cohesive.

My issue stems from not knowing enough about the concept of cohesion to work on fixing this, as well as being new to Java/OO languages.

I believe that the class Entry is way way way too cluttered, and that another class should take on some of this class' functions.

Any hint or clue, cryptic or otherwise would be greatly appreciated!

The lack of a input reader in Dialogue.java is intentional, as the original code uses proprietary code.

These are the three classes: entry, dialogue and printer.

Entry.java

public class Entry {

    public static void main(String[] args){
        String[] wordArray = new String[10];
        Dialogue d = new Dialogue(); 
        wordArray = d.read(wordArray); 
        Printer p = new Printer(); 
        p.printForwards(wordArray); 
        p.printBackwards(wordArray); 
        p.printEveryOther(wordArray); 

    }

}

Dialogue.java

public class Dialogue {

    public String[] read(String[] s){ 
            String[] temp; 
            temp = new String[s.length]; 

            for(int i=0;i<s.length;i++){
                String str = anything that reads input("Enter word number" + " " + (i+1));
            temp[i] = str;     
            } 

        return temp; 
    }

}

Printer.java

public class Printer {

    public void printForwards(String[] s){
        System.out.println("Forwards:");
        for(int i=0;i<s.length;i++){
            System.out.print(s[i] + " ");

            if(i==s.length-1){
                System.out.println("");
            }
        }
    }

    public void printBackwards(String[] s){
        System.out.println("Backwards:");
        for(int i=s.length-1;i>=0;i--){
            System.out.print(s[i]+ " ");

            if(i==0){
                System.out.println("");
            }
        }
    }

    public void printEveryOther(String[] s){
        System.out.println("Every other:");
        for(int i = 0; i < s.length; i++){
            if(i % 2 == 0){
                System.out.print(s[i] + " ");
            }

        }

    }
}// /class
mkobit
  • 43,979
  • 12
  • 156
  • 150
Kyrre
  • 670
  • 1
  • 5
  • 19
  • 2
    If your program works, and you want it reviewed, go to [CodeReview](http://codereview.stackexchange.com) – Vince Oct 22 '15 at 23:24
  • @VinceEmigh Thanks, I will! – Kyrre Oct 22 '15 at 23:27
  • This confuses me: `"I believe that the class Entry is way way way too cluttered, and that another class should take on some of this class' functions."` -- your Entry class isn't a class that I would worry about, since all it is is a container for your main method. There is nothing to OOP-ify in this class, and your efforts should probably lie elsewhere. – Hovercraft Full Of Eels Oct 22 '15 at 23:27

1 Answers1

1

It looks okay overall, the truth is it is a very simple task where as OOP is better suited for more complex programs. That being said, here are a few pointers/examples.

You can also do your printing more OOP style. The purpose of this is build reusable, modular code. We do this by abstracting String array manipulations (which previously existed in the Printer class) to it's own class.

This is also very similar/also known as loose-coupling. We achieve loose-coupling by splitting the string processing functionality and the printing functionality.

Change you Printer class to StringOrderer or something along those lines:

public class StringOrderer {
    private String[] array;

    public class StringOrderer(String[] array) {
         this.array = array;
    }

    public String[] getArray() {
        return array;
    }

    public String[] everyOther(){
        String[] eos = new String[array.length];
        for(int i = 0; i < s.length; i++){
            if(i % 2 == 0){
                eos[eos.length] = s[i];
        }
        return eos;

    }

    public String[] backwards() {
    ...

And then in your main class add a method like such:

private static void printStringArray(String[] array) {
     for (int i=0; i<array.length; i++) {
         System.out.print(array[i]);
     }
}

Then call it in your main method:

StringOrderer s = new StringOrderer(wordArray);
System.out.println('Forward:');
printStringArray(s.getArray());
System.out.println('Every other:');
printStringArray(s.everyOther());
System.out.println('Backwards:');
... 

Extra tip - You can also add methods in your main class like so:

public class Entry {

    public static void main(String[] args){
        String[] wordArray = readWordArray()
        Printer p = new Printer(); 
        p.printForwards(wordArray); 
        p.printBackwards(wordArray); 
        p.printEveryOther(wordArray); 

    }


    private static String[] readWordArray() {
        Dialogue d = new Dialogue(); 
        return d.read(new String[10]); 
   }
}

to make it more readable.

Zakk L.
  • 208
  • 2
  • 5