'How do I fix "constructor SurvivabilityByAge in class SurvivabilityByAge cannot be applied to given types" [duplicate]
Here's the code:
public class HeartTransplant {
// patient array, each Patient is read from the data file
private Patient[] patients;
// SurvivabilityByAge array, each rate is read from data file
private SurvivabilityByAge survivabilityByAge;
// SurvivabilityByCause array, each rate is read from data file
private SurvivabilityByCause survivabilityByCause;
/*
* Default constructor
* Initializes patients to null.
* Initializes survivabilityByAge to null.
* Initializes survivabilityByCause to null.
*/
public HeartTransplant() {
this.survivabilityByAge = null;
this.survivabilityByCause = null;
this.patients = null;
}
/*
* Returns patients
*/
public Patient[] getPatients() {
return patients;
}
/*
* Returns survivabilityByAge
*/
public SurvivabilityByAge getSurvivabilityByAge() {
return survivabilityByAge;
}
/*
* Returns survivabilityByCause
*/
public SurvivabilityByCause getSurvivabilityByCause() {
return survivabilityByCause;
}
/*
* 1) Initialize the instance variable patients array with numberOfLines length.
*
* 2) Reads from the command line data file, use StdIn.readInt() to read an integer.
* File Format:
* ID, ethnicity, Gender, Age, Cause, Urgency, State of health
*
* Each line refers to one Patient, all values are integers.
*
*/
public void readPatients (int numberOfLines) {
// Initializing array with numberofLines length
patients = new Patient[numberOfLines];
for(int i = 0; i < numberOfLines; i++){
// Reading data and filling up the array
patients[i] = new Patient(StdIn.readInt(), StdIn.readInt(), StdIn.readInt(),
StdIn.readInt(), StdIn.readInt(), StdIn.readInt(), StdIn.readInt());
}
}
/*
* 1) Initialize the instance variable survivabilityByAge with a new survivabilityByAge object.
*
* 2) Reads from the command line file to populate the object.
* Use StdIn.readInt() to read an integer and StdIn.readDouble() to read a double.
*
* File Format: Age YearsPostTransplant Rate
* Each line refers to one survivability rate by age.
*
*/
public void readSurvivabilityByAge (int numberOfLines) {
// Initializes instance variable
survivabilityByAge = new SurvivabilityByAge();
// Populating object with data
for(int i = 0; i < numberOfLines; i++){
survivabilityByAge = new SurvivabilityByAge(StdIn.readInt(), StdIn.readInt(), StdIn.readDouble());
}
}
/*
* 1) Initialize the instance variable survivabilityByCause with a new survivabilityByCause object.
*
* 2) Reads from the command line file to populate the object. Use StdIn.readInt() to read an
* integer and StdIn.readDouble() to read a double.
*
* File Format: Cause YearsPostTransplant Rate
* Each line refers to one survivability rate by cause.
*
*/
public void readSurvivabilityByCause (int numberOfLines) {
// Intializing instance variable
survivabilityByCause = new SurvivabilityByCause();
// Populating object with data
for(int i = 0; i < numberOfLines; i++){
survivabilityByCause = new SurvivabilityByCause(StdIn.readInt(), StdIn.readInt(), StdIn.readDouble());
}
}
/*
* Returns a Patient array containing the patients,
* from the patients array, that have age above the parameter age.
*
* The return array has to be completely full with no empty
* spots, that is the array size should be equal to the number
* of Patients with age above the parameter age.
*
* Return null if there is no Patient with age above the
* parameter age.
*/
public Patient[] getPatientsWithAgeAbove(int age) {
int[] containingPatients = new int[patients.length()];
for(int i = 0; i < patients.length; i++){
if(patients[i].getAge() >= age){
containingPatients[i] = patients[i];
}
else {
containingPatients[i] = null;
}
}
}
/*
* Returns a Patient array containing the patients, from the patients array,
* that have the heart condition cause equal to the parameter cause.
*
* The return array has to be completely full with no empty
* spots, that is the array size should be equal to the number
* of Patients with the heart condition cause equal to the parameter cause.
*
* Return null if there is no Patient with the heart condition cause
* equal to the parameter cause.
*/
public Patient[] getPatientsByHeartConditionCause(int cause) {
int[] containingPatients = new int[patients.length()];
for(int i = 0; i < patients.length; i++) {
if(patients[i].getCause() == cause) {
containingPatients[i] = patients[i];
}
else {
containingPatients[i] = null;
}
}
}
/*
* Returns a Patient array containing patients, from the patients array,
* that have the state of health equal to the parameter state.
*
* The return array has to be completely full with no empty
* spots, that is the array size should be equal to the number
* of Patients with the state of health equal to the parameter state.
*
* Return null if there is no Patient with the state of health
* equal to the parameter state.
*/
public Patient[] getPatientsByUrgency(int urgency) {
int[] containingPatients = new int[patients.length()];
for(int i = 0; i < patients.length; i++) {
if(patients[i].getUrgency() == urgency) {
containingPatients[i] = patients[i];
}
else {
containingPatients[i] = null;
}
}
}
/*
* Assume there is a heart available for transplantation surgery.
* Also assume that the heart is of the same blood type as the
* Patients on the patients array.
* This method finds the Patient to be the recepient of this
* heart.
*
* The method returns a Patient from the patients array with
* he highest potential for survivability after the transplant.
*
* Assume the patient returned by this method will receive a heart,
* therefore the Patient will no longer need a heart.
*
* There is no correct solution, you may come up with any
* function to find the patient with the highest potential
* for survivability after the transplant.
*/
public Patient getPatientForTransplant () {
patient[] containingPatients = getPatientsByUrgency(URGENCY_EXTREME);
if(containingPatients.length == 0) {
containingPatients = getPatientsByUrgency(URGENCY_MODERATE);
}
for(int i = 0; i < containingPatients.length; i++) {
if(containingPatients[i].getNeedHeart() == false) {
containingPatients[i] = null;
}
}
for(int i = 0; i < containingPatients.length; i++) {
if(containingPatients[i].getStateOfHealth() == HEALTH_EXCELLENT) {
containingPatients[i].setNeedHeart(true);
return containingPatients[i];
}
}
for(int i = 0; i < containingPatients.length; i++) {
if(containingPatients[i].getStateOfHealth() == HEALTH_EXCELLENT) {
containingPatients[i].setNeedHeart(true);
return containingPatients[i];
}
}
for(int i = 0; i < containingPatients.length; i++) {
if(containingPatients[i].getStateOfHealth() == HEALTH_GOOD) {
containingPatients[i].setNeedHeart(true);
return containingPatients[i];
}
}
for(int i = 0; i < containingPatients.length; i++) {
if(containingPatients[i].getStateOfHealth() == HEALTH_POOR) {
containingPatients[i].setNeedHeart(true);
return containingPatients[i];
}
}
}
}
These are the errors:
./HeartTransplant.java:88: error: constructor SurvivabilityByAge in class SurvivabilityByAge cannot be applied to given types;
survivabilityByAge = new SurvivabilityByAge(StdIn.readInt(), StdIn.readInt(), StdIn.readDouble());
^
required: no arguments
found: int,int,double
reason: actual and formal argument lists differ in length
./HeartTransplant.java:108: error: constructor SurvivabilityByCause in class SurvivabilityByCause cannot be applied to given types;
survivabilityByCause = new SurvivabilityByCause(StdIn.readInt(), StdIn.readInt(), StdIn.readDouble());
^
required: no arguments
found: int,int,double
reason: actual and formal argument lists differ in length
./HeartTransplant.java:124: error: cannot find symbol
int[] containingPatients = new int[patients.length()];
^
symbol: method length()
location: variable patients of type Patient[]
./HeartTransplant.java:128: error: incompatible types: Patient[] cannot be converted to int
containingPatients[i] = patients;
^
./HeartTransplant.java:131: error: incompatible types: <null> cannot be converted to int
containingPatients[i] = null;
^
./HeartTransplant.java:148: error: cannot find symbol
int[] containtingPatients = new int[patients.length()];
^
symbol: method length()
location: variable patients of type Patient[]
./HeartTransplant.java:151: error: incompatible types: Patient cannot be converted to int
containingPatients[i] = patients[i];
^
./HeartTransplant.java:154: error: incompatible types: <null> cannot be converted to int
containingPatients[i] = null;
^
./HeartTransplant.java:171: error: cannot find symbol
int[] containingPatients = new int[patients.length()];
^
symbol: method length()
location: variable patients of type Patient[]
./HeartTransplant.java:174: error: incompatible types: Patient cannot be converted to int
containingPatients[i] = patients[i];
^
./HeartTransplant.java:177: error: incompatible types: <null> cannot be converted to int
containingPatients[i] = null;
^
./HeartTransplant.java:200: error: cannot find symbol
patient[] containingPatients = getPatientsByUrgency(URGENCY_EXTREME);
^
symbol: class patient
location: class HeartTransplant
./HeartTransplant.java:200: error: cannot find symbol
patient[] containingPatients = getPatientsByUrgency(URGENCY_EXTREME);
^
symbol: variable URGENCY_EXTREME
location: class HeartTransplant
./HeartTransplant.java:203: error: cannot find symbol
containingPatients = getPatientsByUrgency(URGENCY_MODERATE);
^
symbol: variable URGENCY_MODERATE
location: class HeartTransplant
./HeartTransplant.java:213: error: cannot find symbol
if(containingPatients[i].getStateOfHealth() == HEALTH_EXCELLENT) {
^
symbol: variable HEALTH_EXCELLENT
location: class HeartTransplant
./HeartTransplant.java:220: error: cannot find symbol
if(containingPatients[i].getStateOfHealth() == HEALTH_EXCELLENT) {
^
symbol: variable HEALTH_EXCELLENT
location: class HeartTransplant
./HeartTransplant.java:227: error: cannot find symbol
if(containingPatients[i].getStateOfHealth() == HEALTH_GOOD) {
^
symbol: variable HEALTH_GOOD
location: class HeartTransplant
./HeartTransplant.java:234: error: cannot find symbol
if(containingPatients[i].getStateOfHealth() == HEALTH_POOR) {
^
symbol: variable HEALTH_POOR
location: class HeartTransplant
18 errors
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
