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

a. Create a class named BloodData that includes fields that hold a blood type (t

ID: 3841043 • Letter: A

Question

a. 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. Save this file as BloodData.java. Create an application named TestBloodData that demonstrates that each method works correctly. Save the application as TestBloodData.java.

b. 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 to “O” and “+”. Create an overloaded constructor that provides values for each field. Also provide get methods for each field. Save the file as Patient.java. Create an application named TestPatient that demonstrates that each method works correctly, and save it as TestPatient.java.

Explanation / Answer

BloodData.java

public class BloodData {
   private String bloodType;
   private String rhFactor;
  
   public BloodData() {
       bloodType = "O";
       rhFactor = "+";
   }
   public BloodData(String bloodType, String rhFactor) {
       this.bloodType = bloodType;
       this.rhFactor = rhFactor;
   }
   public String getBloodType() {
       return bloodType;
   }
   public void setBloodType(String bloodType) {
       this.bloodType = bloodType;
   }
   public String getRhFactor() {
       return rhFactor;
   }
   public void setRhFactor(String rhFactor) {
       this.rhFactor = rhFactor;
   }
   @Override
   public String toString() {
       return "BloodData [bloodType=" + bloodType + ", rhFactor=" + rhFactor + "]";
   }
  
}

TestBloodData.java

public class TestBloodData {

   public static void main(String[] args) {
       BloodData data1=new BloodData();//this create an object with default data
       System.out.println(data1);
       BloodData data2=new BloodData("B","+");
       System.out.println(data2);
      
   }

}

Patient.java

public class Patient {
   private String id;
   private int age;
   private BloodData data;
   public Patient() {
       id ="O";
       age = 0;
       data=new BloodData();
      
   }
   public Patient(String id, int age, String bloodType,String rhFactor) {
       this.id = id;
       this.age = age;
       data = new BloodData(bloodType,rhFactor);
   }
   public String getId() {
       return id;
   }
   public void setId(String id) {
       this.id = id;
   }
   public int getAge() {
       return age;
   }
   public void setAge(int age) {
       this.age = age;
   }
   public BloodData getData() {
       return data;
   }
   public void setData(BloodData data) {
       this.data = data;
   }
   @Override
   public String toString() {
       return "Patient [id=" + id + ", age=" + age + ", data=" + data + "]";
   }
  
}

TestPatient.java

public class TestPatient {

   public static void main(String[] args) {
       Patient patient1=new Patient();
       System.out.println(patient1);
      
       Patient patient2=new Patient("sri001",25,"B","+");
       System.out.println(patient2);
      
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote