Create a class named BloodData that includes fields that hold a blood type (the
ID: 671838 • Letter: C
Question
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 each method works correctly. Save the
application asTestBloodData.java.
Explanation / Answer
BloodData.java
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;
}
}
TestBloodData.java
public class TestBloodData
{
public static void main(String[] args)
{
BloodData test001 = new BloodData();
System.out.println("Blood Type: " + test001.getBloodType());
System.out.println("RH Factor: " + test001.getRhFactor());
BloodData test002 = new BloodData();
test001.setBloodType("A");
test002.setRhFactor('-');
System.out.println();
System.out.println("Blood Type: " + test002.getBloodType());
System.out.println("RH Factor: " + test002.getRhFactor());
System.out.println();
BloodData test003 = new BloodData("B", '+');
System.out.println("Blood Type: " + test003.getBloodType());
System.out.println("RH Factor: " + test003.getRhFactor());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.