QuestionQuestion

Transcribed TextTranscribed Text

1. Heart Transplant A heart transplant is a surgical procedure that replaces the person’s heart with a donor heart. A person may require a heart transplant for several reasons including congenital, arterial and muscle diseases or for unforeseen reasons such as accidents or viral infections. The donor heart is matched to the recipient by blood type. Additional variables are also used to decide which recipient receives a heart if there are not enough hearts available for all recipients waiting for a transplant. The variables included in the decision may include the recipient state of health, cause of the heart condition and the urgency of the transplant. The primary goal of this project is for you to write a matching function that receives as a parameter the number of available hearts for transplant and will output all the recipients ranked in order of precedence. Project Classes This project includes several classes. Following is a high level description of each class (more information available in the respective .java file): Person defines a person waiting for a heart transplant. This class is already written for you. SurvivabilityByAge defines the rate of survivability by age after a certain number of years after the transplant. This class is already written for you. SurvivabilityByCause defines the rate of survivability by heart condition cause after a certain number of years after the transplant. This class is already written for you. HeartTransplant is the driver of the heart transplant program, it contains a main method that is already written for you to test the methods that you will write in this class. Data File and Execution We provide a data file with real data that is the input for the program. The data file contains information on 21 people of the same blood type, 24 survivability by age rates, and 15 survivability by heart condition cause rates. More on the data file format below. Execute the program as follows: java HeartTransplant < data.txt The file data.txt is redirected as the input for the program HeartTransplant and can be read using the StdIn library functions StdIn.readInt() and StdIn.readDouble(). HeartTransplant Following are the methods to be completed in HeartTransplant.java: public HeartTransplant() Initializes all instance variables to null. public int addPerson(Person p, int arrayIndex) Inserts the parameter Person p into the instance variable array listOfPatients. Returns the integer 0 if it successfully inserts p into the array, and -1 if there is not enough space to insert p into array. public int readPersonsFromFile(int numberOfLines) Allocates the listOfPatients array with numberOfLines length, then reads numberOfLines persons from the data file (each line refers to one Person). For each person read from the data file (a)instantiates a Person object, and (b) inserts the Person object into the listOfPatients array. The method returns the number of patients read from file. File Format: ID, Ethinicity, Gender, Age, Cause, Urgency, State of health. public int readSurvivabilityRateByAgeFromFile (int numberOfLines) Allocates the survivabilityByAge array with numberOfLines length, then reads numberOfLines survivability by age rates from the data file (each line refers to one survivability rate by age). For each rate read from the data file (a) instantiates a SurvivabilityByAge object, and (b) inserts the object into the survivabilityByAge array. The method returns the number of survivabilities rates read from file. public int readSurvivabilityRateByCauseFromFile (int numberOfLines) Allocates the survivabilityByCause array with numberOfLines length, then reads numberOfLines survivability by cause rates from the data file (each line refers to one survivability rate by cause). For each rate read from the data file (a) instantiates a SurvivabilityByCause object, and (b) inserts the object into the survivabilityByCause array. The method returns the number of survivabilities rates read from file. public Person[] getPatientsWithAgeAbove(int age) Returns a Person array with every Person that has age above the parameter age from the listOfPatients array. The return array has to be completely full with no empty spots, that is the array size should be equal to the number of persons with age above the parameter age. Return null if there is no Person with age above the parameter age. public Person[] getPatientsByStateOfHealth(int state) Returns a Person array with every Person that has the state of health equal to the parameter state from the listOfPatients array. The return array has to be completely full with no empty spots, that is the array size should be equal to the number of persons with the state of health equal to the parameter state. Returns null if there is no Person with the state of health equal to the parameter state. public Person[] getPatientsByHeartConditionCause(int cause) Returns a Person array with every person that has the heart condition cause equal to the parameter cause from the listOfPatients array. The return array has to be completely full with no empty spots, that is the array size should be equal to the number of persons with the heart condition cause equal to the parameter cause. Return null if there is no Person with the heart condition cause equal to the parameter cause. public Person[] match(int numberOfHearts) Assume there are numberOfHearts available for transplantation surgery. Also assume that the hearts are of the same blood type as the persons on the listOfPatients. This method finds a set of persons to be the recepients of these hearts. The method returns a Person array from the listOfPatients array that have the highest potential for survivability after the transplant. The array size is numberOfHearts. If numberOfHeartsAvailable is greater than listOfPatients array size all Persons will receive a transplant. If numberOfHeartsAvailable is smaller than listOfPatients array size find the set of people with the highest potential for survivability. There is no correct solution, you may come up with any set of persons from the listOfPatients array. For you to compare how effective your matching function is we will provide, 5 days before the due date, the data file with the condition of each patient after 3 years of the transplant. Data file where the last column has the condition of each patient 3 years post transplant (Person.java with condition code). 2. Searching, sorting and efficiency analysis For this assignment, you will complete searching/sorting tasks and efficiency analysis. No code is to be written for this assignment. 1. Binary Search. Search for the character S using the binary search algorithm on the following array of char- acters: A E G K M O R S Z For each iteration of binary search use a table similar to the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point of the array, and (d) the character-to-character number of comparisons made during the search (line 09 and line 11 of the Binary Search algorithm). 2. Selection sort. List the resulting array after each iteration of the outer loop of the selection sort algorithm. Indicate the number of character-to-character comparisons made for each iteration (line 7 of the Selection Sort algorithm). Sort the following array of characters (sort into alphabetical order): C Q S A X B T 3. Insertion sort. List the resulting array after each iteration of the outer loop of the insertion sort algorithm. Indicate the number of character-to-character comparisons made for each iteration (line 06 of the Insertion Sort algorithm). Sort the following array of characters (sort into alphabetical order): C Q S A X B T 4. Mergesort. List the resulting array after each iteration of the mergesort algorithm. Indicate the number of character-to-character comparisons made for each call to merge (line 22 of the Merge algorithm). Sort the following array of characters (sort into alphabetical order): C Q S A X B T 5. Efficiency Analysis. For each problem given below, do the following: 1. Create an algorithm in pseudocode to solve the problem. 2. Identify the factors that would influence the running time of your algorithm. For example, if your algorithm is to search an array the factor that influences the running time is the array size. Assign names (such as n) to each factor. 3. Count the operations performed by the algorithm. Express the count as a function of the factors you identified in Step 2. To do that, identify the basic operations of the algorithm. There is no need count every statement separately only the ones that will influence the running time. 4. Describe the best case scenario for the algorithm and derive the big O. 5. Describe the worst case scenario for the algorithm and derive the big O. The problems are: 1. Determine if two arrays have no elements in common. 2. Counting the total number of characters that have a duplicate within a string, including spaces. (i.e. ”gigi the gato” would result in 9 (g*3 + i*2 + t*2+” “*2) 3. Finding a row where every entry is ’x’ in a 2-D array

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

/**
* ***********************************************************************
* Compilation: javac HeartTransplant.java Execution: java HeartTransplant <
* data.txt
*
* @author:
*
************************************************************************
*/
public class HeartTransplant {

    /* ------ Instance variables -------- */
    // Person array, each Person is read from the data file
    private Person[] listOfPatients;

    // SurvivabilityByAge array, each rate is read from data file
    private SurvivabilityByAge[] survivabilityByAge;

    // SurvivabilityByCause array, each rate is read from data file
    private SurvivabilityByCause[] survivabilityByCause;

    /* ------ Constructor -------- */
/*
    * Initializes all instance variables to null.
    */
    public HeartTransplant() {
       // WRITE YOUR CODE HERE
       listOfPatients = null;
       survivabilityByAge = null;
       survivabilityByCause = null;
    }

    /* ------ Methods -------- */

/*
    * Inserts Person p into listOfPatients
    *
    * Returns: 0 if successfully inserts p into array,
    *          -1 if there is not enough space to insert p into array
    */
    public int addPerson(Person p, int arrayIndex) {
       // WRITE YOUR CODE HERE
       if (listOfPatients == null
                || arrayIndex < 0
                || arrayIndex >= listOfPatients.length
                || listOfPatients[arrayIndex] != null) {
            return -1;
       }
       listOfPatients[arrayIndex] = p;
       return 0;
    }

    /*
    * 1) Creates the listOfPatients array with numberOfLines length.
    *
    * 2) Reads from the command line data file.
    *    File Format: ID, Ethinicity, Gender, Age, Cause, Urgency, State of health
    *    Each line refers to one Person.
    *
    * 3) Inserts each person from file into listOfPatients
    *    Hint: uses addPerson() method
    *
    * Returns the number of patients read from file
    */
    public int readPersonsFromFile(int numberOfLines) {

       // WRITE YOUR CODE HERE
       listOfPatients = new Person[numberOfLines];
       int num = 0;
       for (int i = 0; i < listOfPatients.length
                && StdIn.hasNextChar(); i++) {
            if (addPerson(new Person(StdIn.readInt(), StdIn.readInt(),
                   StdIn.readInt(), StdIn.readInt(), StdIn.readInt(),
                   StdIn.readInt(), StdIn.readInt()), i) < 0) {
                break;
            }
            num++;
       }
       return num;
    }

    /*
    * 1) Creates the survivabilityByAge array with numberOfLines length.
    *
    * 2) Reads from the command line file.
    *    File Format: Age YearsPostTransplant Rate
    *    Each line refers to one survivability rate by age.
    *
    * 3) Inserts each rate from file into the survivabilityByAge array
    *
    * Returns the number of survivabilities rates read from file
    */
    public int readSurvivabilityRateByAgeFromFile(int numberOfLines) {

       // WRITE YOUR CODE HERE
       survivabilityByAge = new SurvivabilityByAge[numberOfLines];
       int num = 0;
       for (int i = 0; i < survivabilityByAge.length
                && StdIn.hasNextChar(); i++) {
            survivabilityByAge[i] = new SurvivabilityByAge(StdIn.readInt(),
                   StdIn.readInt(), StdIn.readDouble());
            num++;
       }
       return num;
    }

    /*
    * 1) Creates the survivabilityByCause array with numberOfLines length.
    *
    * 2) Reads from the command line file.
    *    File Format: Cause YearsPostTransplant Rate
    *    Each line refers to one survivability rate by cause.
    *
    * 3) Inserts each rate from file into the survivabilityByCause array
    *
    * Returns the number of survivabilities rates read from file
    */
    public int readSurvivabilityRateByCauseFromFile(int numberOfLines) {
       // WRITE YOUR CODE HERE
       survivabilityByCause = new SurvivabilityByCause[numberOfLines];
       int num = 0;
       for (int i = 0; i < survivabilityByCause.length
                && StdIn.hasNextChar(); i++) {
            survivabilityByCause[i] = new SurvivabilityByCause(StdIn.readInt(),
                   StdIn.readInt(), StdIn.readDouble());
            num++;
       }
       return num;
    }...

By purchasing this solution you'll be able to access the following files:
Solution1.zip and Solution2.docx.

$55.76
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats