Java Help a. create a class named BloodData that includes fields that hold a blo
ID: 3870883 • Letter: J
Question
Java Help
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 BloodData.java. Create an application named TestBloodData that demonstrates 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 that demonstrates that each method works correctly, and save it as TestPatient.java.
Explanation / Answer
BloodData.java
public class BloodData {
// Declaring instance variables
private char bloodType;
private char Rhfactor;
// Zero argumented constructor
public BloodData() {
bloodType = 'O';
Rhfactor = '+';
}
// Parameterized constructor
public BloodData(char bloodType, char rhfactor) {
super();
this.bloodType = bloodType;
Rhfactor = rhfactor;
}
// getters and setters
public char getBloodType() {
return bloodType;
}
public void setBloodType(char bloodType) {
this.bloodType = bloodType;
}
public char getRhfactor() {
return Rhfactor;
}
public void setRhfactor(char rhfactor) {
Rhfactor = rhfactor;
}
}
___________________
TestBloodData.java
public class TestBloodData {
public static void main(String[] args) {
//Creating an BloodData class object
BloodData bd = new BloodData('A', '-');
//calling the methods on the BloodData class object
System.out.println(bd.getBloodType() + " " + bd.getRhfactor());
}
}
___________________
Output:
A -
___________________
2)
Patient.java
public class Patient {
//Declaring instance variables
private int id;
private int age;
BloodData bd;
//Zero argumented constructor
public Patient() {
this.id = 0;
this.age = 0;
this.bd = new BloodData('O', '+');
}
//Parameterized constructor
public Patient(int id, int age, BloodData bd) {
super();
this.id = id;
this.age = age;
this.bd = bd;
}
//getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public BloodData getBd() {
return bd;
}
public void setBd(BloodData bd) {
this.bd = bd;
}
}
_____________________
TestPatient.java
public class TestPatient {
public static void main(String[] args) {
//Creating BloodData class object
BloodData bd = new BloodData('A', '-');
//Creating Patient class object
Patient p = new Patient(111, 25, bd);
//Displaying the info by calling the methods on the Patient class object
System.out.println("Id :" + p.getId());
System.out.println("Age :" + p.getAge());
System.out.println("Blood Type :" + p.getBd().getBloodType());
System.out.println("Rh Factor :" + p.getBd().getRhfactor());
}
}
______________________
Output:
Id :111
Age :25
Blood Type :A
Rh Factor :-
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.