***FILL IN THE PROVIDED CODE IN JAVA, WITH ONLY FILE 3 NEEDING TO BE FILLED ***
ID: 3720905 • Letter: #
Question
***FILL IN THE PROVIDED CODE IN JAVA, WITH ONLY FILE 3 NEEDING TO BE FILLED ***
Objective: Consider records for patients at a medical facility. Each record contains an integer identification for a patient and strings for the date, the reason for the visit, and the treatment prescribed. Design and implement the class PatientRecord so that it overrides the method hashCode. A program that tests your new class is provided. The code for this part is in the ZIP archive. Your program’s output must be identical to the output of the sample run:?
File 1: BadVisitDateException
File 2: Driver
File 3: PatientRecord
Output . CSC220 (run) × | Notifications | run: Patient Record created: Patient:101 [12/15/2006] Complaint: cough Prescribed: bed rest It has hash code 2115279812 Patient Record created: Patient : 101 [12/17/2006] Complaint : high fever Prescribed : antibiotics It has hash code 1455917371 Creation failed asmt04PatientRecord.BadVisitDateException: Month not in range 1-12 Creation failed asmt04PatientRecord.BadvisitDateException: Day not in range 1-31 Creation failed asmt04PatientRecord.BadVisitDateException: Year not greater than 1900 BUILD SUCCESSFUL (total time: 0 seconds)Explanation / Answer
I have completed the implementation of PatientRecord class. Please note that you have not mentioned about the steps in finding the hash value. Therefore I have used a standard method to calculate the hash (including all fields) using a prime number. So the hash values may not be equal to what you have provided in the sample output. If you want it to be the same, I’ll need to know more details about finding the hash [it is nearly impossible to find the hashing method from hashcode]. Thanks.
// PatientRecord.java
public class PatientRecord {
private int id;
private String date;
private String reasonForVisit;
private String treatmentPrescribed;
public PatientRecord(int anId, int month, int day, int year, String reason,
String treatment) throws BadVisitDateException {
/**
* validating month, day and year fields
*/
if (month < 1 || month > 12) {
throw new BadVisitDateException("Month not in range 1-12");
}
if (day < 1 || day > 31) {
throw new BadVisitDateException("Day not in range 1-31");
}
if (year < 1900) {
throw new BadVisitDateException("Year not greater than 1900");
}
this.id = anId;
this.date = "[" + month + "/" + day + "/" + year + "]";
this.reasonForVisit = reason;
this.treatmentPrescribed = treatment;
}
/**
* Returns the patient's ID number.
*/
public int getPatientID() {
return id;
}
/**
* Returns the patient's visit date.
*/
public String getVisitDate() {
return date;
}
/**
* Returns the patient's reason for visiting.
*/
public String getReasonForVisit() {
return reasonForVisit;
}
/**
* Returns the patient's prescribed treatment.
*/
public String getTreatmentPrescribed() {
return treatmentPrescribed;
}
/**
* returns the hash code
*/
public int hashCode() {
/**
* As you have not mentioned about how to find the hashcode, I'm just
* using the standard way, using a prime number
*/
int result = 1; // starting value
int prime = 31; // prime number to be used
/**
* finding and adding hash values of all fields
*/
result = prime * result + id;
result = prime * result + date.hashCode();
result = prime * result + reasonForVisit.hashCode();
result = prime * result + treatmentPrescribed.hashCode();
return result;
}
/**
* checks if two patients are equal
*/
public boolean equals(Object other) {
if (other instanceof PatientRecord) {
PatientRecord p = (PatientRecord) other;// safe type casting
if (p.id == this.id) {
// if two patients have same id, they must be equal
return true;
}
}
return false;
}
public String toString() {
/**
* returns a properly formatted string
*/
return "Patient:" + id + " " + date + " Complaint: " + reasonForVisit
+ " Prescribed: " + treatmentPrescribed;
}
}
//Driver.java
/**
*
* DO NOT CHANGE
*
* A driver for the class PatientRecord.
*/
public class Driver {
public static void main(String args[]) {
PatientRecord test1;
PatientRecord test2;
try {
test1 = new PatientRecord(101, 12, 15, 2006, "cough", "bed rest");
System.out.println("Patient Record created: " + test1);
System.out.println("It has hash code " + test1.hashCode());
}
catch (BadVisitDateException e) {
System.out.println("Creation failed " + e);
}
try {
test2 = new PatientRecord(101, 12, 17, 2006, "high fever", "antibiotics");
System.out.println("Patient Record created: " + test2);
System.out.println("It has hash code " + test2.hashCode());
}
catch (BadVisitDateException e) {
System.out.println("Creation failed " + e);
}
try {
test2 = new PatientRecord(101, 17, 17, 2006, "high fever", "antibiotics");
System.out.println("Patient Record created: " + test2);
System.out.println("It has hash code " + test2.hashCode());
}
catch (BadVisitDateException e) {
System.out.println("Creation failed " + e);
}
try {
test2 = new PatientRecord(101, 12, 92, 2006, "high fever", "antibiotics");
System.out.println("Patient Record created: " + test2);
System.out.println("It has hash code " + test2.hashCode());
}
catch (BadVisitDateException e) {
System.out.println("Creation failed " + e);
}
try {
test2 = new PatientRecord(101, 12, 17, 06, "high fever", "antibiotics");
System.out.println("Patient Record created: " + test2);
System.out.println("It has hash code " + test2.hashCode());
}
catch (BadVisitDateException e) {
System.out.println("Creation failed " + e);
}
}
}
// BadVisitDateException.java
/**
* DO NOT CHANGE
*
* Visit record could not be created when the date information was invalid.
*/
public class BadVisitDateException extends Exception {
public BadVisitDateException(String reason) {
super(reason);
}
}
/*OUTPUT*/
Patient Record created: Patient:101 [12/15/2006] Complaint: cough Prescribed: bed rest
It has hash code -152201402
Patient Record created: Patient:101 [12/17/2006] Complaint: high fever Prescribed: antibiotics
It has hash code 560688996
Creation failed patient.BadVisitDateException: Month not in range 1-12
Creation failed patient.BadVisitDateException: Day not in range 1-31
Creation failed patient.BadVisitDateException: Year not greater than 1900
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.