Question

In: Computer Science

2. A cook in canteen prepares parotta and stacks it up in a container, and the...

2. A cook in canteen prepares parotta and stacks it up in a container, and the server takes parotta from the container and serves to his customer. The max capacity of the container is 15. If parotta in the container is empty, server waits for the cook to prepare new parotta. Write a Java program to illustrate the given scenario using multithreading.

b. Write a Java program to define a class ‘Covid19’ to store the below mentioned details of a Covid patients for CMC hospital. Name, age, address, mobile number, blood group, date of Covid checking. symptoms. Create ‘n’ objects of this class for all the Covid patients at india. Write these objects to a file. Read these objects from the file and display only those Covid patient details whose symptoms is ‘fever’ and seven days completed from the date of Covid checking.

please code in java only and also provide the screenshot of output along with the code

Solutions

Expert Solution

Answer 1:

import java.util.LinkedList; 
  
public class Threadexample { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
        final PC pc = new PC(); 
  
        Thread t1 = new Thread(new Runnable() { 
            @Override
            public void run() 
            { 
                try { 
                    pc.produce(); 
                } 
                catch (InterruptedException e) { 
                    e.printStackTrace(); 
                } 
            } 
        }); 
  

        Thread t2 = new Thread(new Runnable() { 
            @Override
            public void run() 
            { 
                try { 
                    pc.consume(); 
                } 
                catch (InterruptedException e) { 
                    e.printStackTrace(); 
                } 
            } 
        }); 
  
        t1.start(); 
        t2.start(); 
  
        t1.join(); 
        t2.join(); 
    } 
  

    public static class PC { 
  
      
        LinkedList<Integer> list = new LinkedList<>(); 
        int capacity =15;
  

        public void produce() throws InterruptedException 
        { 
            int value = 0; 
            while (true) { 
                synchronized (this) 
                { 
                    while (list.size() == capacity) 
                        wait(); 
  
                    System.out.println("Producer produced-"
                                       + value); 
  

                    list.add(value++); 
  
                    notify(); 
  
                    Thread.sleep(1000); 
                } 
            } 
        } 
  
  
        public void consume() throws InterruptedException 
        { 
            while (true) { 
                synchronized (this) 
                { 
                    while (list.size() == 0) 
                        wait(); 
  

                    int val = list.removeFirst(); 
  
                    System.out.println("Consumer consumed-"
                                       + val); 
  
                    notify(); 
  
                    Thread.sleep(1000); 
                } 
            } 
        } 
    } 
} 

Answer 2:

package com.kontrolscan.services.impl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Scanner;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Covid19 {

    public static void main(String[] args) throws ParseException, IOException {
        Scanner s = new Scanner(System.in);
        int noOfPatients= 0;
        File file = new File("/home/amity/Desktop/sample.txt");
        if (file.createNewFile()) {
            System.out.println("File created: " + file.getName());
         } else {
            System.out.println("File already exists.");
          }
        FileWriter fileWriter = new FileWriter(file);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        System.out.println("Enter number of patients:");
        noOfPatients = s.nextInt();
        for(int i =0 ;i< noOfPatients ; i++) {
                CovidPatient currentPatient = new CovidPatient("Patient"+i+1,20,"New York","+123345667","AB+",new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse("02/10/2020 10:34:33"),"fever");
                String jsonString = mapper.writeValueAsString(currentPatient);
                fileWriter.write(jsonString);
                
        }
        fileWriter.close();
        File myObj = new File("/home/amity/Desktop/sample.txt");
        Scanner myReader = new Scanner(myObj);
        while (myReader.hasNextLine()) {
          String data = myReader.nextLine();
          Date currentDate1 = new Date();
          CovidPatient patient = mapper.readValue(data, CovidPatient.class);
          String admittedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(patient.getCovidCheckingDate());
          String currentDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentDate1);
          DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
          LocalDate from = LocalDate.parse(admittedDate,format);
          LocalDate to = LocalDate.parse(currentDate,format);

          long days = ChronoUnit.DAYS.between(from, to);
          if(days > 7 && patient.getSymptoms().equals("fever")) {
                  System.out.println(patient);
          }

          
          
        }
        myReader.close();
        
        
    }
        
}

@JsonInclude(Include.NON_DEFAULT)
class CovidPatient {
        String name;
        int age;
        String address;
        String mobileNumber;
        String bloodGroup;
        Date covidCheckingDate;
        String symptoms;
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public int getAge() {
                return age;
        }
        public void setAge(int age) {
                this.age = age;
        }
        public String getAddress() {
                return address;
        }
        public void setAddress(String address) {
                this.address = address;
        }
        public String getMobileNumber() {
                return mobileNumber;
        }
        public void setMobileNumber(String mobileNumber) {
                this.mobileNumber = mobileNumber;
        }
        public String getBloodGroup() {
                return bloodGroup;
        }
        public void setBloodGroup(String bloodGroup) {
                this.bloodGroup = bloodGroup;
        }
        public Date getCovidCheckingDate() {
                return covidCheckingDate;
        }
        public void setCovidCheckingDate(Date covidCheckingDate) {
                this.covidCheckingDate = covidCheckingDate;
        }
        public CovidPatient(String name, int age, String address, String mobileNumber, String bloodGroup,
                        Date covidCheckingDate,String symptoms) {
                super();
                this.name = name;
                this.age = age;
                this.address = address;
                this.mobileNumber = mobileNumber;
                this.bloodGroup = bloodGroup;
                this.covidCheckingDate = covidCheckingDate;
                this.symptoms = symptoms;
        }
        
