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

Java question Include all of the following work in a single class named Question

ID: 3846016 • Letter: J

Question

Java question

Include all of the following work in a single class named Question1. Use appropriate comments for all custom methods. It is your responsibility to ensure your code compiles before you submit! Write several methods that deal with Strings. Be aware of whether each method is asking you to take input from parameters or take input from the user (using the Scanner class).

a)Write a method called shiftString that shifts a certain number of characters from the end of a String to the front. Your method should take a String and an integer as parameters. The String parameter is the String to be used, and the integer is the number of characters to shift from the end to the beginning of the String. Your method should return a String that corresponds to the parameter String after the appropriate number of characters on the end have been shifted to the beginning, in the same order they originally appeared.

For example, the return value after calling this method with parameters “Java” and 2 should be “vaJa”. The return value after calling this method with parameters “Winston” and 4 should be “stonWin”.

b)Write a method called allShifts that calculates all of the possible back-to-front character shifts for a given String. Your method should take a single String as a parameter and return a String that represents all of the possible shifts of characters (from the end to the front), starting with the original String, and with each shift being represented on a separate line. Your method should not print any output of its own.

For instance, the return value after calling this method with parameter “Java” should be:

Java

aJav

vaJa

avaJ

and after calling with parameter “Nepal” should be:

Nepal

lNepa

alNepa

palNe

epalN

c)Write a method called isPalindrome that determines whether a String value is the same backwards as forwards. Your method should take one String parameter that is the String value to be checked. Your method should return true only if the String parameter consists of the same sequence of characters back to front as it does front to back (ignoring case, in both instances). Otherwise, it should return false.

For example, the return value after calling this method with parameter “Brightspace” should be false, and the return value after calling this method with parameter “avid DiVA” should be true.

d)Write a method called checkForPalindrome that determines whether a String given as input by the user is a palindrome. Your method should not take any parameters, but should use the Scanner class to take input from the user when the method is called. Give the user a meaningful prompt for their input. Your method should return true if the user-inputted String was a palindrome (ignoring case), and false otherwise.

For example, the return value after the user inputs “hello” should be false, and the return value after the user inputs “Anna” should be true.

e)Write a method called flip that reverses the order of the characters in a String. Your method should take a single String as a parameter and return a String that corresponds to the parameter String when written in reverse order.

For example, the return value after calling this method with parameter “Winston” should be “notsniW”.

f)Write a method called mirror that creates a palindrome from a String given as input by the user. Your method should not take any parameters, but should use the Scanner class to take input from the user when the method is called. Give the user a meaningful prompt for their input. Your method should process the String given as input, as return a String that is mirrored around the last letter of the input.

For example, the return value after the user inputs “hello” should be “hellolleh”, and the return value after the user inputs “Java” should be “JavavaJ”.

g)Write a main method that calls the methods from parts (b), (d), and (f) at least once each and prints out a meaningful message for each of their outputs. As an example of a “meaningful” message, your output could look like this after calling the method from part (d):

The input ‘Ana’ is a palindrome: true

Verify that your methods behave as you expect; however, you do not need to submit any output for your program.

Explanation / Answer

Excercises.java

package venkanna;
import java.util.Scanner;
public class Excercises
{
   public static String shiftString(String str, int ch)
   {
       String output="";
       ch= ch%str.length();
       output=str.substring(ch) + str.substring(0, ch);
       return output;
   }
   public static String Allshift(String str)
   {
       return str.charAt(str.length()-1)+str.substring(0, str.length()-1);
   }

   public static boolean isPalindrome(String palin)
   {
       String reverse="";
       int len=palin.length();
       for(int i=len-1;i>-1;i--)
       {
       reverse=reverse+palin.charAt(i);
       }
       if(palin.equalsIgnoreCase(reverse))
       return true;
       else
       return false;
   }
   public static boolean checkForPalindrome(Scanner sc)
   {
       System.out.println("Enter any string For Palindrome Checking:");
           String palin = sc.next();
           String reverse="";
           int len=palin.length();
           for(int i=len-1;i>-1;i--)
           {
           reverse=reverse+palin.charAt(i);
           }
           if(palin.equalsIgnoreCase(reverse))
           return true;
           else
           return false;
   }
   private static String flip(String reverse2)
   {
       char temp[]=reverse2.toCharArray();
       int i=0,j=temp.length-1;
       for(i=0;i<j;i++,j--)
       {
           char ch=temp[i];
           temp[i]=temp[j];
           temp[j]=ch;
       }
       String original=String.valueOf(temp);
       return original;
   }
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String str, output;
       System.out.println("1.Shiftstring Implementaion calling:");
       System.out.println("Enter any string For Shistchecking:");
       str = sc.next();
       System.out.println("Enter The No of Characters Want to Shift");
       int n = sc.nextInt();
       output = shiftString(str, n);
       System.out.println("the resulting String is:" + output);
       System.out.println("2.AllShift Implementaion calling:");
       System.out.println("Enter any string For Allshifthecking:");
       String input = sc.next();
       for(int i = 0; i < input.length(); i++)
       {
       input = Allshift(input);
       System.out.println(input);
       }
       System.out.println("3.Palindrome (with Parameter) Implementaion calling:");
       System.out.println("Enter any string For Checking Palindrome:");
           String palin = sc.next();
       boolean value=isPalindrome(palin);
       if(value==true)
       {
           System.out.println(" String is a palindrome");
       }
       else
       {
           System.out.println("String is not a plaindrome");
       }
       System.out.println("4.Palindrome (with Scanner object) Implementaion calling:");
       boolean value2=checkForPalindrome(sc);
       if(value2==true)
       {
           System.out.println("String is a palindrome");
       }
       else
       {
           System.out.println("String is not a plaindrome");
       }
       System.out.println("5.String Reversing Implementaion calling:");
       System.out.println("Enter any string For Reverse Checking:");
           String reverse2 = sc.next();
       String result=flip(reverse2);
       System.out.println("The Reversing Of String is"+result);
       sc.close();
  
   }
  

}

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