Question

Can somebody help me with Java programming? please be brief and explain the process and carefully...

Can somebody help me with Java programming? please be brief and explain the process

and carefully follow the step by step instruction. Thanks

Homework 12.1 - Write a program that generates five random sentences (like mad libs), prints them to a file, then reads in the sentences and prints them to the console.

  1. Start by creating four string arrays for nouns, verbs, colors, and places. They should store at least 5 of each type of word. You can have more words so that your sentences have more variety, but it would easier to keep the number of words the same in each array.
  2. Generate a random number to pick the index number in each array. There should be a different number per array, since you don't just want to end up with the same sentence every time the same number is selected.
  3. Plug your random strings into a sentence:
    The [adjective] [animal] wants to [verb] at the [place]
    An example of a randomly generated sentence might be:
    The fancy monkey wants to slide at the mall
    Feel free to make up your own sentence, as long as it uses at least the four different types of words.
  4. Write each sentence to a file called RandomStrings.txt
  5. Read in each sentence and print it to the console

Be sure to write some code to handle what will happen if the file already exists, or if it has trouble writing to or reading from the file. Your program should not crash if it encounters errors like this.

0 1
Add a comment Improve this question Transcribed image text
Answer #1
SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.



This is a very big question.
I have solved all of them. 
Please give me an upvote dear, Much Appreciated.!!


import java.io.*;
import java.util.Random;

