Question

Suppose that we want to help physicians to diagnose illnesses. A physician observes a patient's symptoms and considers the illnesses that could be associated with those symptoms. Design and implem...

Suppose that we want to help physicians to diagnose illnesses. A physician
observes a patient's symptoms and considers the illnesses that could be
associated with those symptoms. Design and implement a class PhysiciansHelper
thatprovide a list of those illnesses.

PhysiciansHelper should contain a dictionary of illnesses and symptoms.
A method should read a text file of illnesses and their symptoms into
the dictionary. See format in sample data file.

PhysiciansHelper should read a list of symptoms for a patient.
A method should read a list of symptoms and store into a list.
Then, it finds a list illnesses that are associated with those symptoms.
See format in sample run output.

Requirements:
=====================
You will need to complete two methods in PhysiciansHelper class:
   private void processDatafile();   
   private void processSymptoms();
Refer to PhysiciansHelper for detailed info.

* You must use the predefined private Map<String, List<String>> symptomChecker
to store symptoms to illnesses mapping info. See format in sample run output.

* All string data are not case sensitive, i.e. should work for either uppercase
or lowercase letters.

* In processSymptoms() methods, you must
- list possible symptoms in sorted order.
- read user input symptoms in single line
- check for invalid symptoms and eliminate duplicated symptoms
- displayresult in format as shown in sample output
(must count number of matched symptoms)

* Use Java API Map and List in your project
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
https://docs.oracle.com/javase/8/docs/api/java/util/List.html

Here are some methods that you may need:
   Map: get(), put(), containsKey(), keySet(), entrySet()
   List: add(), contains(), size()

Compile program: javac PhysiciansHelper.java
  
Sample Run: Note: I expect similar output from your program
==========

$ cat sample_data.txt           // contents of sample data file
cold: runny nose|cough|sore throat
flu: runny nose|cough|sore throat|muscle aches|fever
mumps: high fever| rash| swelling
whooping cough: cough| rash| high fever
acid reflux: chest pain| burping
chickenpox: fever| blister
measles : runny nose| cough| fever| rash
norovirus : nausea| diarrhoea| vomiting
ague: sweatiness| achy| high fever

$ javac PhysiciansHelper.java

$ java PhysiciansHelper
Enter data filename: sample_data.txt

============================================
symptomChecker map:
achy=[ague]
blister=[chickenpox]
burping=[acid reflux]
chest pain=[acid reflux]
cough=[cold, flu, whooping cough, measles]
diarrhoea=[norovirus]
fever=[flu, chickenpox, measles]
high fever=[mumps, whooping cough, ague]
muscle aches=[flu]
nausea=[norovirus]
rash=[mumps, whooping cough, measles]
runny nose=[cold, flu, measles]
sore throat=[cold, flu]
sweatiness=[ague]
swelling=[mumps]
vomiting=[norovirus]
============================================

The possible symptoms are:
achy
blister
burping
chest pain
cough
diarrhoea
fever
high fever
muscle aches
nausea
rash
runny nose
sore throat
sweatiness
swelling
vomiting

============================================

Enter symptoms: cough,rash,cough,fever,blisterx,cough,runny NOSE
=> duplicate symptom:cough
=> invalid symptom:blisterx
=> duplicate symptom:cough

============================================

PatientSymptoms list: [cough, rash, fever, runny nose]

Possible illnesses that match any symptom are:

==> Disease(s) match 4 symptom(s)
measles

==> Disease(s) match 3 symptom(s)
flu

==> Disease(s) match 2 symptom(s)
whooping cough
cold

==> Disease(s) match 1 symptom(s)
chickenpox
mumps

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

sample_data.txt
cold: runny nose | cough | sore throat
flu: runny nose | cough | sore throat | muscle aches | fever
mumps: high fever | rash | swelling
whooping cough: cough | rash | high fever
acid reflux: chest pain | burping
chickenpox: fever | blister
measles : runny nose | cough | fever | rash
norovirus : nausea | diarrhoea | vomiting
ague: sweatiness | achy | high fever

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

PhysiciansHelper.java

importjava.util.*;
importjava.io.*;

public class PhysiciansHelper
{
   // symptom to illnesses map
   private Map<String, List<String>> symptomChecker;


   /* Constructor symptomChecker map using TreeMap */
   public PhysiciansHelper()
   {
       // use TreeMap, i.e. sorted order keys
       symptomChecker = new TreeMap<String,List<String>>();
   } // end default constructor


   /* Reads a text file of illnesses and their symptoms.
   Each line in the file has the form
       Illness: Symptom| Symptom| Symptom| ...
   Store data into symptomChecker map */

   private void processDatafile()
   {
       // Step 1: read in a data filename from keybaord
       // create a scanner for the file

       // Step 2: process data lines in file scanner
       // 2.1 for each line, split the line into a disease and symptoms
       // make sure to trim() spaces and use toLowercase()
       // 2.2 for symptoms, split into individual symptom
       // create a scanner for symptoms with delimiter "|"
   // i.e. use method useDelimiter("\\|")   
       // make sure to trim() spaces and use toLowercase()
       // for each symptom
       // if it is already in the map, insert disease into related list
       // if it is not already in the map, create a new list with the disease
       // Step 3: display symptomChecker map

       // implement here.....

   } // end processDatafile


