In Java Define a class quizQuestion In this class define a m

In Java.

Define a class quiz.Question. In this class define a method named answer.

Define this method so that it returns a String consisting of the all the integers from 0 to n inclusive, comma-seperated, if n >= 0, and the String \"0\" otherwise.

For example:

answer (-1) must return \"0\"

answer (0) must return \"0\"

answer (3) must return \"0, 1, 2, 3\"

Solution

Question.java


public class Question {
  
   public String answer(int n){
   String s = \"\";
       if(n<0){
           s = \"0\";
       }
       else{
           for(int i=0; i<=n; i++){
               if(i==n){
                   s = s + i;
               }
               else{
                   s = s + i +\", \";
               }
           }
       }
   return s;
   }
  
}

QuestionTest.java


public class QuestionTest {

  
   public static void main(String[] args) {
       Question q = new Question();
       System.out.println(q.answer (-1) );
       System.out.println(q.answer (0) );
       System.out.println(q.answer (3) );
   }

}

Output:

0
0
0, 1, 2, 3

In Java. Define a class quiz.Question. In this class define a method named answer. Define this method so that it returns a String consisting of the all the inte

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site