please read carefully I need two classes on each sub part a class file and a Tes
ID: 671813 • Letter: P
Question
please read carefully I need two classes on each sub part a class file and a Test file with the test file containing the main method and objects for the class file. (please make sure code works have already posted question once) Thanks in advance.
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 t hat 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
a)
BloodData.java
package javaApp;
//class name
public class BloodData
{
private String bloodType;
private String rhFactor;
BloodData()
{
//default constructor
bloodType = "O";
rhFactor= "+";
}
BloodData(String bloodType1, String rhFactor1)
{
//Parameterized constructor
bloodType = bloodType1;
rhFactor = rhFactor1;
}
//get method
public String getbloodType()
{
return bloodType;
}
//set method
public void setbloodType(String bloodType )
{
this.bloodType = bloodType;
}
//get method
public String getrhFactor() {
return rhFactor;
}
//set method
public void rhFactor(String rhFactor )
{
this.rhFactor = rhFactor;
}
}
TestBloodData.java
package javaApp;
public class TestBloodData extends BloodData
{
public static void main(String args[]){
BloodData bData=new BloodData();
System.out.println("Calling of Default Constructor");
System.out.println("Blood type is:"+bData.getbloodType());
System.out.println("RhFactor is:"+bData.getrhFactor());
BloodData bData1=new BloodData("AB","+");
System.out.println("Calling of Parameterized Constructor");
System.out.println("Blood type is:"+bData1.getbloodType());
System.out.println("RhFactor is:"+bData1.getrhFactor());
}
}
Sample Output:
Calling of Default Constructor
Blood type is:O
RhFactor is:+
Calling of Parameterized Constructor
Blood type is:AB
RhFactor is:+
b)
Pateint.java
package javApp2;
public class Patient
{
private int idNumber;
int age;
private String bloodType;
private String rhFactor;
Patient()
{
idNumber=0;
age=0;
bloodType = "O";
rhFactor= "+";
}
Patient(int idNumber1,int age1,String bloodType1, String rhFactor1)
{
//Parameterized constructor
idNumber=idNumber1;
age=age1;
bloodType = bloodType1;
rhFactor = rhFactor1;
}
//get method
public int getidNumber()
{
return idNumber;
}
//set method
public void setidNumber(int idNumber )
{
this.idNumber = idNumber;
}
//get method
public int getage()
{
return age;
}
//set method
public void setage(int age )
{
this.age = age;
}
public String getbloodType()
{
return bloodType;
}
//set method
public void setbloodType(String bloodType )
{
this.bloodType = bloodType;
}
//get method
public String getrhFactor()
{
return rhFactor;
}
//set method
public void rhFactor(String rhFactor )
{
this.rhFactor = rhFactor;
}
}
TestPatient.java
package javApp2;
public class TestPatient extends Patient
{
public static void main(String[] args)
{
Patient patObj=new Patient();
System.out.println("Calling of Default Constructor");
System.out.println("idNumber is:"+patObj.getidNumber());
System.out.println("Age is:"+patObj.getage());
System.out.println("Blood type is:"+patObj.getbloodType());
System.out.println("Rhfactor is:"+patObj.getrhFactor());
Patient patObj1=new Patient(1,25,"B","+");
System.out.println("Calling of Parameterized Constructor");
System.out.println("idNumber is:"+patObj1.getidNumber());
System.out.println("Age is:"+patObj1.getage());
System.out.println("Blood type is:"+patObj1.getbloodType());
System.out.println("Rhfactor is:"+patObj1.getrhFactor());
}
}
Sample Output:
Calling of Default Constructor
idNumber is:0
Age is:0
Blood type is:O
Rhfactor is:+
Calling of Parameterized Constructor
idNumber is:1
Age is:25
Blood type is:B
Rhfactor is:+
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.