   /* Read patient's symptoms in a line and adds them to the list.
       Input format: Symptom, Symptom, Symptom,...
   Shows diseases that match a given number of the symptoms. */

   private void processSymptoms()
   {
       // Step 1: get all possible symptoms from symptomChecker map
       // and display them
       // Step 2: process patient symptoms, add to patientSymptoms list
       // read patient's symptoms in a line, separated by ','
       // create a scanner for symptoms
       // UseDelimeter(",") to split into individual symptoms
       // make sure to trim() spaces and use toLowercase()
       // add valid symptoms to patientSymptoms list
       // display invalid/duplicate symptoms
       // Step 3: display patientSymptoms list
    // Step 4: process illnesses to frequency count
       // create a map of disease name and frequency count
       // for each symptom in patientSymptoms list
       // get list of illnesses from symptomChecker map
       // for each illness in the list
       //    if it is already in the map, increase counter by 1
   //   if it is not already in the map, create a new counter 1
       // ** need to keep track of maximum counter numbers (Max)
       // Step 5: display result
       // for count i = Max downto 1
       // display illness that has count i
         

       // implement here.....

   } // end processSymptoms

   public static void main(String[] args)
   {

       PhysiciansHelper lookup = new PhysiciansHelper();
       lookup.processDatafile();
       lookup.processSymptoms();
   } // end main
} // end PhysiciansHelper

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
import java.io.*;

public class PhysiciansHelper
{
// symptom to illnesses map
private Map<String, List<String>> symptomChecker;


/* Constructor symptomChecker map using TreeMap */
public PhysiciansHelper()
{
// use TreeMap, i.e. sorted order keys
symptomChecker = new TreeMap<String,List<String>>();
} // end default constructor


/* Reads a text file of illnesses and their symptoms.
Each line in the file has the form
Illness: Symptom| Symptom| Symptom| ...
Store data into symptomChecker map */

private void processDatafile()
{
// Step 1: read in a data filename from keybaord
// create a scanner for the file
   try {
System.out.print("Enter data filename : ");

Scanner input = new Scanner(System.in);
//String cwd = new File(".").getAbsolutePath();
File file = new File(input.nextLine());

input = new Scanner(file);


while (input.hasNextLine()) {
   // Step 2: process data lines in file scanner
// 2.1 for each line, split the line into a disease and symptoms
// make sure to trim() spaces and use toLowercase()
     
   String line = input.nextLine();

String []singleentry = line.trim().split(":");
String []symptom = singleentry[1].trim().split("\\|");

List<String> singleentryTrim= Arrays.asList(singleentry);
for(int i = 0; i < singleentryTrim.size();i++)
{
   singleentryTrim.set(i, singleentryTrim.get(i).trim().toLowerCase());
}

List<String> symTrim= Arrays.asList(symptom);

for(int i = 0; i < symTrim.size();i++)
{
   symTrim.set(i, symTrim.get(i).trim().toLowerCase());
}

// 2.2 for symptoms, split into individual symptom
// create a scanner for symptoms with delimiter "|"
// i.e. use method useDelimiter("\\|")   
// make sure to trim() spaces and use toLowercase()
// for each symptom
// if it is already in the map, insert disease into related list
// if it is not already in the map, create a new list with the disease

for(int i = 0; i < symTrim.size();i++)
{
     
if(symptomChecker.containsKey(symTrim.get(i)))
{
   List<String> illness = symptomChecker.get(symTrim.get(i));
   if(illness.contains(singleentryTrim.get(0)) == false)
   {
       symptomChecker.put(symTrim.get(i), null);
         
       List<String> illList = new ArrayList<String>(illness);
         
       illList.add(singleentryTrim.get(0));
       symptomChecker.put(symTrim.get(i), illList);
         
   }
     
}
else {
   List<String> illness = Arrays.asList(singleentryTrim.get(0));
   symptomChecker.put(symTrim.get(i), illness);
}
}
           }
//System.out.println(line);

input.close();

} catch (Exception ex) {
ex.printStackTrace();
}
     

// Step 3: display symptomChecker map
   for (Map.Entry<String,List<String>> entry : symptomChecker.entrySet())
   {
System.out.print(entry.getKey() + "=[");

List<String> valueList = entry.getValue();
for(int i = 0;i < valueList.size();i++)
{
   System.out.print(valueList.get(i));
   if(i< (valueList.size() -1))
   {
       System.out.print(",");
   }
}
System.out.println("]");
   }
// implement here.....

} // end processDatafile


/* Read patient's symptoms in a line and adds them to the list.
Input format: Symptom, Symptom, Symptom,...
Shows diseases that match a given number of the symptoms. */

private void processSymptoms()
{
// Step 1: get all possible symptoms from symptomChecker map
// and display them
   System.out.print(" Enter symptoms:");
  
   Scanner input = new Scanner(System.in);
   String sympatient = input.nextLine();
     
     
     
// Step 2: process patient symptoms, add to patientSymptoms list
// read patient's symptoms in a line, separated by ','
// create a scanner for symptoms
// UseDelimeter(",") to split into individual symptoms
// make sure to trim() spaces and use toLowercase()
String[] SymList = sympatient.split(",");
     
   List<String> SymListTrim= Arrays.asList(SymList);
for(int i = 0; i < SymListTrim.size();i++)
{
   SymListTrim.set(i, SymListTrim.get(i).trim().toLowerCase());
}


List<String> invalidSymptoms = new ArrayList<String>();
List<String> patientSymptoms = new ArrayList<String>();
List<String> duplicateSymptoms = new ArrayList<String>();
for(int i = 0; i < SymListTrim.size();i++)
{
   if(symptomChecker.containsKey(SymListTrim.get(i)))
   {
       if(patientSymptoms.contains(SymListTrim.get(i)))
       {
           duplicateSymptoms.add(SymListTrim.get(i));
       }
       else {
           patientSymptoms.add(SymListTrim.get(i));
           }
         
   }
   else
   {
       invalidSymptoms.add(SymListTrim.get(i));
   }
}

//List<String> patientSymptoms;
// add valid symptoms to patientSymptoms list
// display invalid/duplicate symptoms
if(invalidSymptoms.size() > 0)
{
   System.out.print(" Invalid symptoms:[");
   for(int i =0 ; i < invalidSymptoms.size(); i++)
   {
       System.out.print(invalidSymptoms.get(i));
       if(i< (invalidSymptoms.size() -1))
   {
       System.out.print(", ");
   }
   }
   System.out.println("]");
}

if(patientSymptoms.size() > 0)
{
   System.out.print(" Pateint symptoms:[");
   for(int i =0 ; i < patientSymptoms.size(); i++)
   {
       System.out.print(patientSymptoms.get(i));
       if(i< (patientSymptoms.size() -1))
   {
       System.out.print(", ");
   }
   }
   System.out.println("]");
}

if(duplicateSymptoms.size() > 0)
{
   System.out.print(" Duplicate symptoms:[");
   for(int i =0 ; i < duplicateSymptoms.size(); i++)
   {
       System.out.print(duplicateSymptoms.get(i));
       if(i< (duplicateSymptoms.size() -1))
   {
       System.out.print(", ");
   }
   }
   System.out.println("]");
}

// Step 3: display patientSymptoms list

// Step 4: process illnesses to frequency count
// create a map of disease name and frequency count
Map<String, Integer> nameCount = new TreeMap<String, Integer>();
  
// for each symptom in patientSymptoms list
// get list of illnesses from symptomChecker map
// for each illness in the list
// if it is already in the map, increase counter by 1
// if it is not already in the map, create a new counter 1
// ** need to keep track of maximum counter numbers (Max)
int max = -1;
for(int i=0;i<patientSymptoms.size();i++)
{
   List<String> illList = symptomChecker.get(patientSymptoms.get(i));
     
   for(int j = 0; j< illList.size(); j++)
   {
       if(nameCount.containsKey(illList.get(j)))
       {
           int count = nameCount.get(illList.get(j));
           nameCount.put(illList.get(j), ++count);
           if(max < count)
           {
               max = count;
           }
       }
       else
       {
           nameCount.put(illList.get(j), 1);
           if(max < 1)
           {
               max = 1;
           }
       }
   }
}
  


// Step 5: display result
// for count i = Max downto 1
// display illness that has count i

Map<Integer, List<String>> diseaseCount = new LinkedHashMap<Integer, List<String>>();

for (int i = max; i>=1; i--)
{
   for (Map.Entry<String,Integer> entry : nameCount.entrySet())
   {
       int count = entry.getValue();
       if(count == i)
       {
           if(diseaseCount.containsKey(i))
       {
           List<String> disease= diseaseCount.get(i);
           diseaseCount.put(i, null);
           disease.add(entry.getKey());
           diseaseCount.put(i, disease);
       }
       else
       {
           List<String> disease= new ArrayList<String>();
           disease.add(entry.getKey());
           diseaseCount.put(i, disease);
       }
       }
   }
}

for (Map.Entry<Integer,List<String>> entry : diseaseCount.entrySet())
   {
         
           System.out.println(String.format("==> Disease(s) match %1$s symptom(s)", entry.getKey()));
             
           for(int i=0;i<entry.getValue().size();i++)
           {
           System.out.println(String.format("%1$s",entry.getValue().get(i)));
           }
   }

// implement here.....

} // end processSymptoms

public static void main(String[] args)
{

PhysiciansHelper lookup = new PhysiciansHelper();
lookup.processDatafile();
lookup.processSymptoms();
} // end main
} // end PhysiciansHelper

Add a comment
Know the answer?
Add Answer to:
Suppose that we want to help physicians to diagnose illnesses. A physician observes a patient's symptoms and considers the illnesses that could be associated with those symptoms. Design and implem...
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
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