Open the file BloodData.java that includes fields that hold a blood type (the fo
ID: 3699548 • Letter: O
Question
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.
BloodDatajavaPatient.java TestBloodData.java TestPatient.java 1 public class BloodData 3/ declare private variables here 4 private String bloodType; 5 private String rhFactor; 6 7 public BloodData() // add default constructor values here bloodType"O"; 10 rhFactor"+"; 13 public BloodData(String bType, String rh) // add constructor logic here bloodTypebType; 15 16 17 18 19 20 public void setBloodType(String bType) 21 // add method code here this.bloodType bType; 23 25 public String getBloodType) 26 27 28 29 30 public void setRhFactor (String rh) // add method code here return bloodType; // add method code here this .rhFactor = rh; public String getRhFactor() 35 36 37 38 39 40 // add method code here return rhFactorExplanation / Answer
Solution:
--> you didn't use 'this' keyword properly. that's why it is creating issuses.
--> below changes will execute properly for this class.
Source Code(BloodData.java):
public class BloodData {
private String bloodType;
private String rhFactor;
public BloodData(){
//use 'this' keyword here in order to store the member values for that current object(current context)
this.bloodType = "O";
this.rhFactor = "+";
}
public BloodData(String bType, String rh){
//here also use 'this' keyword
this.bloodType = bType;
this.rhFactor = rh;
}
public void setBloodType(String bType){
this.bloodType = bType;
}
public String getBloodType(){
// when returning also use 'this' keyword
return this.bloodType;
}
public void setRhFactor(String rh){
this.rhFactor = rh;
}
public String getRhFactor(){
// here also use 'this' keyword
return this.rhFactor;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.