Question

In: Computer Science

PLEASE USE MEHODES (Print part of the string) Write a method with the following header that...

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! ↵

Solutions

Expert Solution

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


Related Solutions

Write a method with the following header to return an array of integer values which are...
Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if the 2D array passed to the method is: 8 5 5 6 6 3 6 5 4 5 2 5 4 5 2 8 8 5 1 6 The method returns an array of integers which are the largest values from each row...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive). public static int countPrimes(int from, int to) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write a program...
Java - Firstly, write a method, using the following header, that counts the number of prime...
Java - Firstly, write a method, using the following header, that counts the number of prime numbers between from and to (inclusive) . public static int countPrimes(int from, int to ) For example, countPrimes(11,19) returns 4 since 11, 13, 17 and 19 are primes. You can assume that someone has already written the following function to determine is an integer is prime or not. public static boolean isPrime(int i) // returns true if i is a prime number Secondly, write...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str)...
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str) that returns the starting position of the first substring of the text that matches str. Return –1 if str is not a substring of the text. For example, s.indexOf("Mississippi", "sip") returns 6. Hint: You must keep track of how far the match is from the beginning of the text. Make that value a parameter variable of a helper method.
Write a method that will have a C++ string passed to it. The string will be...
Write a method that will have a C++ string passed to it. The string will be an email address and the method will return a bool to indicate that the email address is valid. Email Validation Rules: ( These rules are not official.) 1) No whitespace allowed 2) 1 and only 1 @ symbol 3) 1 and only 1 period allowed after the @ symbol. Periods can exist before the @ sign. 4) 3 and only 3 characters after the...
Write a method that will have a C++ string passed to it. The string will be...
Write a method that will have a C++ string passed to it. The string will be an email address and the method will return a bool to indicate that the email address is valid. Email Validation Rules: ( These rules are not official.) 1) No whitespace allowed 2) 1 and only 1 @ symbol 3) 1 and only 1 period allowed after the @ symbol. Periods can exist before the @ sign. 4) 3 and only 3 characters after the...
Can someone please write me a header file in c++ using the quadratic probing hash method...
Can someone please write me a header file in c++ using the quadratic probing hash method that will work with this program? #include "hash.h" #include <algorithm> #include <cstdlib> #include <ctime> #include <iostream> #include <list> using namespace std; const size_t KEYS_COUNT = 1000; // Number of keys to generate for testing string random_key(); class HashCheck { private: size_t count; Hash hash; public: HashCheck(Hash& h) { count = 0; hash = h; } void operator() (const string key) { if (hash[key] !=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT