Question

In: Computer Science

Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...

Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time as the quiz will close after 40 minutes and I will not accept email submissions. It is better to paste periodically into the quiz so you turn in something.

Solutions

Expert Solution

I am using Scanner class to take the said .txt file as a input from a predefined path.

Kindly refer below java code snippet:

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;

public class PoemDriver {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("PoemInfo.txt"));
        input.useDelimiter("-|\n");

        Product[] products = new Product[0];

        //read the .txt file line by line and convert to JAVA objects
        while(input.hasNext()) {
            int id = input.nextInt();
            String department = input.next();
            String name = input.next();
            double price = Double.valueOf(input.next().substring(1));
            int stock = input.nextInt();

            Product newProduct = new Product(name, price, department, id, stock);
            products = addProduct(products, newProduct);
        }

        for (Product product : products) {
            System.out.println(product);
        }
    }

    private static Product[] addProduct(Product[] products, Product productToAdd) {
        Product[] newProducts = new Product[products.length + 1];
        System.arraycopy(products, 0, newProducts, 0, products.length);
        newProducts[newProducts.length - 1] = productToAdd;

        return newProducts;
    }

    //definition of Product class, you can take anything
    public static class Product {
        protected String name;
        protected double price;
        protected String department;
        protected int id;
        protected int stock;

        private static NumberFormat formatter = new DecimalFormat("#0.00");

        constructor
        public Product(String n, double p, String d, int i, int s) {
            name = n;
            price = p;
            department = d;
            id = i;
            stock = s;
        }

        @Override
        public String toString() {
            return String.format("ID: %d\r\nDepartment: %s\r\nName: %s\r\nPrice: %s\r\nStock: %d\r\n",
                    id, department, name, formatter.format(price), stock);
        }
    }
}

Related Solutions

Lab Text Manipulation Inside the main method, do the following: Create an ArrayList of strings and...
Lab Text Manipulation Inside the main method, do the following: Create an ArrayList of strings and call it parks. Read in the names of national parks from the user until the user enters done(or DONE, or dOnE, .. ) Keep in mind, that the names of some national parks consist of more than one word, for example, Mesa Verde. As you read in the national parks, add them to the list. Next, we are going to build a string based...
Develop the getSuggestions(ArrayList wordCountList) method as the base method of the class. Develop the countWords(ArrayList wordList)...
Develop the getSuggestions(ArrayList wordCountList) method as the base method of the class. Develop the countWords(ArrayList wordList) method to find the frequencies of all words in the text file. Develop the getWordList(File[] fileArray) method to get all words in the text file. Ignore the words that have 3 or less characters. Your customer wants you to develop a method that will find the sentences that contain a specific word. This is basically a word search, but your customer needs the list...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the...
Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) 2. Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points) 3. You have been given the job of creating a new order processing system for the Yummy Fruit CompanyTM. The system reads pricing information for...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT