In: Computer Science
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
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
}
}
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.