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

On a sheet of paper, write a method called square that will: Take an integer val

ID: 3921715 • Letter: O

Question

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)

Explanation / Answer

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote