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

In Project#3 you read in lines from a file and tested each to see if it was a pa

ID: 3904078 • Letter: I

Question

In Project#3 you read in lines from a file and tested each to see if it was a palindrome or not. In this Lab you will take your working solution from that assignment and transplant your code into a boolean method named isPalindrome(). As usual you are given a starter file with the main written. This time you will add your code inside the isPalindrome methods that is just an empty shell.

The expected output for Lab#4 is IDENTICAL to what Project#3 produced. The only difference is that your main program will call your boolean method isPalindrome() to determine whether the line is to be printed or not.

// Lab4.java Starter File Uses a method to print only palindromes from a text file

import java.io.*; // BufferedReader

import java.util.*; // Scanner

public class Lab4

{

public static void main (String args[]) throws Exception // i.e. the input file you put on cmd line is not in directory

{

// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE

if (args.length < 1 )

{

System.out.println(" usage: C:\> java Lab4 <input filename> "); // i.e. C:> java Lab4 L4input.txt

System.exit(0);

}

Scanner infile = new Scanner( new File(args[0]) ); // we read our text file line by line

while( infile.hasNextLine() )

{

String line = infile.nextLine();

if ( isPalindrome(line) )

System.out.println(line);

}

infile.close();

} // END MAIN

// RETURNs true if and only if the string passed in is a palindrome

static boolean isPalindrome( String line )

{

CODE GOES HERE

return false; // Just to make it compile. You change as needed

}

} // END LAB4 CLASS

Command Prompt C: Users imDesktoplab-04 solution>java Lab4 L4input.txt Madam I mAdam Stanley Yelnats wow radar wow palindromic cimordnilap C: Users imDesktoplab-04solution»-

Explanation / Answer

Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments

public class Lab4

{

public static void main(String args[]) throws Exception // i.e. the input

// file you put on

// cmd line is not

// in directory

{

// ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE

// COMMAND LINE

if (args.length < 1)

{

System.out.println(" usage: C:\> java Lab4 <input filename> "); // i.e.

// C:>

// java

// Lab4

// L4input.txt

System.exit(0);

}

Scanner infile = new Scanner(new File(args[0])); // we read our text

// file line by line

while (infile.hasNextLine())

{

String line = infile.nextLine();

if (isPalindrome(line))

System.out.println(line);

}

infile.close();

} // END MAIN

// RETURNs true if and only if the string passed in is a palindrome

static boolean isPalindrome(String line)

{

// Please write your code after this line

String revStr = "";

for (int i = 0; i < line.length(); i++) {

revStr = line.charAt(i) + revStr;

}

// for easier comparison

line = line.toLowerCase();

revStr = revStr.toLowerCase();

if (line.compareTo(revStr) == 0) {

return true;

}

return false;

}

} // END LAB4 CLASS

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