On a sheet of paper write a method called square that will T

On a sheet of paper, write a method called square that will:

Take an integer value as a parameter

Return the square of the parameter (for example, the square of 4 is 4*4)

Write another method called countA that will:

Take a String value as a parameter

Return the count of how many \'a\' letters are in the parameter (for example, there are three \'a\' letters in \"banana\")

Assume you are in the same class as the methods above, and write a main method where you:

Create a String variable called str and initialize is to \"banana\"

Call your countA method above, passing str as the parameter. Store the result in a new variable called count.

Call your square method above, passing count as the parameter. Store the result in a new variable called sq.

Print the value of sq.

(Code must be compatible with BlueJ)

Solution

Test15.java


public class Test15 {

   public static void main(String[] args) {
       String str = \"banana\";
       int count = countA(str);
       System.out.println(\"A count is \"+count);
       int sq = square(count );
       System.out.println(\"Square is \"+sq);

   }
   public static int square (int number){
       return number * number;
   }
   public static int countA (String s){
       int count = 0;
       for(int i=0; i<s.length(); i++){
           if(s.charAt(i) == \'a\' || s.charAt(i) == \'A\'){
               count++;
           }
       }
       return count;
   }

}

Output:

A count is 3
Square is 9

On a sheet of paper, write a method called square that will: Take an integer value as a parameter Return the square of the parameter (for example, the square of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site