Question
hey, this is a filei/o homework. um please show me how to do this (im using ONLY arraylist for the first part so please continue on that) i have put my work please fix some mistakes and continue on it. the file is named “tools.txt” its a text document file that i savedin the netbeansprojects file. um pleas euse netbeans and show me the output afterwards, make it simple and continue on what i have worked on whilw fixing some erros. ( please make it so good that when i put it in netbeans it runs beautifuly) and answer all. THANK U
works Price Mat ARC Date in ID Name Rubber Aluminum 14 9 13 true 920.0 05/02/1958 19/05/1965 12/01/1921 04/01/1971 17/02/1975
Do the following steps: 1. Download the data file: lab02b-tools.txt 2. Write a program with a while loop and switch structu
12 public class Lab02 ( 13E public static void main (String [] args) Arraylist<String> name Arraylist<Integer> id Arraylist<I
works Price Mat ARC Date in ID Name Rubber Aluminum 14 9 13 true 920.0 05/02/1958 19/05/1965 12/01/1921 04/01/1971 17/02/1975 knife 7 10 9 true 4 680.0 handsaw 593 true 6 14 12 320.0 Iron steel wool Pump wood screw plane 452 8 true 4 6 450.0 Paper Paper Aluminum Gold 22 true 1 2 960.0 831 true 3 14 01/07/1914 910.0 585 12 14 12 true 22/09/1917 02/07/2007 10/10/1929 20/03/1934 17/04/1931 01/02/1990 11/03/1921 13/06/1962 15/10/1952 14/11/1998 03/10/1978 06/06/1929 26/11/1984 15/09/1969 23/04/1949 25/05/1922 260.0 894 screw true Plastic Gold 2 5 14 370.0 socket wrench 846 12 12 0 true pipe cutter wire stripper 240.0 559 0 12 13 true 800.0 Rubber 319 12 13 14 bolt 330.0 Rubber true 630 false vise 600.0 2 5 2 711 Stone Leather masonry bit paint roller circular saw 390.0 .1 7 3 true 817 990.0 3 12 15 9 9 Rubber Glass Fabric true 757 870.0 985 true Ruler 0.0 520 15 6 13 true level screwdriver bits 655 270.0 Rubber Fabric 7 2 12 true 140.0 310.0 1000.0 500 13 2 true Magnet 491 Steel false 14 10 4 Plastic フフ12 spanner Allen key 532 true 234 800.0 500.e 630.0 290.0 Steel Plastic 1 10 true T2 12 2 true 768 ax Wheel 386 08/11/1942 Rubber 8 5 11 true Bucket mallet 406 24/09/1944 26/01/1942 22/05/1942 01/05/1982 08/08/2011 25/05/1926 Fabric 7 10 13 true 772 30.0 Nood 10 12 13 true socket drill bit needle-nose pliers 515 770.0 Wood e 11 15 8 13 2 3 true 2 11 true true 26 540.0 580.0 Leather Rubber Plastic 2 13 true 420 hammer 159 738 400.0 2 glue gun 5ocket wrench level pipe cutter niter block ww.needle-nose pliers 17/03/1934 350.0 520.0 Gold Ceramic 6 14 12 true 55 3/11/1977 26/12/1942 270.0 Paper 28/10/1965 23/10/1932 23/01/1975 26/02/1938 19/08/1959 20/05/1905 05/08/ 197 9 10 4 true 114 9 3 12 true 378 172 90.0 Brass Plastic Wood Fabric Gold false e 10 16 320.0 e90.0 e10.0 10 11 4 true false 588 6 13 14 15 10 orace needle-nose piiers 632 445 true 660.0 650.0 200.0 13 2 2 false fretsa 210 Stone 15 7 tnue Sweepen phillips screwdeiven 790 Stone
Do the following steps: 1. Download the data file: "lab02b-tools.txt" 2. Write a program with a while loop and switch structure and a menu with these tasks/options: a. Load file: reads the data file one line at a time and saves the respective values into appropriate arraylists (each column in a separate arraylist) or you could use parallel arrays or one 2D Object array b. Find number of tools. c. Find number of records for the same tool name. e.g How many Knives? d. Find all tools that> a price e. Find all tools that
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below with appropriate comments.

............................................................CODE STARTS HERE.................................................................

import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;

public class Lab02
{
    private static ArrayList<String> name = new ArrayList<>();
   private static ArrayList<Integer> id = new ArrayList<>();
   private static ArrayList<Integer[]> date = new ArrayList<>();
   private static ArrayList<Float> price = new ArrayList<>();
   private static ArrayList<String> material = new ArrayList<>();
   private static ArrayList<Integer> shelf = new ArrayList<>();
   private static ArrayList<Integer> row = new ArrayList<>();
   private static ArrayList<Integer> col = new ArrayList<>();
   private static ArrayList<Boolean> works = new ArrayList<>();
   private static FileWriter writer; // file writer used for writing to output file
   private static boolean isScreenOutput = true; // if output is printed to screen
   private static boolean isFileOutput = true; // if output is saved to file
  
   private static boolean load(String filename) // Object loader
   {
        File f1 = new File(filename); // Input file tools.txt
       try
       {
            Scanner fin = new Scanner(f1);
            fin.nextLine();
            fin.nextLine();
          
            while(fin.hasNext())
            {
                name.add(fin.next());
                id.add(fin.nextInt());
                String[] d = fin.next().split("/");
                Integer[] d1 = new Integer[3];
                d1[0] = Integer.parseInt(d[0]);
                d1[1] = Integer.parseInt(d[1]);
                d1[2] = Integer.parseInt(d[2]);
                date.add(d1);
                price.add(fin.nextFloat());
                material.add(fin.next());
                shelf.add(fin.nextInt());
                row.add(fin.nextInt());
                col.add(fin.nextInt());
                works.add(fin.nextBoolean());
            }
          
            fin.close();
            return true;
        }
        catch (FileNotFoundException e) {
            System.out.println("File not found");
            return false;
        }
   }
  
   private static int numTools()
   {
        Set<Integer> uniqueId = new HashSet<>();
        uniqueId.addAll(id); // Add all to a set, so duplicates are removed
        return uniqueId.size(); // Size of the set is the number of unique tools
   }
  
   private static int numRecords(String tool)
   {
        return Collections.frequency(name, tool); // In build function which returns the number of occurrences of a keyword
   }
  
   private static ArrayList<String> priceGreaterThan(float priceGiven)
   {
        ArrayList<String> newTools = new ArrayList<>();
        for(int i=0 ; i<price.size() ; i++) // Iterate over all rows
        {
            if (price.get(i) > priceGiven) // Check if price greater
            {
                newTools.add(name.get(i));
            }
        }
        return newTools;
   }
  
   private static ArrayList<String> priceLessThan(float priceGiven)
   {
        ArrayList<String> newTools = new ArrayList<>();
        for(int i=0 ; i<price.size() ; i++)
        {
            if (price.get(i) < priceGiven) // Check if price lesser
            {
                newTools.add(name.get(i));
            }
        }
        return newTools;
   }
  
   private static ArrayList<String> dateNewerThan(int year)
   {
        ArrayList<String> newTools = new ArrayList<>();
        for(int i=0 ; i<date.size() ; i++)
        {
            if (date.get(i)[2] > year) // Check if date newer
            {
                newTools.add(name.get(i));
            }
        }
        return newTools;
   }
  
   private static ArrayList<String> dateOlderThan(int year)
   {
        ArrayList<String> newTools = new ArrayList<>();
        for(int i=0 ; i<date.size() ; i++)
        {
            if (date.get(i)[2] < year) // Check if date older
            {
                newTools.add(name.get(i));
            }
        }
        return newTools;
   }
  
   private static ArrayList<String> dateBetween(int year1, int year2)
   {
        ArrayList<String> newTools = new ArrayList<>();
        for(int i=0 ; i<date.size() ; i++)
        {
            if (year1 < date.get(i)[2] && date.get(i)[2] < year2) // Check if date between
            {
                newTools.add(name.get(i));
            }
        }
        return newTools;
   }
  
   private static int age(int idGiven)
   {
        int pos = id.indexOf(idGiven);
        int d = date.get(pos)[2];
        int curYear = Calendar.getInstance().get(Calendar.YEAR); // Current year
        return curYear-d; // Age is 'current year - year'
   }
  
   private static ArrayList<String> onShelf(int shelfGiven)
   {
        ArrayList<String> newTools = new ArrayList<>();
        for(int i=0 ; i<shelf.size() ; i++)
        {
             if (shelf.get(i) == shelfGiven) // On same shelf
             {
                 newTools.add(name.get(i));
             }
        }
        return newTools;
   }
  
   private static ArrayList<String> onRow(int rowGiven)
   {
        ArrayList<String> newTools = new ArrayList<>();
        for(int i=0 ; i<row.size() ; i++)
        {
             if (row.get(i) == rowGiven) // In same row
             {
                 newTools.add(name.get(i));
             }
        }
        return newTools;
   }
  
   private static ArrayList<String> onColumn(int colGiven)
   {
        ArrayList<String> newTools = new ArrayList<>();
        for(int i=0 ; i<col.size() ; i++)
        {
             if (col.get(i) == colGiven) // In same column
             {
                 newTools.add(name.get(i));
             }
        }
        return newTools;
   }
  
   private static ArrayList<String> working()
   {
        ArrayList<String> newTools = new ArrayList<>();
        for(int i=0 ; i<works.size() ; i++)
        {
             if (works.get(i) == true) // Working!
             {
                 newTools.add(name.get(i));
             }
        }
        return newTools;
   }
  
   private static String tool(int idGiven)
   {
        int pos = id.indexOf(idGiven); // Gets the index of an element in the ArrayList
        return name.get(pos);
   }
  
   private static void print(String printString)
   {
        if (isScreenOutput) // If screen printing
        {
            System.out.println(printString); // print the string to console
        }
      
        if (isFileOutput) // If file printing
        {
            try
            {
                writer.write(printString); // write the string to the file
                writer.write("\n"); // Manually append a new line
            }
            catch(Exception e)
            {
                System.out.println("writer error");
            }
        }
   }
  
   public static void main(String[] args) {
        String opfile = "testop.txt";
      
       Scanner reader = new Scanner(System.in);
       int op;
       System.out.println("Enter command: ");
       op = reader.nextInt();
       try
       {
            writer = new FileWriter(opfile);
           while(op != -1) // Exit when the user inputs -1.
           {
                // Command is int value of the character in question. Eg. a->1, b->2, etc.
                switch(op)
                {
                    case 1:
                        load("tools.txt");
                        break;
                    case 2:
                        print("Total number of tools is " + numTools());
                        break;
                    case 3:
                        String toolName;
                        System.out.println("Enter tool name: ");
                        toolName = reader.next();
                        print("Number of records for " + toolName + " is " + numRecords(toolName));
                        break;
                    case 4:
                        float p;
                        System.out.println("Enter a price: ");
                        p = reader.nextFloat();
                        print("Tools > price: " + priceGreaterThan(p));
                        break;
                    case 5:
                        System.out.println("Enter a price: ");
                        p = reader.nextFloat();
                        print("Tools < price: " + priceLessThan(p));
                        break;
                    case 6:
                        int y;
                        System.out.println("Enter a date: ");
                        y = reader.nextInt();
                        print("Newer tools: " + dateNewerThan(y));
                        break;
                    case 7:
                        System.out.println("Enter a date: ");
                        y = reader.nextInt();
                        print("Older tools: " + dateOlderThan(y));
                        break;
                    case 8:
                        int y1, y2;
                        System.out.println("Enter a date range (y1 y2): ");
                        y1 = reader.nextInt(); y2 = reader.nextInt();
                        print("Tools between: " + dateBetween(y1, y2));
                        break;
                    case 9:
                        int idee;
                        System.out.println("Enter the id: ");
                        idee = reader.nextInt();
                        print("Age of the tool is " + age(idee));
                        break;
                    case 10:
                        int sf;
                        System.out.println("Enter shelf: ");
                        sf = reader.nextInt();
                        print("Tools on shelf: " + onShelf(sf));
                        break;
                    case 11:
                        int r;
                        System.out.println("Enter row: ");
                        r = reader.nextInt();
                        print("Tools in row: " + onRow(r));
                        break;
                    case 12:
                        int c;
                        System.out.println("Enter column: ");
                        c = reader.nextInt();
                        print("Tools in column: " + onColumn(c));
                        break;
                    case 13:
                        print("Working tools: " + working());
                        break;
                    case 14:
                        System.out.println("Enter the id: ");
                        idee = reader.nextInt();
                        print("Tool is " + tool(idee));
                        break;
                    case 15:
                        System.out.println("Enter op filename: ");
                        opfile = reader.next();
                        writer.close();
                        writer = new FileWriter(opfile);
                    case 16:
                        System.out.println("Enter 1 for true, 0 for false: ");
                        if (reader.nextInt() == 1)
                        {
                            isFileOutput = true;
                        }
                        else
                        {
                            isFileOutput = false;
                        }
                        break;
                    case 17:
                        System.out.println("Enter 1 for true, 0 for false: ");
                        if (reader.nextInt() == 1)
                        {
                            isScreenOutput = true;
                        }
                        else
                        {
                            isScreenOutput = false;
                        }
                        break;
                    default:
                        System.out.println("Enter correct command");
                        break;
                }
                System.out.println("Enter command: ");
                op = reader.nextInt();
           }
           writer.close(); // Always close the writer
       }
       catch(Exception e)
        {
            System.out.println(e);
        }
   }
}

............................................................................CODE ENDS HERE............................................................................

Sample output for a small file tools.txt I created just for demonstration:

tools.txt

Name id
...
knife 7 05/02/1958 920.0 Rubber 14 9 13 true
handsaw 593 19/05/1965 680.0 Aluminium 10 4 9 false

Command line output:

Enter command:
1
Enter command:
4
Enter a price:
60.5
Tools > price: [knife, handsaw]
Enter command:
4
Enter a price:
700.6
Tools > price: [knife]
Enter command:
13
Working tools: [knife]
Enter command:
-1

File output:

Tools > price: [knife, handsaw]
Tools > price: [knife]
Working tools: [knife]

..................................................................................................................................................................................

Feel free to ask any doubt or issue in the code and I'll be happy to help!

Add a comment
Know the answer?
Add Answer to:
hey, this is a filei/o homework. um please show me how to do this (im using...
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
  • Need help writing a program that meets pseudocode and criteria . Txt File below input.txt file...

    Need help writing a program that meets pseudocode and criteria . Txt File below input.txt file data 05 11/30/16 03 12/07/16 05 12/07/16 05 12/08/16 01 12/10/16 07 12/11/16 07 12/14/16 06 12/15/16 02 12/21/16 05 12/21/16 06 12/22/16 07 12/22/16 08 12/23/16 07 12/23/16 07 12/23/16 07 12/23/16 08 12/24/16 08 12/24/16 07 12/24/16 03 12/26/16 05 12/26/16 07 12/28/16 04 12/29/16 07 01/01/17 06 01/03/17 07 01/03/17 08 01/05/17 05 01/10/17 04 01/17/17 08 01/17/17 07 01/18/17 07...

  • Nonlinear. Here we want you to verify this property by computing the output of S, for the followi...

    nonlinear. Here we want you to verify this property by computing the output of S, for the following two pairs of inputs. The S-box S is given as follows: 0-6. (10 points) One important property which makes DES secure is that the S-Boxes are S-box S S0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 14 04 13 01 02 15 11 08 03 10 06 12 05 09 00 07 1...

  • Describe the type(s) of relationship(s) between EMPLOYEE and JOB. MP-104 103 18 101 13 103 15...

    Describe the type(s) of relationship(s) between EMPLOYEE and JOB. MP-104 103 18 101 13 103 15 101 05 02 04 01 14 11 14 12 104 103 05 08 15 18 15 22 15 25 25 15 15 22 15 25 22 25 18 18 18 15 22 25 0 559 78 87 95-55 36 35 96 10 84 67 55 26 45 48 34 18 500 001 900 004 500 $10 03 04 05 06 07 08 09 10...

  • The Primary Key of the table recording this information is SheepID+WeighingDate. The table has not been...

    The Primary Key of the table recording this information is SheepID+WeighingDate. The table has not been normalized beyond First Normal Form. That is, there are no ‘repeating groups’, but there may be Partial and Transitive Dependencies. SheepID Owner Birthdate WeighingDate Vet Weight VetPhoneNum K3922 McNab013 2013-05-12 2013-08-14 M330 22 7633088852 K3922 McNab013 2013-05-12 2014-06-02 S929 34 7609865463 K3922 McNab013 2013-05-12 2015-08-02 M330 43 7633088852 K3922 McNab013 2013-05-12 2016-07-30 P301 53 7682907965 K3922 McNab013 2013-05-12 2017-08-12 P301 52 7682907965 K3922 McNab013...

  • Random Sampling Written Homework 1. The table below lists students in an imaginary Statistics class, along...

    Random Sampling Written Homework 1. The table below lists students in an imaginary Statistics class, along with their identification numbers. Use your graphing calculator to select a random sample of 8s 01. Adrian 07, Gregg 08. Helern 13. Max 19. Stella Adrian07 Greg8 02. Betty 03. Carl 04. Diane 05. Earl ET11. Kar 14. Nicole 15. Oliver 16. Paxton 20. Trace 21. Una 22. Verle 12. Lucy 18. Rashid 17. Quentin 23. Wyatt 24. Xavier 06. Filene 2. The same...

  • How do i find the image height and width in SOF marker Segment( the Xthumbnail and...

    How do i find the image height and width in SOF marker Segment( the Xthumbnail and Ythumbnail are all 0)? Which is the resolution of the image ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 c8 00 c8 00 00 ff db 00 43 00 02 02 02 02 02 01 02 02 02 02 02 02 02 03 03 06 04 03 03 03 03 07 05 05 04 06 08 07...

  • Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART...

    Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART A PART B Assignment 1 #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <stdio.h> #include <ctype.h> #include <string.h> #include <algorithm> using namespace std; /** This structure is to store the date and it has three integer fields **/ struct Date{    int day;    int month;    int year; }; /** This structure is to store the size of the box and it...

  • price time month day year 149.3999939 1 01 02 13 146.5 2 01 03 13 147.3499908...

    price time month day year 149.3999939 1 01 02 13 146.5 2 01 03 13 147.3499908 3 01 04 13 150.3999939 4 01 07 13 148.1499939 5 01 08 13 147.8999939 6 01 09 13 149.6499939 7 01 10 13 153.3499908 8 01 11 13 153.3000031 9 01 14 13 152.5 10 01 15 13 153 11 01 16 13 155.5 12 01 17 13 156.3000031 13 01 18 13 148.5999908 14 01 22 13 150.3999939 15 01 23 13...

  • Please show how you did this in excel. :13-19 Every home football game for the past...

    Please show how you did this in excel. :13-19 Every home football game for the past eight years at Eastern State University has been sold out. The revenues from ticket sales are significant, but the sale of food, beverages, and souvenirs has contrib- uted greatly to the overall profitability of the football program. One particular souvenir is the football pro- gram for each game. The number of programs sold at each game is described by the following probabil- ity distribution:...

  • What is the major product for each reaction? Could you please draw the mechanism? JAN FEB...

    What is the major product for each reaction? Could you please draw the mechanism? JAN FEB MAR APR MAY 01 02 03 04 05 06 07 08 09 10 11 12 13 JUN 14 15 16 JUL AUG SEP O 17 18 19 20 21 22 23 24 25

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