public class Sentence {
    static void printFromFile(String fileName)
    {
        System.out.println("Reading data from the file..");
        // declare a reader
        BufferedReader reader;
        // Try to read the data from file.
        try {
            reader = new BufferedReader(new FileReader(fileName));//reader object for file
            String line = reader.readLine();//read next line
            while (line != null) {
                System.out.println(line);
                // read next line
                line = reader.readLine();//read next line
            }
            reader.close();
        } catch (Exception e) {
            // if any exception, just print the error.
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // declare the variables here
        // create four string arrays for nouns, verbs, colors, and places.
        // They should store at least 5 of each type of word
        String[] nouns={"Monkey", "Dog", "Donkey", "Horse", "Pig"};
        String[] verbs={"eat", "drink", "slide", "throw", "Visit"};
        String[] colors={"Red", "Green", "Blue", "Orange", "Pink"};
        String[] places={"Mall", "School", "Home", "College", "Office"};

        // Generate a random number to pick the index number in each array.
        Random random=new Random();
        // generate 5 different indices
            // prepare the sentence
            String sentence="The ";
            int randomIndex = random.nextInt(5);
            sentence+= colors[randomIndex];
            randomIndex = random.nextInt(5);
            sentence+= " " + nouns[randomIndex]+" wants to ";
            randomIndex = random.nextInt(5);
            sentence+= verbs[randomIndex];
            randomIndex = random.nextInt(5);
            sentence+=" at the "+places[randomIndex];

        // Here we have the sentence. Now print it to the file.
        // Edit the filepath here for your file
        String fileName="/home/praveen_kumar/hu16-java-leavetracker/dummy/src/RandomStrings.txt";
        printToFile(fileName, sentence);
        printFromFile(fileName);
    }

    static void printToFile(String fileName, String sentence) {
        try {
            // open the file here
            FileWriter myWriter = new FileWriter(fileName);
            // write to the file
            myWriter.write(sentence);
            myWriter.close();
            // print success message
            System.out.println("Successfully wrote to the file.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

=====

If there are any doubts, comment down here. We are right here to help you. Happy Learning..!! PLEASE give an UPVOTE I Have Pu

Add a comment
Know the answer?
Add Answer to:
Can somebody help me with Java programming? please be brief and explain the process and carefully...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a script that uses random number generation to create sentences. Use four arrays of strings...

    Write a script that uses random number generation to create sentences. Use four arrays of strings called article, noun, verb and preposition. Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is picked, concatenate it to the previous words in the sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and...

  • Can you please produce the Java code that solves this non-assessed homework problem? I'm very new...

    Can you please produce the Java code that solves this non-assessed homework problem? I'm very new to Java and Object-Oriented Programming and I have no idea how to construct the logic for this code. PROBLEM: "In this exercise, you will build a program that determines the validity of a select few simple English sentences, according to rules detailed below. A sentence will be represented as an array of words, which you will create as a static nested class `Word`. `Word`...

  • Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from...

    Python 3 Modify the sentence-generator program of Case Study so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt, verbs. txt, articles.txt, and prepositions.txt. (HINT: Define a single new function, getWords. This function should expect a filename as an argument. The function should open an input file with this name, define a temporary list, read words from the file, and add them to the list. The function should then convert the list...

  • Assignment #6 - Arrays and Strings - Making random sentences! Due: Tuesday April 2, 11:59:00 pm...

    Assignment #6 - Arrays and Strings - Making random sentences! Due: Tuesday April 2, 11:59:00 pm Objective This assignment will consist of writing a program that involves practice with arrays, random number generation, and Strings. You may use c-strings or string objects here, however, string objects is recommended. Exercise Filename: sentences.cpp Write a program that uses random-number generation to create sentences. Create four arrays of strings (string objects highly suggested over c-strings) called article, noun, verb, and preposition. The arrays...

  • Part B: Java Programming Problems. Choose 2 of the following problems. Each solution is worth 20...

    Part B: Java Programming Problems. Choose 2 of the following problems. Each solution is worth 20 marks. You are expected to use comments in your code. Marks will be deducted for incorrect brackets, syntax etc, so be careful as possible (40 marks). 1. Create a new program that: • Reads a sentence from a file; Writes the sentence to another file in reverse; and • Prints a message to the user in a JOptionPane message box when finished. 2. Write...

  • Write a method that has the following header: public static void printShuffled(String filename) The method reads...

    Write a method that has the following header: public static void printShuffled(String filename) The method reads a text file, filename, sentence by sentence into an array list, shuffles the sentences, and then prints out the shuffled contents. Assume sentences end with one of these characters: ".", ":", "!" and "?". Your method should create an array list for storing the sentences. The array list should be created with approximately the correct number of sentences, instead of being gradually expanded as...

  • please write a C++ code Problem A: Word Shadow Source file: shadow.cpp f or java, or.cj...

    please write a C++ code Problem A: Word Shadow Source file: shadow.cpp f or java, or.cj Input file: shadow.in in reality, when we read an English word we normally do not read every single letter of that word but rather the word's "shadow" recalls its pronunciation and meaning from our brain. The word's shadow consists of the same number of letters that compose the actual word with first and last letters (of the word) in their original positions while the...

  • PART B. The part of the exam is worth 5 Points - BRIEF Essay In the...

    PART B. The part of the exam is worth 5 Points - BRIEF Essay In the space below provide a brief essay on the following topic. Please follow the rules. TOPIC We are experiencing an unusual period in US GAAP reporting. More and more companies are reporting Negative Balances for TOTAL Equity. In class meetings we reviewed the Balance Sheets of McDonald's and American Airlines to see two examples of this. Negative equity is counter-intuitive. That means it is both...

  • please help me with this question. This is required to be written in Java. Objective The...

    please help me with this question. This is required to be written in Java. Objective The objective of this programming assignment is to design and implement in Java the Merge-Sort algorithm presented in the lecture to sort a list of numbers. We are interested in exploring the relationship between the time complexity and the "real time". For this exploration, you will collect the execution time T(n) as a function of n and plot the functions T(n)/log:(n). T(n)/n.log:(n), and T(n)/n2.loga(n) on...

  • Can you write with comments please. Thank you. Write a Java Program that asks the user...

    Can you write with comments please. Thank you. Write a Java Program that asks the user to enter the responses on the Console and write the responses to a text file called responses.txt (in the same directory as the program) Write another JAVA prorgram to read responses.txt file and calculate the frequency of each rating in the file and output the number and frequency in two columns (number, frequency) on the Console.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT