Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9220265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:18:26+00:00 2026-06-18T03:18:26+00:00

Im working on a program I need to finish tonight, and basically it does

  • 0

Im working on a program I need to finish tonight, and basically it does a cheep version of factoring…
The problem is, that its not giving me numbers, but NaN.
Heres my code:

Class 1(Part that deals with this program):

    System.out.println("--------------------------------------------------");
        System.out.println("                   ~Factoring~");
        System.out.println("--------------------------------------------------");
        System.out.println("in a polynomial, there are 3 important numbers used");
        System.out.println("to figure out x. they are a, b, and c, shown below.\n");
        System.out.println("\t\t1x^2 +2x -3");
        System.out.println("\t\t^     ^   ^");
        System.out.println("\t\ta     b   c");
        System.out.print("\nPlease type a, b, and c here[a b c]: ");
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        mathey factor = new mathey(a,b,c, chooser);
        System.out.print(factor.solvefact());

Class 2:

    public class mathey
    {
        double a,b,c;

double solution1;
double solution2;
double discriminant;
double x1 = 0;
double x2 = 0;
  public mathey(int aN, int bN, int cN)
{
    a = aN;
    b = bN;
    c = cN;
    discriminant = (b*b)-4*a*c;
    solvea();
    solveb();
}
public String solvea()
{
    solution1 = (-1*b + Math.sqrt(discriminant))/(2*a);
    x1 = solution1;
    if(discriminant > 0) 
    { 
        return"x = " + solution1; 

    } 
    else if(discriminant == 0) 
    { 
        return "x =  " + solution1; 
    } 
    else 
    { 
        double root1complex = -b/(2*a); 
        double root1complex2 = Math.sqrt(-discriminant)/(2*a); 

        return root1complex + " + " + root1complex2 + " i "; 
    }
}
    public String solveb()
{
    solution2 = (-1*b - Math.sqrt(discriminant))/(2*a);
    x2 = solution2;
   if(discriminant > 0) 
    { 
        return"x = " + solution2; 

    } 
    else if(discriminant == 0) 
    { 
        return"x =  " + solution2; 
    } 
    else 
    { 
        double root1complex = -b/(2*a); 
        double root1complex2 = Math.sqrt(-discriminant)/(2*a); 

        return root1complex + " - " + root1complex2 + " i "; 
    }
}
public mathey(int aFact, int bFact ,int cFact, int chooser)
{
    a = aFact; b = bFact; c = cFact;
    discriminant = (b*b)-4*a*c;
    solvea();
    solveb();
    solvefact();
}
public String solvefact()
{
    String Answer = "";
    if((int)solution1 == solution1)
    {
         int wholeNum = (int)solution1/1;
         double numerator = (solution1%1) * 10;
         int denominator = 10;
         while(numerator > denominator) {
             denominator = denominator * 10;
            }   
         Answer+="("+denominator+"x + "+((denominator * wholeNum) + numerator)+")";

    }
    else
    {
        Answer +="( x + " +(solution1*-1) +")";



    }
    if((int)solution2 == solution2)
        {
            int wholeNum = (int)solution2/1;
            double numerator = (solution2%1) * 10;
            int denominator = 10;
            while(numerator > denominator) {
                denominator = denominator * 10;
            }   
            Answer+="("+denominator+"x + "+((denominator * wholeNum) + numerator)+")";


        }
        else
        {
          Answer +="( x + " +(solution2*-1) +")";  
        } 
    return Answer;
}

Heres the output:

    Choose a Way to Solve
    1. Quadratic Formula
    2. Factoring
    Which Method? [1/2]: 2
    --------------------------------------------------
                       ~Factoring~
    --------------------------------------------------
    in a polynomial, there are 3 important numbers used
    to figure out x. they are a, b, and c, shown below.

    1x^2 +2x -3
    ^     ^   ^
    a     b   c

    Please type a, b, and c here[a b c]: 1 2 -3
    (10x + 10.0)(10x + -30.0)

How Do I fix this, so I get the Output I should get? (x + 3.0)(x-1.0)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:18:27+00:00Added an answer on June 18, 2026 at 3:18 am

    In your 4-param constructor Mathey() (which is the constructor you are calling) you are redeclaring the variables a, b, c and assigning the values passed in to them, masking the instance variables which remain equal to 0 (the default). These local variables are only in scope in the constructor. In solveA() and solveB(), a, b, c again refer to the instance variables (which are all 0), so you’re dividing by 2*a = 0, which makes solution1 and solution2 equal to NaN.

    Change the line in the second constructor (if you continue to use it) from

    double a = aN, b = bN, c = cN;
    

    to

    a = aN, b = bN, c = cN;
    

    to solve the masking issue. You most likely want the instance variables to be doubles rather than ints, though, so change

    int a;int b;int c;
    

    to

    double a, b, c;
    

    (you can do multiple declarations of the same type like this).

    I don’t know why you have two Mathey constructors, so either scrap the second one (what is chooser?) and just use the first, or make sure the second one also assigns a value to determinant.

    This should be a start, anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a program that will need to delete a folder (and then
I need to restart the program that im working on after an update has
I'm working to finish a math problem that approximates the square root of a
I was working on a program,that I need to support new additions. Hmmm. Let
I am working on a program with a tabbed interface, my problem is that
I need to translate an old program working on AS/400 that was picking random
I am working on a program and need ot know how I would read
I need to understand the working of this particular program, It seems to be
I have a working program in C++ that generates data for a Mandelbrot Set.
I have been working on a program where I need to slowly and smoothly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.