Amanda Amanda - 3 months ago 26
Java Question

Constructor Java Programming

The Survey class will have a static class variable that stores the current respondent’s ID. As respondents complete the survey, this value will be incremented by one.
The Survey class will have an instance variable to hold the title of the survey.
The Survey class should have two overloaded constructors.
The first constructor should take no arguments. It should set the survey title to a default value of “Customer Survey”.
The second constructor should accept a String value for the survey title. It should reset the static respondent ID to zero. It should set the survey title to the value passed into the constructor.
The survey class should have a generateRespondentId() method which returns the next value of the respondent ID. This method should increment the static instance variable for the respondent ID by one.

public class Survey {
String surveyTitle = "";

static int respondentID = 0;

static int generateRespondentID(){
return ++respondentID;
}

public Survey(){
surveyTitle = "Customer Survey";
}

public Survey(String newSurveyTitle){
respondentID=0;

}

public static void main(String[] args) {
Survey s = new Survey();
System.out.println(s.getSurveyTitle());
System.out.println(s.generateRespondentID());
System.out.println();

Survey s2 = new Survey("My Survey");
System.out.println(s2.getSurveyTitle());
System.out.println(s2.generateRespondentID());
System.out.println();
System.out.println(s2.generateRespondentID());
System.out.println();
}
}


This is what I have so far. I am struggling with the second constructor

Answer

According to your requirement the following code below should work.

public Survey(String newSurveyTitle){
    respondentID=0;
    SurveyTitle = newSurveyTitle;
 }

You were missing the code for setting the title of the survey in the constructor.