Question

In: Computer Science

More Object Concepts (Exercise 4) Part A: Open the file BloodData.java that includes fields that hold...

More Object Concepts (Exercise 4)

Part A:

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 button to demonstrate that each method works correctly.

Part B:

Open the file Patient.java which 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. Open TestPatient.java. Click the Run button at the bottom of your screen to demonstrate that each method works correctly.

BloodData.java

public class BloodData
{
public String bType;
public String rh;
public BloodData()
{
bType = "O";
rh = "+";
}
public BloodData(String bType, String rh)
{
this.bType = bType;
this.rh = rh;
}
public void setBloodType(String bType)
{
this.bType = bType;
}
public String getBloodType()
{
return bType;
}
public void setRhFactor(String rh)
{
this.rh = rh;
}
public String getRhFactor()
{
return rh;
}
}

Patient.java

public class Patient
{
// declare private variables here
public Patient()
{
// add default constructor values here
}
public Patient(String id, int age, String bType, String rhFactor)
{
// add constructor logic here
}
public String getId()
{
// write method code here
}
public void setId(String id)
{
// write method code here
}
public int getAge()
{
// write method code here
}
public void setAge(int age)
{
// write method code here
}
public BloodData getBloodData()
{
// write method code here
}
public void setBloodData(BloodData b)
{
// write method code here
}
}

TestBloodData.java

public class TestBloodData
{
public static void main(String[] args)
{
BloodData b1 = new BloodData();
BloodData b2 = new BloodData("A", "-");
display(b1);
display(b2);
b1.setBloodType("AB");
b1.setRhFactor("-");
display(b1);
}
public static void display(BloodData b)
{
System.out.println("The blood is type " + b.getBloodType() + b.getRhFactor());
}
}

TestPatient.java

public class TestPatient
{
public static void main(String[] args)
{
Patient p1 = new Patient();
Patient p2 = new Patient("1234", 35, "B", "+");
BloodData b2 = new BloodData("A", "-");
display(p1);
display(p2);
p1.setId("3456");
p1.setAge(42);
BloodData b = new BloodData("AB", "-");
p1.setBloodData(b);
display(p1);
}
public static void display(Patient p)
{
BloodData bt = p.getBloodData();
System.out.println("Patient #" + p.getId() + " age: " + + p.getAge() +
"\n The blood is type " + bt.getBloodType() + bt.getRhFactor());
}

}

Solutions

Expert Solution

Please find the answer, all the details are coded as per the instructions given in the question. Both the Test program is executed and output is attached here for the reference.

BloodData.java
public class BloodData {
    //fields
    private String bType;
    private String rh;

    //default constructor
    public BloodData() {
        this.bType = "O";
        this.rh = "+";
    }

    //Overloaded Constructor
    public BloodData(String bType, String rh) {
        this.bType = bType;
        this.rh = rh;
    }

    public String getBloodType() {
        return bType;
    }

    public void setBloodType(String bType) {
        this.bType = bType;
    }

    public String getRhFactor() {
        return rh;
    }

    public void setRhFactor(String rh) {
        this.rh = rh;
    }
}
Patient.java
public class Patient
{
    // declare private variables here
    private String id;
    private int age;
    private BloodData bloodData;

    //default constructor
    public Patient() {
        // add default constructor values here
        this.id = "0";
        this.age = 0;
        this.bloodData = new BloodData();
    }

