Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,153 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,640 people online right now. Registration is fast and FREE... Join Now!




Quadratic Formula Help

 

Quadratic Formula Help

heathro03

5 Oct, 2009 - 04:40 PM
Post #1

New D.I.C Head
*

Joined: 5 Oct, 2009
Posts: 13

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...
?
?

CODE

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));
      
    }
}





User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 13)

pbl

RE: Quadratic Formula Help

5 Oct, 2009 - 04:48 PM
Post #2

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
This is the semester of the Quadratic equation biggrin.gif 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
User is offlineProfile CardPM
+Quote Post

heathro03

RE: Quadratic Formula Help

5 Oct, 2009 - 05:09 PM
Post #3

New D.I.C Head
*

Joined: 5 Oct, 2009
Posts: 13

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: 5 Oct, 2009 - 05:10 PM
User is offlineProfile CardPM
+Quote Post

pbl

RE: Quadratic Formula Help

5 Oct, 2009 - 05:13 PM
Post #4

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
so ? blink.gif
User is offlineProfile CardPM
+Quote Post

heathro03

RE: Quadratic Formula Help

5 Oct, 2009 - 05:21 PM
Post #5

New D.I.C Head
*

Joined: 5 Oct, 2009
Posts: 13

"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?
User is offlineProfile CardPM
+Quote Post

pbl

RE: Quadratic Formula Help

5 Oct, 2009 - 05:25 PM
Post #6

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
QUOTE(heathro03 @ 5 Oct, 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?

Do you enter the 3 numbers on a single line on the console without doing a <CR> between them ?
User is offlineProfile CardPM
+Quote Post

heathro03

RE: Quadratic Formula Help

5 Oct, 2009 - 05:42 PM
Post #7

New D.I.C Head
*

Joined: 5 Oct, 2009
Posts: 13

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.


User is offlineProfile CardPM
+Quote Post

pbl

RE: Quadratic Formula Help

5 Oct, 2009 - 06:07 PM
Post #8

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
What is your locale ?

If I run that code

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
User is offlineProfile CardPM
+Quote Post

heathro03

RE: Quadratic Formula Help

5 Oct, 2009 - 06:16 PM
Post #9

New D.I.C Head
*

Joined: 5 Oct, 2009
Posts: 13

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...
User is offlineProfile CardPM
+Quote Post

pbl

RE: Quadratic Formula Help

5 Oct, 2009 - 06:34 PM
Post #10

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
QUOTE(heathro03 @ 5 Oct, 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...

US is the last country in the world where the decimal point is a "." rather than "," actually we should call it a decimal coma smile.gif 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
User is offlineProfile CardPM
+Quote Post

heathro03

RE: Quadratic Formula Help

5 Oct, 2009 - 06:46 PM
Post #11

New D.I.C Head
*

Joined: 5 Oct, 2009
Posts: 13

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.
User is offlineProfile CardPM
+Quote Post

pbl

RE: Quadratic Formula Help

5 Oct, 2009 - 06:53 PM
Post #12

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
QUOTE(heathro03 @ 5 Oct, 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.

Did you tried what I have suggested in previous post
User is offlineProfile CardPM
+Quote Post

heathro03

RE: Quadratic Formula Help

5 Oct, 2009 - 07:13 PM
Post #13

New D.I.C Head
*

Joined: 5 Oct, 2009
Posts: 13

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.
User is offlineProfile CardPM
+Quote Post

pbl

RE: Quadratic Formula Help

5 Oct, 2009 - 07:39 PM
Post #14

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
QUOTE(heathro03 @ 5 Oct, 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.

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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:07PM

Live Java Help!

Be Social

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

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month