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

This program #3 requires you to process three command line args: two ints and a

ID: 3651921 • Letter: T

Question

This program #3 requires you to process three command line args: two ints and a String - like this:

C:> java Program3 5 100 input1.txt
The "5" will be stored in args[0], the "100" will be stored in args[1] and the "input1.txt" into args[2].

args[0] must be converted then stored into an int named lo.
args[1] must be converted then stored into an int named hi.
args[2] must be stored into a String named infileName. No conversion will be required, just copy it from args[2] into a String var named infileName.
Once you have the three args stored as variables you must do the following calculations:

Use a nested for loop to print all the prime numbers between lo and hi inclusive on the same line of output - separated by a space.
Use a nested for loop to print all the perfect numbers between lo and hi inclusive on the same line of output - separated by a space.
Open up the file whose name was given on the cmd line. Loop through that file reading each word from its own line one at a time. With each word you are to determine if that word is or is not a palindrome, then write a separate line of output for that word stating that the word is (or is not) a palindrome.

Sample Input File
amanaplanacanalpanama
radar
foobar
mississippi
lookatoyotakool
mister roberts
XBOX 3 ROCKS!


Here is the starter file

*/

import java.io.*;
import java.util.*;

public class Program3
{

public static void main (String[] args)
{
try
{

int lo=0,hi=0; // range of #s to look for primes and perfects
String infileName; // file of Strings, one per line
Scanner infile = null;

if (args.length < 3)
{
System.out.println("CMD LINE INPUT ERROR: Must enter 2 numbers followed by name of input file on cmd line.");
System.exit(0);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .YOUR CODE BELOW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

// convert args[0] and args[1] to ints using parseInt() then store results in lo and hi respectively
// copy args[2] into infileName

// write a nested loop that prints out all and only the prime numbers between lo and hi inclusive
// put one space between each number - all on same line. Write a new line after the loops finish

// same as above but you are looking for perfect numbers this time

// create a new BufferedReader using infileName as the file to be opened for reading

// with each line from the file, store it into a String and determine if it is a palindrome or not.
// print one line of output per String saying whether it is a palindrome or not. (SEE SCREENSHOT)

// close the file
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . NO CODE BELOW HERE . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

}
catch ( Exception e)
{
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
System.out.println("EXCEPTION CAUGHT: " + sw.toString() );
System.exit( 0 );
}
} // END main
} //END CLASS PROGRAM3

Explanation / Answer

Please rate...

Program Program3.java

====================================================

import java.io.*;
public class Program3
{
    public static void main (String[] args)
    {
        try
        {

            int lo=0,hi=0; // range of #s to look for primes and perfects
            String infileName; // file of Strings, one per line
            BufferedReader infile = null;

            if (args.length < 3)
            {
                System.out.println("CMD LINE INPUT ERROR: Must enter 2 numbers followed by name of input file on cmd line.");
                System.exit(0);
            }

// - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// . . . . . . . . . . . . . . . .
// . . . . . . . . . . . . . . .YOUR CODE BELOW . . . . . . .
// . . . . . . . . . . . . . . . . . . . . . . . .

// convert args[0] and args[1] to ints using parseInt() then
// store results in lo and hi respectively
            lo=Integer.parseInt(args[0]);
            hi=Integer.parseInt(args[1]);
// copy args[2] into infileName
            infileName=args[2];
// write a nested loop that prints out all
// and only the prime numbers between lo and hi inclusive
// put one space between each number - all on same line. Write a
// new line after the loops finish
            int i,j,c;
            for(i=lo;i<=hi;i++)
            {
                c=0;
                for(j=2;j<i;j++)
                {
                    if(i%j==0)c++;
                }
                if(c==0)System.out.print(i+" ");
            }
            System.out.println();

// same as above but you are looking for perfect numbers this time
            for(i=lo;i<=hi;i++)
            {
                c=0;
                for(j=1;j<i;j++)
                {
                    if(i%j==0)c=c+j;
                }
                if(c==i)System.out.print(i+" ");
            }
            System.out.println();
// create a new BufferedReader using infileName as the file to be opened for reading
            infile=new BufferedReader(new FileReader(infileName));
// with each line from the file, store it into a String
// and determine if it is a palindrome or not.
// print one line of output per String saying whether it
// is a palindrome or not. (SEE SCREENSHOT)
            String t;
            while ((t = infile.readLine()) != null)
            {
                String t1="";
                for(i=(t.length()-1);i>=0;i--)
                {
                    t1=t1+t.charAt(i);
                }
                if(t1.equals(t))System.out.println(t+" is a palindrome");
                else System.out.println(t+" is not a palindrome");
            }
// close the file
            infile.close();
// . . . . . . . . . . . . . . . . . . . . .
// . . . . . . . . . NO CODE BELOW HERE . . . . . . .
// . . . . . . . . . . . . . . . . . . . . . .

        }
        catch ( Exception e)
        {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            System.out.println("EXCEPTION CAUGHT: " + sw.toString() );
            System.exit( 0 );
        }
    } // END main
} //END CLASS PROGRAM3

====================================================

Sample output:

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