    //overloaded constructor
    public Patient(String id, int age, String bType, String rhFactor) {
        // add constructor logic here
        this.id = id;
        this.age = age;
        this.bloodData = new BloodData(bType,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 getBloodData() {
        return bloodData;
    }

    public void setBloodData(BloodData bloodData) {
        this.bloodData = bloodData;
    }
}
TestBloodData.java
public class TestBloodData
{
    public static void main(String[] args)
    {
        BloodData b1 = new BloodData();
        BloodData b2 = new BloodData("A", "-");
        display(b1);
        display(b2);
        b1.setBloodType("AB");
        b1.setRhFactor("-");
        display(b1);
    }
    public static void display(BloodData b)
    {
        System.out.println("The blood is type " + b.getBloodType() + b.getRhFactor());
    }
}

Output

TestPatient.java
public class TestPatient
{
    public static void main(String[] args)
    {
        Patient p1 = new Patient();
        Patient p2 = new Patient("1234", 35, "B", "+");
        BloodData b2 = new BloodData("A", "-");
        display(p1);
        display(p2);
        p1.setId("3456");
        p1.setAge(42);
        BloodData b = new BloodData("AB", "-");
        p1.setBloodData(b);
        display(p1);
    }
    public static void display(Patient p)
    {
        BloodData bt = p.getBloodData();
        System.out.println("Patient #" + p.getId() + " age: " + + p.getAge() +
                "\n The blood is type " + bt.getBloodType() + bt.getRhFactor());
    }

}

Output:

Modified class is highlighted. Please let us know in the comments if you face any problems.


Related Solutions

Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG),...
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG), the course number (for example, 101), the credits (for example, 3), and the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data. Create a subclass named LabCourse that adds $50 to the course fee....
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name,...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name, last name, and hourly pay rate (use of string datatype is not allowed). Create an array of five Employee objects. Input the data in the employee array. Write a searchById() function, that ask the user to enter an employee id, if its present, your program should display the employee record, if it isn’t there, simply print the message ”Employee with this ID doesn’t exist”....
Abstraction is a key part of object-oriented programming and the concepts apply particularly well to classes....
Abstraction is a key part of object-oriented programming and the concepts apply particularly well to classes. How would the same concepts apply to data structures and how we tend to define and think of ADTs?
Abstraction is a key part of object-oriented programming and the concepts apply particularly well to classes....
Abstraction is a key part of object-oriented programming and the concepts apply particularly well to classes. How would the same concepts apply to data structures and how we tend to define and think of ADTs?
1.The file name Final.xlsx, sheet named Part 4 is kept blank. Use Part 4 sheet to...
1.The file name Final.xlsx, sheet named Part 4 is kept blank. Use Part 4 sheet to answer this question. Put a box around your answers. Round your answer to 4 decimal places. Information from the American Institute of Insurance indicates the mean amount of life insurance per household in the United States is $110,000 with a standard deviation of $40,000. Assume the population distribution is normal. A random sample of 100 households is taken. (a)What is the probability that sample...
1. Download the EXCEL file: Access Exercise Tables 2. Open a new blank database in ACCESS...
1. Download the EXCEL file: Access Exercise Tables 2. Open a new blank database in ACCESS and name it “Exercise-Your Name” where you replace Your Name with your name. 3. Import each worksheet in the EXCEL file into ACCESS as a separate table as follows: a. External Data Tab -> Import Excel icon b. In the dialog box browse for the destination of the excel file you saved in step 1, it should default to “import the source data in...
Chapter 4 Assignments - Part #1 (Part #2 is on next page) Submit an HTML file...
Chapter 4 Assignments - Part #1 (Part #2 is on next page) Submit an HTML file attached to an email with your web page containing the elements below. - Create a web page with a comment containing your name after this line. o - In the head section, add character encoding. - Add the title "Chapter 4 Web Page #1 - by yourname" - Create a file called myStyles.css as the style sheet for the web page. - Add the...
DIET ANALYSIS PART 4 : NO FILE NEEDS TO BE ATTACHED FOR THIS SECTION. Cannot submit...
DIET ANALYSIS PART 4 : NO FILE NEEDS TO BE ATTACHED FOR THIS SECTION. Cannot submit part 4 if you have not submitted part 1-3. Review SAM: SUBJECT: SAM Age: 39 Birthdate:7/14/1980 Height: 6 feet 2 inches Weight: 312 pounds Waist circumference: 65 inches Sam works at the local Wells Fargo Bank. He is a Branch Manager He works 40 hours per week. Sam spends a lot of his time behind a desk working for his clients. He smokes cigarette...
Exercise 1: probability Concepts Part one: Contingency table and Probability rules A study on speeding violation...
Exercise 1: probability Concepts Part one: Contingency table and Probability rules A study on speeding violation and driver age produced the following contingency table.         Speeding Age speeding violation last year No speeding violation last year 20 -29 95 70 30 -39 101 97 40 -49 70 85 50 -59 45 90 60 -69 19 103 Does it appear that being 60 years of age or more and speed violating are mutually exclusive events? Explain your reasoning If a driver...
Logisim Evolution is a freeware 4. Bit sequence recognizer [30] Submission file for this part: 4.circ...
Logisim Evolution is a freeware 4. Bit sequence recognizer [30] Submission file for this part: 4.circ Main circuit name: sequencecheck Input pin(s): inputx [1], sysclock [1] Output pin(s): outputr [1] Derive a minimal state table for a Mealy model FSM that acts as a sequence checker. During four consecutive clock cycles, a sequence of four values of the signal x is applied, forming a binary number. The oldest value of x would become the most significant bit in that binary...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT