For this lab you will write two programs with a main method and one String metho
ID: 3760631 • Letter: F
Question
For this lab you will write two programs with a main method and one String method. The main method handles all the user input and all the println statements.
1. Write a program asks the user to his or her first and last name all on one line. The main method should call the changeNameFormat method described below and print out the results from that method.
The method changeNameFormat takes a String which is a name like this
John Smith
and returns a String which is the name like this
Smith, John
The output should look similar to this....
Enter your name:
John Smith
Your name for our system will be Smith, John.
2. Write a program that asks the user to input a string. The main method should call the palindrome() method that takes a String and returns true if the string is a palindrome else returns false. Palindrome is a word that reads the same forward or reversed eg: rotor, radar, pop etc.
The output should look similar to this...
Enter a word:
radar
radar is a palindrome
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Conversion
{
public static void changeNameFormat(String first,String last)
{
System.out.println("Your name for our system will be "+last+", "+first);
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner input = new Scanner(System.in);
System.out.println("Enter your first name followed by last name : ");
String line = input.nextLine();
String first = line.split(" ")[0];
String last = line.split(" ")[1];
changeNameFormat(first,last);
}
}
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Palindrome
{
public static void palindrome(String line)
{
int len = line.length();
String reverse = "";
for ( int i = len - 1; i >= 0; i-- )
reverse = reverse + line.charAt(i);
if(line.equals(reverse))
System.out.println(line+" is Palindrome");
else
System.out.println(line+" is not Palindrome");
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner input = new Scanner(System.in);
System.out.println("Enter a string:");
String line = input.nextLine();
palindrome(line);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.