Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part A: Open the file BloodData.java that includes fields that hold a blood type

ID: 3813913 • Letter: P

Question

Part A:

Open the file BloodData.java that includes fields that hold a blood type (the four blood types are O, A, B, and AB) and an Rh factor (the factors are + and –). Create a default constructor that sets the fields to “O” and “+”, and an overloaded constructor that requires values for both fields. Include get and set methods for each field. Open the file TestBloodData.java and click the "Run Code" button to demonstrate that each method works correctly.

Part B:

Open the file Patient.java which includes an ID number, age, and BloodData. Provide a default constructor that sets the ID number to “0”, the age to 0, and the BloodData to “O” and “+”. Create an overloaded constructor that provides values for each field. Also provide get methods for each field. Open TestPatient.java. Click the green "Run Code" button at the bottom of your screen to demonstrate that each method works correctly.

Explanation / Answer

Package Blood;

}

import java.util.HashMap; import java.util.Map; import java.util.NoSuchElementException; public enum BloodType { O("O"), A("A"), B("B"), AB("AB"); private final String dispName; private static final Map<String, BloodType> strToBlood = new HashMap<String, BloodType>(); static { for(BloodType bt : BloodType.values()) { strToBlood.put(bt.dispName, bt); } } private BloodType(String dispName) { this.dispName = dispName; } public boolean canGiveTo(BloodType cand) { if(this.equals(BloodType.O) // O can donate to {O,A,B,AB} || cand.equals(BloodType.AB) // AB can receive from {O,A,B,AB} || this.equals(cand)) { // A gives to {A,AB}, B gives to {B,AB} return true; } else { // O cannot receive from {A,B,AB} // A cannot receive from {B,AB} // B cannot receive from {A,AB} return false; } } public boolean canGetFrom(BloodType donor) { return donor.canGiveTo(this); } public static BloodType getBloodType(String name) { String key = name.trim().replaceAll("[0-9]","").toUpperCase(); if(strToBlood.containsKey(key)) { return strToBlood.get(key); } else { throw new NoSuchElementException(key + " not a recognized blood type."); } } @Override public String toString() { return dispName; }

Package Blood;

public class BloodData { private String bloodType; private char rhFactor; public BloodData() { bloodType = "O"; rhFactor = '+'; } public BloodData(String type, char factor) { bloodType = type; rhFactor = factor; } public void setBloodType(String type) { this.bloodType = type; } public void setRhFactor(char factor) { this.rhFactor = factor; } public String getBloodType() { return bloodType; } public char getRhFactor() { return rhFactor; } }

}