Question: Create a class named BloodData that includes fields that hold a blood type (the four blood types are O , A , B , and
Create a class named BloodData 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.
Create a class named Patient that 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 values to O and +. Create an overloaded constructor that provides values for each field. Also provide get methods for each field.
This is the code I have but it is not correct:
public class BloodData { private String blood_Type; private char rh_Factor; public BloodData() { blood_Type = "O"; rh_Factor = '+'; } /* OVERLOADED CONSTRUCTOR */ public BloodData(String type1, char factor1) { blood_Type = type1; rh_Factor = factor1; } /* SET METHODS */ public void setBloodGroupType(String type1) { this.blood_Type = type1; } public void setBloodGroupRhFactor(char factor1) { this.rh_Factor = factor1; } /* GET METHODS */ /* RETURN THE BLOOD TYPE */ public String getBloodGroupType() { return blood_Type; } /* RETURN THE Rh FACTOR OF THE BLOOD */ public char getBloodGroupRhFactor() { return rh_Factor; } }
public class TestBloodData { public static void main(String[] args) { //CREATE INSTANCE OF BloodData WITH DEFAULT VALUE BloodData data1 = new BloodData(); System.out.println("Blood GroupType: " + data1.getBloodGroupType()); System.out.println("Blood GroupRH Factor: " + data1.getBloodGroupRhFactor()); BloodData data2 = new BloodData(); //SET THE BLOOD TYPE AND RH FATOR BY SETTER METHODS data2.setBloodGroupType("A"); data2.setBloodGroupRhFactor('-'); System.out.println(); System.out.println("Blood Group Type: " + data2.getBloodGroupType()); System.out.println("Blood Group RH Factor: " + data2.getBloodGroupRhFactor()); System.out.println(); ////CREATE INSTANCE OF BloodData WITH VALUE BloodData data3 = new BloodData("A", '+'); System.out.println("Blood Group Type: " + data3.getBloodGroupType()); System.out.println("Blood GroupRH Factor: " + data3.getBloodGroupRhFactor()); } } public class Patient { //VARIABLES DECLARATION FOR PATIENT ID , PATIENT'S AGE AND private int pID; private int pAge; //VARIABLE FOR BloodData private BloodData pBloodData; //DEFAULT CONSTRUCTOR public Patient() { pID= 0; pAge = 0; pBloodData = new BloodData("O", '+'); } //OVERLOADED CONSTRUCTOR public Patient(int id1, int age1, String type1, char factor1) { pID = id1; pAge = age1; pBloodData = new BloodData(type1,factor1); } /* GETTER METHODS*/ /* GET PATIENT ID*/ public int get_PatientID() { return pID; } /* GET PATIENT AGE */ public int get_PatientAge() { return pAge; } /* GET PATIENT BloodData TYPE */ public String getPatient_BloodGroupType() { return pBloodData.getBloodGroupType(); } /* GET PATIENT BloodData Rh Factor */ public char getPatient_BloodGroupRhFactor() { return pBloodData.getBloodGroupRhFactor(); } }
public class TestPatient { public static void main(String[] args) { //CREATE AN INSTANCE OF PATIENT Patient patient1 = new Patient(1,35,"B",'+'); //DISPLAY PATIENT INFORMATION System.out.println("Patient Identification: " + patient1.get_PatientID()); System.out.println("Patient Age: " + patient1.get_PatientAge()); System.out.println("Blood Type: " + patient1.getPatient_BloodGroupType()); System.out.println("RH Factor: " + patient1.getPatient_BloodGroupRhFactor()); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
