Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

(1) Question:For this java coding challenge, you will ask the user for the name of an output file to save the data.

(1) Question:For this java coding challenge, you will ask the user for the name of an output file to save the data.

You will expand the java program that asks for a start number and stop number, and tells you if the numbers in the inclusive range are prime, perfect, imperfect deficient, or imperfect abundant. You will next add a check for Perfect Squares. A perfect square is the square of an integer (e.g., 1,4,9,16, 25, etc.)

Note 1: For this project, the number 1 will be considered a prime number, but it is not a perfect number (since its only factor is just 1, which by definition is an improper factor, thus disqualifying it from being perfect)

Note 2: All Prime Numbers and Perfect Squares are also Imperfect Deficient. But a positive check for "Prime" or "Perfect Square" supersedes a check for "Imperfect Deficient"

Note 3: You do not need to add any error testing code on the numbers; the code will be tested with strictly positive integers. You must add error checking for the creation of the external file (per Java's insistence)

Below is my Java code:

import java.io.*;

import java.util.Scanner;

public class CodingChallenge4CRM {

  public static void main(String[] args){

    Scanner sc = new Scanner(System.in);

    System.out.println("Welcome to the Prime, Perfect Square, and Perfect Number Tester ");

    System.out.println("Please enter the name of the output file to save your data:");

    String name = sc.nextLine();

    PrintWriter pw = null;

    try {

      FileOutputStream fout = new FileOutputStream(name);

      pw = new PrintWriter(fout);

    }

    catch (Exception e){

      e.printStackTrace();

    }

    System.out.print("Enter a start number:");

    int a = Integer.parseInt(sc.nextLine());

    System.out.print("Enter a stop number:");

    int b = Integer.parseInt(sc.nextLine());

    for (int i = a; i<b; i++){

      int ps = 0;

      for (int k = 0; k<a; k++){

        if (k*k == i){

         ps = 1;

         System.out.println("The number " + i+ " is: Perfect Square");

         pw.println("The number " + i+ " is: Perfect Square");

        }

      }

      if (ps == 1)

       continue;

      int sum = 0;

      for (int j = 2; j<i; j++){

        if (i % j == 0)

         sum = sum + j;

      }

      if (sum == 0){

        System.out.println("The number " + i+ " is: Prime");

        pw.println("The number " + i+ " is: Prime");

      }

      else if (sum == i){

        System.out.println("The number " + i+ " is: Perfect Number");

        pw.println("The number " + i+ " is: Perfect Number");

      }

      else if (sum > i){

        System.out.println("The number " + i+ " is: Imperfect Abundant");

        pw.println("The number " + i+ " is: Imperfect Abundant");

      }

      else{

        System.out.println("The number " + i+ " is: Imperfect Defficient");

        pw.println("The number " + i+ " is: Imperfect Defficient");

      }

    }   

  }

}

The output needs to look like the following:

Welcome to the Prime, Perfect Square, and Perfect Number Tester

Enter the name of the output file: MyNumData.txt

Enter a start number: 20

Enter a stop number : 30

The number 20 is: Imperfect Abundant

The number 21 is: Imperfect Deficient

The number 22 is: Imperfect Deficient

The number 23 is: Prime

The number 24 is: Imperfect Abundant

The number 25 is: Perfect Square

The number 26 is: Imperfect Deficient

The number 27 is: Imperfect Deficient

The number 28 is: Perfect

The number 29 is: Prime

The number 30 is: Imperfect Abundant

Results: my program stops one less--stops at 29 and it incorrectly reports 28 as imperfect deficient when in fact it is perfect. I can't figure out where I went wrong.

(2) Plus, next I need to ask the user for a string to search in the file, and the program will print all lines that contain that string. The search will be case insensitive.

This builds on the previous question above.

The output format must look like the following:

"Welcome to the Prime, Perfect Square, and Perfect Number Tester

Enter the name of the output file: MyNumData.txt

Enter a start number: 20

Enter a stop number : 30

The number 20 is: Imperfect Abundant

The number 21 is: Imperfect Deficient

The number 22 is: Imperfect Deficient

The number 23 is: Prime

The number 24 is: Imperfect Abundant

The number 25 is: Perfect Square

The number 26 is: Imperfect Deficient

The number 27 is: Imperfect Deficient

The number 28 is: Perfect

The number 29 is: Prime

The number 30 is: Imperfect Abundant

Enter the name of the input file to search: MyNumData.txt

Enter the string to search: prim

The following lines were found:

The number 23 is: Prime

The number 29 is: Prime"

Any assistance will be greatly appreciated.

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question