Write a Java application named Lab 06 Java whose main method

Write a Java application named Lab 06. Java, whose main method invokes the two methods written in class on Wednesday of Week 5 and described below. However, before invoking each method, your main method will prompt the user for input that will then be passed to each method. When the methods return their values, main should display the returned value with a proper label. Here is what main will do: Prompt the user for a power of two to be calculated and displayed Read this integer value from the user Call the method, calculated Power of 2, passing if the value just entered by the user Display the value returned by calculate power of 2 with a proper label Prompt the user for a string to be displayed with each character on its own line Read this string value from the user Call the method, vertical, passing if the string value just entered by the user Display the value returned by vertical with a proper label Write method, calculatePowerOf2 - this method accepts an integer as its formal parameter and returns the integer value of 2 raised to the power represented by the value of the format parameter. For example if a 4 is passed to this method, then its return value should be 16 You may not use the Math class in this assignment Write method, vertical - this method accepts a string value as its formal parameter and returns a string that contains each character in the formal parameter with a newline character (\'\ ) following it. For example, if the value of the formal parameter is \'Java\', then the value of the returned string should be \"J\ a\ v\ a\ \". When the caller prints the value returned by this method, each character of the argument will appear on a separate line.

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.Scanner;

public class Lab06 {

  

   public static int calculatePowerOf2(int x){

      

       int power = 1;

       for(int i=1; i<=x; i++)

           power = power*2;

      

       return power;

   }

  

   public static void vertical(String str){

       System.out.println(\"Your string vertically: \");

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

           System.out.println(str.charAt(i));

   }

  

   public static void main(String[] args) {

      

       Scanner sc = new Scanner(System.in);

      

       System.out.print(\"Enter a power for 2: \");

       int n = sc.nextInt();

      

       System.out.println(\"2 raised to the power of \"+n+\" is \"+calculatePowerOf2(n));

      

       System.out.print(\"\ Enter a string to display vertically: \");

       String str = sc.next();

       vertical(str);

   }

}

/*

Sample run:

Enter a power for 2: 5

2 raised to the power of 5 is 32

Enter a string to display vertically: CS1234

Your string vertically:

C

S

1

2

3

4

*/

 Write a Java application named Lab 06. Java, whose main method invokes the two methods written in class on Wednesday of Week 5 and described below. However, be
 Write a Java application named Lab 06. Java, whose main method invokes the two methods written in class on Wednesday of Week 5 and described below. However, be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site