Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a method that accepts a String as an input; the parameter that the meth

ID: 3829757 • Letter: 1

Question

1. Write a method that accepts a String as an input; the parameter that the method is receiving MUST be in such a form ("1,3,33,55,66,-2"). If the String is not in that form (meaning integers separated with comma, or has a Letter instead of digits or a character other than comma (,) you should display the string "Error Format" and return -999. If the form of the string is Okay, calculate the integers and returns the Sum. For example the return of the sample String above would be: 1+3+33+55+66+(-2) = 156 2. write a method that accepts an integer between 0 and 20 and return the string representation of the number. For example, 18 the program should return Eighteen. Or 15 the program should return Fifteen. If the number is less than 0 or greater than 20 you should display the string "Error Format". The method MUST deal with arrays.

Explanation / Answer

Please find my implementation.

public class StringIntSum {

   public static int getSumStringInt(String s){

      

       try{

          

           String strArr[] = s.split(",");

          

           int sum = 0;

           for(int i=0; i<strArr.length; i++){

              

               int num = Integer.parseInt(strArr[i].trim());

               sum = sum + num;

           }

          

           return sum;

          

       }catch(Exception e){

           System.out.println("Error Forma");

           return -999;

       }

   }

  

   public static String getStringRepresentation(int x){

      

       String[] str = {"zero", "one", "two", "three", "four", "five","six","seven","eight","nine","ten",

               "eleven","twelve","thiteen","fouteen","fifteen","sixteen","seventeen","eighteen","ninteen","twenty"};

      

       if(x >=0 && x <=20)

           return str[x];

       else

           return "Out of range";

   }

  

   public static void main(String[] args) {

      

       String s = "1,3,33,55,66,-2";

       System.out.println("1,3,33,55,66,-2: "+getSumStringInt(s));

      

       System.out.println("string rep (15): "+getStringRepresentation(15));

   }

}

/*

Sample run:

1,3,33,55,66,-2: 156

string rep (15): fifteen

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote