please answer following question in Java: Program #4 (25 points) Create a Java c
ID: 3822765 • Letter: P
Question
please answer following question in Java:
Program #4 (25 points)
Create a Java class called Headlines, with the following elements:
Three instance variables that are arrays of type String. Each array should hold at least 12 Strings. The three arrays will hold data read from three different files, described below.
A method that opens three input files (named settings.txt, actions.txt, and people.txt – see attachments) and reads their data into the 3 arrays (put actions in the action bag, etc.)
A method that generates and prints out “headlines” consisting of strings pulled randomly from each of the arrays, in the following order: people (for example : Donald Trump, actions: for example: Eats1000 Hot dogs, settings; For example: on National television.
in other words, randomly choose one people string, then one action string, then one settings string, and print them all out as one string. Each string should be chosen independently: generate a new random index to pull a string from each arry.
The main method that creates a Headlines object prints out a headline then prompts the user to see whether or not to continue; the program should continue to run as long as the user chooses to see more, and terminates when the user chooses to quit.
Extra credit options (15 points possible)
• Generate your own input files; this option will be graded on a purely subjective basis, the criteria being how much the text amuses your instructor (up to 5 points)
• Keep track of which phrases in each file have been used, and don’t re-use them until all phrases have been used (up to 5 points)
• Allow the user to save any headlines s/he likes in a separate output file (up to 5 points)
Quick refresher on file I/O:
1. import java.util.*; // gives access to class Scanner import java.io.*; // gives access to class FileInputStream
2. declare a FileInputStream object to represent an input file; for example, FileInputStream infile;
3. Associate the file with an actual file using the constructor; for example, infile = new FileInputStream(“e:\people.txt”); (Note: this can throw a FileNotFoundException)
4. Attach the file to a Scanner object; example Scanner f = new Scanner (infile);
5. You can now read from the file using the same input methods you use with a keyboard Scanner object; when finished reading from a file, close it using the close() method, for example: f.close();
6. Once the file is closed, you can set your FileInputStream object to refer to a different file, and associate the Scanner with that object.
7. As an alternative to steps 2 – 4, you can combine the creation of the Scanner object with the creation of an anonymous FileInputStream object, for example: Scanner f = new Scanner(new FileInputStream(“e:\people.txt”);
Note: Each of the input files contains 12 words or phrases to be read; the last line of each file is a solitary 0 You can use for loops to populate the arrays based on this information, or you can use while loops with (!file.hasNextInt() as end condition).
Explanation / Answer
HeadLines.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class HeadLines {
// To store headlines
static List<String> headLines = new ArrayList<>();
public static void main(String[] args) {
List<String> settings;
List<String> actions;
List<String> people;
File settingsFile = new File("D:\headlines\actions.txt");
File actionsFile = new File("D:\headlines\settings.txt");
File peopleFile = new File("D:\headlines\people.txt");
Scanner scanner = new Scanner(System.in);
settings = inputFromFile(settingsFile);
actions = inputFromFile(actionsFile);
people = inputFromFile(peopleFile);
System.out.println("******HeadLines*********");
printHeadLines(settings, actions, people);
System.out.println("Do you eant to see more?(Yes/No)");
while (scanner.hasNext()) {
String test=scanner.next();
if (test.equalsIgnoreCase("Yes")) {
printHeadLines(settings, actions, people);
} else if (test.equalsIgnoreCase("No")) {
System.out.println("Done");
scanner.close();
System.exit(1);
}else{
System.out.println("Please Enter value Yes or No");
}
System.out.println("Do you eant to see more?(Yes/No)");
}
}
public static void printHeadLines(List<String> settings, List<String> actions, List<String> people) {
int a = (int) (Math.random() * settings.size());
int b = (int) (Math.random() * actions.size());
int c = (int) (Math.random() * people.size());
String headLine=settings.get(a) + " " + actions.get(b) + " " + people.get(c)+" ";
System.out.println(headLine);
headLines.add(headLine);//headlines stored can be stored into any output file
}
/*
* Method to read words from the file and set to the list
*/
public static List<String> inputFromFile(File myFile) {
List<String> words = new ArrayList<String>();
Scanner in;
try {
// read the details from the given file
in = new Scanner(myFile);
while (in.hasNext()) {
words.add(in.nextLine());
}
in.close();
} catch (FileNotFoundException e) {
System.out.println("Input file not found : " + myFile);
System.exit(1);
}
// Read from input file and get the list of words
return words;
}
}
Sample output:
******HeadLines*********
Action 12 Settings 10 People 11
Do you eant to see more?(Yes/No)
yes
Action 4 Settings 12 People 4
Do you eant to see more?(Yes/No)
yes
Action 1 Settings 2 People 6
Do you eant to see more?(Yes/No)
yes
Action 6 Settings 9 People 7
Do you eant to see more?(Yes/No)
no
Done
Sample files used:
actions.txt
Action 1
Action 2
Action 3
Action 4
Action 5
Action 6
Action 7
Action 8
Action 9
Acton 10
Action 11
Action 12
settings.txt
Settings 1
Settings 2
Settings 3
Settings 4
Settings 5
Settings 6
Settings 7
Settings 8
Settings 9
Settings 10
Settings 11
Settings 12
people.txt
People 1
People 2
People 3
People 4
People 5
People 6
People 7
People 8
People 9
People 10
People 11
People 12
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.