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 6698709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:36:36+00:00 2026-05-26T06:36:36+00:00

Although the program works perfectly fine, my professor mentioned that the following is a

  • 0

Although the program works perfectly fine, my professor mentioned that the following is a logical error and should be fixed. I’m stumped, isn’t there only one root when the discriminant is equal to 0? Help will truly be appreciated!

This is the code he mentioned:

     if(discrim == 0) 
     { 
         eq1root1 = ((-1*coefB) + Math.sqrt(discrim))/(2 * coefA);
         System.out.println("This equation only has a single real root. Root = " + eq1root1);

Here’s the full code:

import java.lang.Math;
import javax.swing.JOptionPane; 

public class Assignment6
{
  public static void main(String[] args)
  {
    String a, 
           b,
           c; 
  double coefA,
         coefB,
         coefC,
         discrim,
         eq1root1,
         eq1root2;

  //Here the user is inputting the coefficients through a popup dialog box
  //Then the entered Strings are being converted to floating point numbers.  

  a = JOptionPane.showInputDialog( "Please enter a number for the quadratic coefficient a" ); 
  coefA = Double.parseDouble (a);        
  b = JOptionPane.showInputDialog( "Please enter a number for the quadratic coefficient b" );
  coefB = Double.parseDouble (b);  
  c = JOptionPane.showInputDialog( "Please enter a number for the quadratic coefficient c" );
  coefC = Double.parseDouble (c);  

   //Here the coefficients that the user entered are being displayed. 

   System.out.println("Your coefficient  a = " + coefA);
   System.out.println("Your coefficient  b = " + coefB);
   System.out.println("Your coefficient  c = " + coefC);

  //The following "nested if" statement sorts out equations with only 1 root, 2 roots, and or no roots at all.  
   discrim = coefB*coefB - (4 * coefA * coefC);
   if(discrim == 0) 
     { eq1root1 = ((-1*coefB) + Math.sqrt(discrim))/(2 * coefA);
      System.out.println("This equation only has a single real root. Root = " + eq1root1);
     }  
   else  if (discrim > 0)
   { eq1root1 = ((-1*coefB) + Math.sqrt(discrim))/(2 * coefA);
     eq1root2 =((-1*coefB) - Math.sqrt(discrim))/(2 * coefA);
     System.out.println("This equation has two real roots."); 
     System.out.println("Root 1 = " +  eq1root1); 
     System.out.println("Root 2 = " + eq1root2);
   }
   else 
   {
      System.out.println("This equation does not have any real roots."); 
   }

 }
}
  • 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-05-26T06:36:36+00:00Added an answer on May 26, 2026 at 6:36 am

    Only error I can see is that you don’t really need to do:

     eq1root1 = ((-1*coefB) + Math.sqrt(discrim))/(2 * coefA);
    

    You can do only

     eq1root1 = -1*coefB/(2 * coefA);
    

    Since you’re inside the if where you know the descriminant is zero.

    Like mentioned in another comment, you need to make sure that coefA is different from 0, since that will cause your code to raise an exception (you can’t divide by 0). Although that wouldn’t actually be a quadratic equation, it’s important to validate it.

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

Sidebar

Related Questions

The example program below compiles two in-memory assemblies. The first compilation works fine. The
Although my program works correctly in all cases, it doesn't use a pipe to
I'm trying to allocate a 2d array in a C program. It works fine
I have an OpenGL program that works on all of my computers but one.
Although appengine already is schema-less, there still need to define the entities that needed
Although the classic ASP method of server-side includes works in ASP.NET, I get the
I'm using Delphi 2009. My program has been compiling and running fine. I use
I have a system that I need to distrubte that works as follows. Servlets
I am loading an image using the OpenEXR library. This works fine, except the
Given the following code... class Program { static void Main(string[] args) { Foo foo

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.