lundi 19 octobre 2015

create an adjecive story program using java

So, I have an asignment from my teacher, the prorgam should be able to take 2 text files with a story and one with the adjetives and create a story out of it, like those stories we used to play widt when we were children. Anyway here is the code my teacher gave me

update: I didn't ask a question, sorry about that, I don't expect anyone to just give me the finished code, but my question or questions are, in the storycreator class, there is some empty methods that I need to fill in,the storycreator class, now those methods has strings in it, how do I import tet file and make a string out of it, and lastly, I don't see any public main method, is that even possible or should I create one?

Storycreator.class

import java.util.Random;
import java.io.*;
/**
 * Class description
 */
public class StoryCreator
{
    private InputReader reader;
    private OutputWriter writer;
    private Random random;

    public StoryCreator()
    {
        reader = new InputReader();
        writer = new OutputWriter();
        random = new Random();
    }

    public void createAdjectiveStory(String storyFilename, String adjectivesFilename, String outputFilename)
    {

    }

    public void createAdjectiveStory(String storyFilename, String outputFilename)
    {

    }


    public void createAdjectiveStoryFromDictionary(String storyFilename, String dictionaryFilename, String outputFilename)
    {

    }

}

InputReader.class

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Class for reading input from file. You may want to expand it, if needed...
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class InputReader
{

    /**
     * Constructor for objects of class InputReader
     */
    public InputReader()
    {
    }

    /**
     * Return all the words in a file.
     * 
     * @param    filename the name of the file
     * @return   an arraylist of all the words in the file
     */
    public ArrayList<String> getWordsInFile(String filename)
    {
        ArrayList<String> words = new ArrayList<>();

        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filename),
                        "8859_1"));
            String line = in.readLine();
            while(line != null) {
                String [] elements = line.split(" ");
                for(int i = 0 ; i < elements.length ; i++){
                    words.add(elements[i]);
                }
                line = in.readLine();
            }
            in.close();
        }
        catch(IOException exc) {
            System.out.println("Error reading words in file: " + exc);
        }
        return words;
    }

}

OutputWriter.class

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Class for writing information to a file.
 * 
 * @author (Per Lauvås) 
 * @version (1.0)
 */
public class OutputWriter
{

    /**
     * Constructor for objects of class OutputWriter
     */
    public OutputWriter()
    {
    }

    /**
     * Writes a list of words to a file. The words are separated by the 'space' character.
     * 
     * @param  output   the list of words
     * @param  filename   the name of the output file
     */
    public void write(ArrayList<String> output, String filename)
    {
        try {
            FileWriter out = new FileWriter(filename);
            for(String word : output) {
                out.write(word + " ");
            }
            out.close();
        }
        catch(IOException exc) {
            System.out.println("Error writing output file: " + exc);
        }

    }
}




Aucun commentaire:

Enregistrer un commentaire