Please help! Create a Java class called Headlines, with the following elements:
ID: 3634966 • Letter: P
Question
Please help!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 bags, in the following order: people, actions, settings: in other words, randomly choose one people string, then randomly choose one action string, then randomly choose one settings string, and print them all out as one string.
? A 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.
**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”);**
Thank you!!!!
Explanation / Answer
The Java programming language is a strongly typed language, which means that every variable and every expression has a type that is known at compile time. Types limit the values that a variable (§4.12) can hold or that an expression can produce, limit the operations supported on those values, and determine the meaning of the operations. Strong typing helps detect errors at compile time. The types of the Java programming language are divided into two categories: primitive types and reference types. The primitive types (§4.2) are the boolean type and the numeric types. The numeric types are the integral types byte, short, int, long, and char, and the floating-point types float and double. The reference types (§4.3) are class types, interface types, and array types. There is also a special null type. An object (§4.3.1) is a dynamically created instance of a class type or a dynamically created array. The values of a reference type are references to objects. All objects, including arrays, support the methods of class Object (§4.3.2). String literals are represented by String objects (§4.3.3). Types exist at compile-time. Some types correspond to classes and interfaces, which exist at run-time. The correspondence between types and classes or interfaces is incomplete for two reasons: At run-time, classes and interfaces are loaded by the Java virtual machine using class loaders. Each class loader defines its own set of classes and interfaces. As a result, it is possible for two loaders to load an identical class or interface definition but produce distinct classes or interfaces at run-time. Type arguments and type variables (§4.4) are not reified at run-time. As a result, different parameterized types (§4.5) are implemented by the same class or interface at run time. Indeed, all invocations of a given generic type declaration (§8.1.2, §9.1.2 )share a single run-time implementation. A consequence of (1) is that code that compiled correctly may fail at link time if the class loaders that load it are inconsistent. See the paper Dynamic Class Loading in the Java Virtual Machine, by Sheng Liang and Gilad Bracha, in Proceedings of OOPSLA '98, published as ACM SIGPLAN Notices, Volume 33, Number 10, October 1998, pages 36-44, and The Java Virtual Machine Specification, Second Edition for more details. A consequence of (2) is the possibility of heap pollution (§4.12.2.1). Under certain conditions, it is possible that a variable of a parameterized type refers to an object that is not of that parameterized type. The variable will always refer to an object that is an instance of a class that implements the parameterized type. See (§4.12.2) for further discussion.