Write a method starString that accepts an integer parameter

Write a method starString that accepts an integer parameter n and returns a string of stars (asterisks) 2^n long (i.3., 2 to the n^th power). You should throw an I11ega1ArgumentException if passed a value less than 0.This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

Solution

Note :I couldnt able to see the symbols .I think those are astrisks.That why I used the astrisk symbol.If the symbols are not correct just comment the symbols.So that I will change.

____________________

AsteriskMarks.java

import java.util.Scanner;

public class AsteriskMarks {

   public static void main(String[] args) {
       //Declaring variables
       int num;
      
       //Scanner class object is sued to read the inputs entered by the user
       Scanner sc = new Scanner(System.in);
      
       /* This loop continue to iterate until
       * the user enters a positive number
       */
       while (true) {
          
           //Getting the Integer Entered by the user
           System.out.print(\"Enter an Integer :\");
           num = sc.nextInt();
           try {
               /* If the user enters a number which is less than zero
               * Throw exception
               */
               if (num < 0) {
                   throw new IllegalArgumentException(\":: Number Must be Greater than Zero ::\");
               }
               else
               {
                   //Calling the method by passing the number as input
                   starString(num);
                   break;
               }
           } catch (IllegalArgumentException IAE) {
               System.out.println(IAE);
               continue;
           }
       }
   }

   /* This method will display the Astrisk
   *based on the User entered number
   */
   private static void starString(int num) {
       for(int i=0;i<Math.pow(2,num);i++)
       {
           System.out.print(\"*\");
       }
      
   }

}

______________________

Output:

Enter an Integer :-5
java.lang.IllegalArgumentException: :: Number Must be Greater than Zero ::
Enter an Integer :5
********************************

_______Thank You

 Write a method starString that accepts an integer parameter n and returns a string of stars (asterisks) 2^n long (i.3., 2 to the n^th power). You should throw
 Write a method starString that accepts an integer parameter n and returns a string of stars (asterisks) 2^n long (i.3., 2 to the n^th power). You should throw

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site