In: Computer Science
PLEASE USE MEHODES
(Print part of the string)
Write a method with the following header that returns a partial
string:
public static String getPartOfString(int n, String firstOrLast,
String inWord)
Write a test program that uses this method to display the first or
last number of characters of a string provided by the user.
The program should output an error if the number requested is
larger than the string.
SAMPLE RUN #1: java PartOfString
Enter a string: abracadabra↵
Enter the number of characters to display: 4 ↵
Enter first or last: first ↵
The first 4 letters are abra ↵
SAMPLE RUN #2: java PartOfString
Enter a string: abracadabra↵
Enter the number of characters to display: 5 ↵
Enter first or last: last ↵
The last 5 letters are dabra ↵
SAMPLE RUN #3: java PartOfString
Enter a string: abracadabra↵
Enter the number of characters to display: 25 ↵
Error The requested size is too large! ↵
import java.util.Scanner;
public class PartOfString {
   public static void main(String[] args) {
       Scanner sc = new
Scanner(System.in);
       System.out.print("Enter a string:
");
       String str = sc.next();
       System.out.print("Enter number of
characters to display: ");
       int n = sc.nextInt();
       if (str.length() < n) {
          
System.out.println("Error The requested size is too large!");
           return;
       }
       System.out.println("Enter first or
last: ");
       String s = sc.next();
      
       if (s.equalsIgnoreCase("first"))
{
          
System.out.println("The first " + n + " letters are " +
str.substring(0, n));
       } else {
          
System.out.println("The last " + n + " letters are " +
str.substring(str.length() - n));
       }
   }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME