School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
You're Browsing As A Guest! Register Now...
Become an Expert!

Join 353,816 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,501 people online right now.Registration is fast and FREE... Join Now!



Quadratic Formula Help

Quadratic Formula Help Rate Topic: -----

#1 heathro03  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-October 09


Dream Kudos: 0

Post icon  Posted 05 October 2009 - 04:40 PM

Hey guys,
I am working on a homework assignment and am just looking for some guidance or direction in where I'm going wrong. I am new to java and have been learning it for a couple weeks now. My assignment is to compute the roots of a quadratic using the quadratic formula. My program needs to read in three doubles with the values being separated by blanks in the test input. The roots need to be displayed to exactly 4 places past the decimal point, with a leading zero shown before the decimal point if it is between -1.0 and 1.0. No words are ever displayed because I turn it in electronically where the system compiles and tests my code automatically.

An example being:

If the values input are,
1.0 4.0 3.0

The output would be,
-1.0000
-3.0000


When I run my program and type in an input like whats above I get...
?
?

import java.util.Scanner;
import static java.lang.Math.*;
import static java.lang.System.*;
import java.text.DecimalFormat;
class ComputeRoots
{
 
	public static void main (String [] args)
	{
	  Scanner scan = new Scanner(in);
		
	  double a, b, c, discr, root1, root2;
	   
	  a = scan.nextDouble();
	  b = scan.nextDouble();
	  c = scan.nextDouble();
	  
	  discr = Math.sqrt ((b*b) - ( 4 * a * c));
	  
	  root1 = ( -b + discr ) / 2 * a;
	  root2 = ( -b + discr ) / 2 * a;
	  
	  DecimalFormat fmt1 = new DecimalFormat (" 0.####");
	  
	  out.println(fmt1.format(root1));
	  out.println(fmt1.format(root2));
	  
	}
}



Was This Post Helpful? 0
  • +
  • -


#2 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 1510
  • View blog
  • Posts: 11,683
  • Joined: 06-March 08


Dream Kudos: 475

Re: Quadratic Formula Help

Posted 05 October 2009 - 04:48 PM

This is the semester of the Quadratic equation :D 4 posts in a week
That will make a change from the Inventory program and its DVD which extends Product

Now problem with Scanner and new line

a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();

this implies that the 3 numbers are typed on the same line (one single line like: 1.2 2.3 4.5)
Is is what you are doing ? Not easy to find as you do not prompt the user to enter values and which way to enter them ?

Also make sure 2 * a is not == 0.0 because you will divide by 0
Was This Post Helpful? 0
  • +
  • -

#3 heathro03  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-October 09


Dream Kudos: 0

Re: Quadratic Formula Help

Posted 05 October 2009 - 05:09 PM

yea he wants us to do this because the program I write is not turned into him physically. The code is uploaded into an online application that is fairly sensitive. The application will compile the code I write it will then test it and will give me an instant grade response based on whether or not the test passed. The application is expecting this program and is prepared for just those three blank spots without any words. The program will throw random numbers in the input and expects an exact response, overall the program is not meant to be user friendly.

This post has been edited by heathro03: 05 October 2009 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 1510
  • View blog
  • Posts: 11,683
  • Joined: 06-March 08


Dream Kudos: 475

Re: Quadratic Formula Help

Posted 05 October 2009 - 05:13 PM

so ? :blink:
Was This Post Helpful? 0
  • +
  • -

#5 heathro03  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-October 09


Dream Kudos: 0

Re: Quadratic Formula Help

Posted 05 October 2009 - 05:21 PM

"a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();

this implies that the 3 numbers are typed on the same line (one single line like: 1.2 2.3 4.5)
Is is what you are doing ? Not easy to find as you do not prompt the user to enter values and which way to enter them ?"


I was trying to inform you on why it is like so. Maybe I misunderstood what you were getting at?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 1510
  • View blog
  • Posts: 11,683
  • Joined: 06-March 08


Dream Kudos: 475

Re: Quadratic Formula Help

Posted 05 October 2009 - 05:25 PM

View Postheathro03, on 5 Oct, 2009 - 05:21 PM, said:

"a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();

this implies that the 3 numbers are typed on the same line (one single line like: 1.2 2.3 4.5)
Is is what you are doing ? Not easy to find as you do not prompt the user to enter values and which way to enter them ?"


I was trying to inform you on why it is like so. Maybe I misunderstood what you were getting at?

Do you enter the 3 numbers on a single line on the console without doing a <CR> between them ?
Was This Post Helpful? 0
  • +
  • -

#7 heathro03  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-October 09


Dream Kudos: 0

Re: Quadratic Formula Help

Posted 05 October 2009 - 05:42 PM

yep nothing between them, only a single space on a single line. I don't type in any numbers, all I do is type the code and upload it to the grading application where it runs all the numbers it wants to make sure my coding is correct.

Once I upload the code it will run numbers in only that specific format, any other way or extra code and it will not come out properly.

example,

Input:
1.0 2.0 1.0

Output:
-1.0000
-1.0000


That is exactly how the online grading application expects the input for the program to work, as well it expects that type of output.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 1510
  • View blog
  • Posts: 11,683
  • Joined: 06-March 08


Dream Kudos: 475

Re: Quadratic Formula Help

Posted 05 October 2009 - 06:07 PM

What is your locale ?

If I run that code

import java.util.Scanner;
import java.text.DecimalFormat;
class ComputeRoots
{

	public static void main (String [] args)
	{
	  Scanner scan = new Scanner(System.in);
		
	  double a, b, c, discr, root1, root2;
	   
	  System.out.print("Enter a b c: ");
	  a = scan.nextDouble();
	  System.out.println("a = " + a);
	  b = scan.nextDouble();
	  System.out.println("b = " + b);
	  c = scan.nextDouble();
	  System.out.println("c = " + c);
	  
	  discr = Math.sqrt ((b*b) - ( 4 * a * c));
	  
	  root1 = ( -b + discr ) / 2 * a;
	  root2 = ( -b + discr ) / 2 * a;
	  
	  DecimalFormat fmt1 = new DecimalFormat (" 0.####");
	  
	  System.out.println(fmt1.format(root1));
	  System.out.println(fmt1.format(root2));
	  
	}
}




into which I have entered debug statements

if I enter: 1.2 3.4 5.6 it crashes as I have a French Canadian system
if I enter: 1,2 3,4 5.6 it works like a charm
Was This Post Helpful? 0
  • +
  • -

#9 heathro03  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-October 09


Dream Kudos: 0

Re: Quadratic Formula Help

Posted 05 October 2009 - 06:16 PM

I am in the US (Ohio, not sure if it makes a difference). I am not aware of the differences between the two and why such would happen. Thanks for all your continuous help...
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 1510
  • View blog
  • Posts: 11,683
  • Joined: 06-March 08


Dream Kudos: 475

Re: Quadratic Formula Help

Posted 05 October 2009 - 06:34 PM

View Postheathro03, on 5 Oct, 2009 - 06:16 PM, said:

I am in the US (Ohio, not sure if it makes a difference). I am not aware of the differences between the two and why such would happen. Thanks for all your continuous help...

US is the last country in the world where the decimal point is a "." rather than "," actually we should call it a decimal coma :) but in US
So you are in US with a US PC build by Dell or HP or Compaq and you download a US Java from a US site and your Regional Settings in Windows are US ? Or you use Linux and you install the US version ?

Just for fun... try to input your numbers as 1,2 3,4 and 5,6
Was This Post Helpful? 0
  • +
  • -

#11 heathro03  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-October 09


Dream Kudos: 0

Re: Quadratic Formula Help

Posted 05 October 2009 - 06:46 PM

wow, I had no idea. That's real interesting.. I have an HP with Windows Vista with Regional settings of US. I am using BlueJ as my compiler. I do not use Linux.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 1510
  • View blog
  • Posts: 11,683
  • Joined: 06-March 08


Dream Kudos: 475

Re: Quadratic Formula Help

Posted 05 October 2009 - 06:53 PM

View Postheathro03, on 5 Oct, 2009 - 06:46 PM, said:

wow, I had no idea. That's real interesting.. I have an HP with Windows Vista with Regional settings of US. I am using BlueJ as my compiler. I do not use Linux.

Did you tried what I have suggested in previous post
Was This Post Helpful? 0
  • +
  • -

#13 heathro03  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-October 09


Dream Kudos: 0

Re: Quadratic Formula Help

Posted 05 October 2009 - 07:13 PM

yes I did, it was unsuccessful. My compiler (BlueJ) is not working on my computer for some reason so I have been compiling with my command prompt if that makes any difference to you. This time it did not give me the two ?s like it does when I normally run it with the decimal points. This time it gave me an error saying, exception in thread "main" java.util.InputMismatchException. Being as I am fairly new to everything I am not exactly sure what this error message means.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 1510
  • View blog
  • Posts: 11,683
  • Joined: 06-March 08


Dream Kudos: 475

Re: Quadratic Formula Help

Posted 05 October 2009 - 07:39 PM

View Postheathro03, on 5 Oct, 2009 - 07:13 PM, said:

yes I did, it was unsuccessful. My compiler (BlueJ) is not working on my computer for some reason so I have been compiling with my command prompt if that makes any difference to you. This time it did not give me the two ?s like it does when I normally run it with the decimal points. This time it gave me an error saying, exception in thread "main" java.util.InputMismatchException. Being as I am fairly new to everything I am not exactly sure what this error message means.

Let us not mix apples and oranges here
You are usiong BlueJ as Editor (not compiler)
your compiler is the standard Java compiler provided by Sun
the InputMismatchException telles us (when use with , instead of .) that your settings for US are OK
Now, sorry but I am kind of lost
Hope some Dic from the US can advise... works well on my side with French Canadian settings
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month