        public String getSymptoms() {
                return symptoms;
        }
        public void setSymptoms(String symptoms) {
                this.symptoms = symptoms;
        }
        public CovidPatient() {
                
        }
        @Override
        public String toString() {
                return "CovidPatient [name=" + name + ", age=" + age + ", address=" + address + ", mobileNumber=" + mobileNumber
                                + ", bloodGroup=" + bloodGroup + ", covidCheckingDate=" + covidCheckingDate + "]";
        }
        
        
}

Related Solutions

. As part of his summer job at a resturant, Jim learned to cook up a...
. As part of his summer job at a resturant, Jim learned to cook up a big pot of soup late at night, just before closing time, so that there would be plenty of soup to feed customers the next day. He also found out that, while refrigeration was essential to preserve the soup overnight, the soup was too hot to be put directly into the fridge when it was ready. (The soup had just boiled at 100 degrees C,...
Container 1 has 8 items, 3 of which are defective. Container 2 has 5 items, 2...
Container 1 has 8 items, 3 of which are defective. Container 2 has 5 items, 2 of which are defective. If one item is drawn from each container, what is the probability that only one of the items is defective?
Container 1 has 8 items,3of which are defective. Container 2 has 5 items, 2 of which...
Container 1 has 8 items,3of which are defective. Container 2 has 5 items, 2 of which are defective. If one item is drawn from each container, what is the probability that only one of the items is defective? 0.2250 0.3000 0.0250 0.4750 0.1500
Ma’s Meat Sauce has come up with a new product, Slow Cook Meat Sauce, to provide...
Ma’s Meat Sauce has come up with a new product, Slow Cook Meat Sauce, to provide a superior spaghetti sauce cooked over a longer period. Ma paid $130,000 for a marketing survey to determine the viability of the products, including pricing and projected sales. They believe they will generate sales of $890, 000 in year 1, $900,000 in year 2, $910,000 in year 3 and in the final year $900,000. Fixed costs are estimated at $230,000 per year and variable...
Ma’s Meat Sauce has come up with a new product, Slow Cook Meat Sauce, to provide...
Ma’s Meat Sauce has come up with a new product, Slow Cook Meat Sauce, to provide a superior spaghetti sauce cooked over a longer period. Ma paid $130,000 for a marketing survey to determine the viability of the products, including pricing and projected sales. They believe they will generate sales of $890, 000 in year 1, $900,000 in year 2, $910,000 in year 3 and in the final year $900,000. Fixed costs are estimated at $230,000 per year and variable...
This assignment asks you to set up an Excel budget spreadsheet file that automatically prepares the...
This assignment asks you to set up an Excel budget spreadsheet file that automatically prepares the master budget for a company, given sales projections and information on beginning balances, production requirements, desired ending inventories, etc. Information on developing the budgets appears in Chapter 8 of your text, and examples of budget worksheets appear in the schedules throughout the chapter. Data Glamour Inc. produces and sells lady handbags. Below is information on its activities for the next few months. Sales projections...
Exercise 1-2. Sunk Cost [LO 2] Rachel Cook owns Campus Copies, a copy business with several...
Exercise 1-2. Sunk Cost [LO 2] Rachel Cook owns Campus Copies, a copy business with several high-speed copy machines. One is a color copier that was purchased just last year at a cost of $25,000. Recently a salesperson got Rachel to witness a demo of a new $23,000 color copier that promises higher speed and more accurate color representation. Rachel is interested but she can’t get herself to trade in a perfectly good copier for which she paid $25,000 and...
2.-Airtight and Longlife are competitors in the commercial container industry. The demand curves for an important...
2.-Airtight and Longlife are competitors in the commercial container industry. The demand curves for an important product of both companies are: Airtight PA = 1000 - 5QA QA = 200- .2Pa Longlife PL = 1600 - 4QL The companies currently sell 100 and 250 units of their product, respectively: a) What are the price point elasticities currently facing the two firms? b) Suppose Longlife lowers its prices and increases its sales to 300 units, and that this action results in...
A rigid container has water of 2 kg at 120 C with a quality of 25%....
A rigid container has water of 2 kg at 120 C with a quality of 25%. The container is heated to the temperature of 140 Celsius, calculate the new quality?
A closed container with a volume of 2 m3 contains 4 kg of saturated liquid and...
A closed container with a volume of 2 m3 contains 4 kg of saturated liquid and saturated steam mixture water at 200 kPa pressure. Heat is given from the ambient temperature of 160⁰C until the water in the container is completely saturated. Note: T (K) = 273 + ºC a) Find the entropy change of the system. b) Find the heat transfer to the container. c) Find the total entropy change.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT