Question

In: Computer Science

Core_java_threads The management wants to know the purchase frequency of items that each individual purchases in...

Core_java_threads

The management wants to know the purchase frequency of items that each individual purchases in a shopping. This is helpful analysing the demand for a particular item.

Given a purchase list of items and the list of items whose purchase frequency is to be determined. Can you determine the frequency of each item whose purchase frequency is to be calculated using multi-threading.

Create a class named ItemCount extends Thread class with the following attributes
Public

  • String itemName
  •     List<String> itemList
  •     Integer count

Include appropriate getters and setters.
Include default and parameterized constructor with following order (itemName, itemList).

And also include the following override methods.

No

Method Name

Method Description

1

public void run()

Override the run method, and find the number of purchase for the given item.


Create the class named as Main, which consists a main method read n, the number of purchases and the item names. Also, read the number of items to be searched and read the search items.
Span a thread to calculate the number of purchases for each item.
Once the thread execution is completed, print the items and number of purchases for it, to the console in the main method.

Input Format :

First line of input contains an integer n, the number of purchases.
The next n lines contains strings, the name of the items purchased.
The next n+1 th line contains an integer s, the number of items to be searched.
The next s lines contains strings the items to be searched.

Output Format :
System.out.format("%-5s %-5s\n","Item Name","Count")

Refer to the sample input and output for more details

[Note :Strictly adhere to the object oriented specifications given as a part of the problem statement.Use the same class names, attribute names and method names.]
[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output :

Enter the number of purchases
10
Enter the items purchased
Redmi 4
Redmi 4 backcase
Redmi 4
Adidas Neo shoes
Apple iphone 7plus
Adidas Neo shoes
Fossil JR1514 Analog-Digital
Ray ban aviator
Adidas Neo shoes
Ray ban aviator

Number of items to search
2
Apple iphone 7plus
Adidas Neo shoes

Item Name Count
Apple iphone 7plus 1
Adidas Neo shoes 3

Don'ts:
      1. Do not create packages for classes. Strictly adhere to the program structure given in the template code.
      2. Do not use Internet Explorer, highly recommended to use chrome browser to launch ebox platform.
      3. Do not create multiple classes inside a single file. Create separate file for each class.

Main.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class Main {

   public static void main(String[] args) throws IOException, InterruptedException{

       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter the number of purchases");
       Integer n = Integer.parseInt(br.readLine());
       List<String> itemList = new ArrayList<String>();
       String item,searchItem;
       System.out.println("Enter the items purchased");
       for(int i=0;i<n;i++){
           item = br.readLine();
           itemList.add(item);
       }
       System.out.println("Number of items to search");
       n = Integer.parseInt(br.readLine());
       List<ItemCount> threadList = new ArrayList<ItemCount>();
       for(int i=0;i<n;i++){
           searchItem = br.readLine();
           threadList.add(new ItemCount(searchItem, itemList));
       }

       //fill in your code here

       System.out.format("%-15s %-15s\n","Item Name","Count");
       for(ItemCount i:threadList){
           System.out.format("%-15s %-15s\n",i.itemName,i.count);
       }
      

   }

}
-----------------------------------

ItemCount.java

import java.util.*;


public class ItemCount //fill in your code here
{
   String itemName;
   List<String> itemList;
   Integer count;
  
   public ItemCount(String itemName,List<String> itemList){
       this.itemName = itemName;
       this.count = 0;
       this.itemList=itemList;
   }
  
   public void run(){

       //fill in your code here

   }

}

Solutions

Expert Solution

Main.java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;


public class Main {

   public static void main(String[] args) throws IOException, InterruptedException{

       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

       System.out.println("Enter the number of purchases");

       Integer n = Integer.parseInt(br.readLine());

       List<String> itemList = new ArrayList<String>();

       String item,searchItem;

       System.out.println("Enter the items purchased");

       for(int i=0;i<n;i++){

           item = br.readLine();

           itemList.add(item);

       }

       System.out.println("Number of items to search");

       n = Integer.parseInt(br.readLine());

       List<ItemCount> threadList = new ArrayList<ItemCount>();

       for(int i=0;i<n;i++){

           searchItem = br.readLine();

           threadList.add(new ItemCount(searchItem, itemList));

       }

       //fill in your code here

       for(ItemCount i:threadList){

           i.start();

           

       }

       System.out.format("%-15s %-15s\n","Item Name","Count");

       for(ItemCount i:threadList){

           System.out.format("%-15s %-15s\n",i.itemName,i.count);

       }

   }

}

-----------------------------------

ItemCount.java

import java.util.*;


public class ItemCount extends Thread //fill in your code here

{

   String itemName;

   List<String> itemList;

   Integer count;

  

   public ItemCount(String itemName,List<String> itemList){

       this.itemName = itemName;

       this.count = 0;

       this.itemList=itemList;

   }

  

   public void run(){

       //fill in your code here

       Iterator iterator = (this.itemList).iterator();

       while (iterator.hasNext()) {

            if(this.itemName.equals(iterator.next()))

                this.count += 1;

       }

    }                                

}

Note 1: Add extends Thread in line number 2 in ItemCount.java file here we inherit the properties of Thread class

Note 2: In line number 14 run method is created which compares the current item to all items for eg if an item is Apple iphone 7plus then it is compared to all 10 items that are present in the list.

Note 3: In Main.java line number 29 we call item.start() method which basically starts the execution of the thread.JVM calls the run() method on the thread.


Related Solutions

A retail corporation wants to know how often a customer returns items in a year. The...
A retail corporation wants to know how often a customer returns items in a year. The company’s data analytics team analyzed customers’ shopping behavior and came up with three categories of customers. Customers in category 1 do not return items often. Customers in category 2 return some items, and customers in category 3 heavily return items. Suppose that the data analytics team modeled the number of returns in each category of customers according to a Poisson process with rates λ1...
Predictive accounting is a trend in that management wants to know not only how product costs...
Predictive accounting is a trend in that management wants to know not only how product costs were derived but also more about what future costs and profits will be. How can managerial accountants provide this information to management?
Predictive accounting is a trend in that management wants to know not only how product costs...
Predictive accounting is a trend in that management wants to know not only how product costs were derived but also more about what future costs and profits will be. How can managerial accountants provide this information to management?
Predictive accounting is a trend in that management wants to know not only how product costs...
Predictive accounting is a trend in that management wants to know not only how product costs were derived but also more about what future costs and profits will be. How can managerial accountants provide this information to management? Do not forget to use APA format on your sources and in-text citations.
Your company provides a variety of delivery services. Management wants to know the volume of a...
Your company provides a variety of delivery services. Management wants to know the volume of a particular delivery that would generate $10,000 per month in operating profits before taxes. The company charges $20 per delivery. The controller’s office has estimated overhead costs at $9,000 per month for fixed costs and $12 per delivery for variable costs. You believe that the company should use regression analysis. Your analysis shows the results to be: Monthly overhead = $ 26 , 501 +...
Quip Corporation wants to purchase a new machine for $290,000. Management predicts that the machine will...
Quip Corporation wants to purchase a new machine for $290,000. Management predicts that the machine will produce sales of $198,000 each year for the next 5 years. Expenses are expected to include direct materials, direct labor, and factory overhead (excluding depreciation) totaling $88,000 per year. The firm uses straight-line depreciation with an assumed residual (salvage) value of $50,000. Quip's combined income tax rate, t, is 30%. Management requires a minimum after-tax rate of return of 10% on all investments. What...
1 The purchases of items from foreigners will be equal to the sales of items to...
1 The purchases of items from foreigners will be equal to the sales of items to foreigners. True False 2 The following chart indicates a hypothetical newspaper quotation of the exchange rates of various currencies. U.S. Dollar Equivalent February 1 February 2 British pound 1.99 1.975 Canadian dollar 0.645 0.86 On February 2, the U.S. dollar (appreciated /depreciated) against the British pound. On February 2, the U.S. dollar (appreciated /depreciated)   against the Canadian dollar. 3 Suppose the exchange rate between...
An obstetrician wants to know whether or not the proportions of children born on each day...
An obstetrician wants to know whether or not the proportions of children born on each day of the week are the same. She randomly selects 500 birth records and obtains the data shown in table. Is there reason to believe that the day on which a child is born occurs with equal frequency at the alpha= 0.01. Sunday Monday Tuesday Wednesday Thursday Friday Saturday Observed count (O) 46 76 83 81 81 80 53 PLEASE FOLLOW THE STEPS BELOW: You...
Pique Corporation wants to purchase a new machine for $270,000. Management predicts that the machine can...
Pique Corporation wants to purchase a new machine for $270,000. Management predicts that the machine can produce sales of $160,000 each year for the next 4 years. Expenses are expected to include direct materials, direct labor, and factory overhead (excluding depreciation) totaling $69,000 per year. The firm uses straight-line depreciation with no residual value for all depreciable assets. Pique's combined income tax rate is 40.00%. Management requires a minimum after-tax rate of return of 10.00% on all investments. What is...
37.) Marc Corporation wants to purchase a new machine for $400,000. Management predicts that the machine...
37.) Marc Corporation wants to purchase a new machine for $400,000. Management predicts that the machine will produce sales of $281,000 each year for the next 5 years. Expenses are expected to include direct materials, direct labor, and factory overhead (excluding depreciation) totaling $81,000 per year. The company uses MACRS for depreciation. The machine is considered to be a 3-year property and is not expected to have any significant residual value at the end of its useful life. Marc's combined...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT