/ch 7 #4, Programming logic & Design Introductory Joyce Farrell, 4. The Apgar Me
ID: 3765306 • Letter: #
Question
/ch 7 #4, Programming logic & Design Introductory Joyce Farrell,
4. The Apgar Medical group keeps a patient file for each Dr. Records contain the patient’s first and last name, address, and birth year, sorted in ascending birth year order. Two doctors, best and Cushing have formed a partnership.
a. Design the logic that produces a merged list of patients’ names in ascending order by birth year.
b. Modify the program so that it does not display names, but only produces a count of the number of patients born each year.
Explanation / Answer
Since its not mentioned that the records should be stored in file so I did this program as per my understanding.
Two array lists are used to hold patient objects for two doctors and after intialization of patient objects for both the doctors, these tow lists are merged into a single list after which the list is sorted based on the birth dates of the patients (part a of the question). Part b of the question is implemented as static method named "displayYearCount()".
The patient class implements Comparable interface and then its compareTo() method has been overidden to perform sorting of the objects.
Here is the complete code
import java.util.*;
class Patient implements Comparable<Patient>{
private String lastname;
private String firstname;
private String address;
private int birthyear;
public Patient(String lastname, String firstname, String address, int birthyear) {
this.lastname = lastname;
this.firstname = firstname;
this.address = address;
this.birthyear = birthyear;
}
public String getName() {
return firstname+" "+lastname;
}
public void setStudentname(String studentname) {
}
public int getBirthyear() {
return birthyear;
}
public void setRollno(int rollno) {
}
public int getStudentage() {
return 0;
}
public void setStudentage(int studentage) {
}
//////////// Overide this method of Comparable Interface to perform sorting ///////////
public int compareTo(Patient comparestu) {
int compareage=((Patient)comparestu).getBirthyear();
/* For Ascending order*/
return this.birthyear-compareage;
}
public String toString() {
return " "+firstname + " "+lastname+" "+ birthyear;
}
}
public class ArrayListSorting {
////////////// This method produces a count of the number of patients born each year.///
public static void displayYearCount(ArrayList<Patient> mergedRecords)
{
Patient vals[]=new Patient[mergedRecords.size()];
mergedRecords.toArray(vals);
int n=vals.length;
int yearcount[][]=new int[n][2];
Patient p,p1;
for(int i=0;i<n;i++)
{
int x=1,y2=0;
p=vals[i];
int y1=p.getBirthyear();
for(int j=0;j<n;j++)
{
if(i!=j)
{
p1=vals[j];
y2=p1.getBirthyear();
if(y1==y2)
{
x++;
}
}
}
yearcount[i][0]=y1; yearcount[i][1]=x;
}
System.out.println(" (b) Year No. of Births ");
for(int z=0;z<n;z++) System.out.println(" "+yearcount[z][0]+" "+yearcount[z][1]);
}
public static void main(String args[]){
ArrayList<Patient> doc1 = new ArrayList<Patient>(); //List to hold first doctor's patients
doc1.add(new Patient("Khan","Asif","Kashmir",1987));
doc1.add(new Patient("Shah","Junaid","Kashmir",1986));
doc1.add(new Patient("Sheikh","Junaid","Kashmir",1988));
ArrayList<Patient> doc2 = new ArrayList<Patient>(); //List to hold second doctor's patients
doc2.add(new Patient("Khan","Asif","Kashmir",1982));
doc2.add(new Patient("Shah","Junaid","Kashmir",1981));
doc2.add(new Patient("Sheikh","Junaid","Kashmir",1998));
doc2.add(new Patient("Baba","Naseer","Kashmir",1998));
ArrayList<Patient> mergedRecords = new ArrayList<Patient>(); //List to hold merded records
mergedRecords.addAll(doc1);
mergedRecords.addAll(doc2);
Collections.sort(mergedRecords); // Sort the records
System.out.println(" (a) Name Birthyear ");
for(Patient str: mergedRecords){
System.out.println(str);
}
displayYearCount(mergedRecords);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.