Create a class called Backward and include in it an instance variable named char
ID: 3629664 • Letter: C
Question
Create a class called Backward and include in it an instance variable named charsIn that is an array of 80 characters and an instance variable charsOut that is also an array of 80 characters.Create a regular (non-static) method reverse that takes whatever is stored in the instance variable charsIn and stores the exact reverse of it in charsOut.
In a main method, ask the user to enter up to 80 characters, store them in charsIn, invoke reverse(), and then print whatever is in charsOut.
Assuming you have written everything correctly, your output should look like this:
Welcome to the Talking Backward Program
Please enter from 1 to 80 characters.
> This is a test of reverse.
The reverse is
> .esrever fo tset a si sihT
Goodbye.
Explanation / Answer
please rate - thanks
import java.util.Scanner;
public class Backward
{
public static void main(String[] args)
{int i,j;
Scanner in = new Scanner(System.in);
char []charsIn=new char[80];
char []charsOut=new char[80];
String input;
System.out.println("Welcome to the Talking Backward Program");
System.out.println("Please enter from 1 to 80 characters. ");
input=in.nextLine();
for(i=0;i<input.length();i++)
charsIn[i]=input.charAt(i);
reverse(charsIn,charsOut,i);
System.out.println("The reverse is");
for(j=0;j<i;j++)
System.out.print(charsOut[j]);
System.out.println();
System.out.println("Goodbye.");
}
public static void reverse(char a[],char b[],int n)
{int i;
for(i=0;i<n;i++)
b[n-i-1]=a[i];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.