Results 1 to 11 of 11
  1. #1
    _Jk_ is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Help with equation

    Hi, I need some help in making a program that reads three values (a, b and c) from a quadratic equation and then writes in this manner (a x^2 + b x + c = 0).

    examples:

    a b c ? 0 1 -2,5
    the equation is: x – 2,5 = 0

    a b c ? 2 -1 0
    the equation is: 2,0 x^2 - x = 0

    a b c ? 3,25 1 -2
    the equation is: 3,25 x^2 + x – 2,0 = 0

    a b c ? 1 0 1
    the equation is: x^2 + 1,0 = 0

    Any kind of help is appreciated.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    What part of this are you having trouble with?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    _Jk_ is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Well is the part that if any of the values are zero we have to omit them and the x's, if any of them is equal to 1 we must leave them out but print the x's and if the values are positive or negative. The only idea that i had was with if-elses but it would be a loooong code.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Let's see that loooong code and maybe we can help you improve it.

    db

  5. #5
    _Jk_ is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Ok here´s the code:

    Java Code:
    import java.util.*;
    
    public class Problem
    {
    	public static void main(String[] args)
    	{
    		Scanner teclado = new Scanner(System.in);
    		System.out.print("v1 v2 v3 ? ");
    		double a = teclado.nextDouble();
    		double b = teclado.nextDouble();
    		double c = teclado.nextDouble();
    		
    		if (a>1 || a<0)
    		{
    			if (b>1)
    			{
    				if (c>1) System.out.println("equation is: " + a + "x^2 + " + b + "x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: " + a + "x^2 + " + b + "x = 0");
    				else if (c==1) System.out.println("equation is: " + a + "x^2 + " + b + "x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: " + a + "x^2 + " + b + "x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: " + a + "x^2 + " + b + "x " + c + " = 0");
    			}
    			if (b<-1)
    			{
    				if (c>1) System.out.println("equation is: " + a + "x^2 " + b + "x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: " + a + "x^2 " + b + "x + " + c + " = 0");
    				else if (c==1) System.out.println("equation is: " + a + "x^2 " + b + "x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: " + a + "x^2 " + b + "x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: " + a + "x^2 " + b + "x " + c + " = 0");
    			}	
    			else if (b==0)
    			{
    				if (c>1) System.out.println("equation is: " + a + "x^2 + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: " + a + "x^2 = 0");
    				else if (c==1) System.out.println("equation is: " + a + "x^2 + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: " + a + "x^2 " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: " + a + "x^2 " + c + " = 0");
    			}
    			else if (b==1)
    			{
    				if (c>1) System.out.println("equation is: " + a + "x^2 + x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: " + a + "x^2 + x = 0");
    				else if (c==1) System.out.println("equation is: " + a + "x^2 + x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: " + a + "x^2 + x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: " + a + "x^2 + x " + c + " = 0");
    			}
    			else if (b==-1)
    			{
    				if (c>1) System.out.println("equation is: " + a + "x^2 - x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: " + a + "x^2 - x = 0");
    				else if (c==1) System.out.println("equation is: " + a + "x^2 - x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: " + a + "x^2 - x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: " + a + "x^2 - x " + c + " = 0");
    			}
    		}	 
    		
    		else if (a==0)
    		{
    			if (b>1)
    			{
    				if (c>1) System.out.println("equation is: " + b + "x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: " + b + "x = 0");
    				else if (c==1) System.out.println("equation is: " + b + "x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: " + b + "x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: " + b + "x " + c + " = 0");
    			}
    			if (b<-1)
    			{
    				if (c>1) System.out.println("equation is: " + b + "x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: " + b + "x + " + c + " = 0");
    				else if (c==1) System.out.println("equation is: " + b + "x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: " + b + "x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: " + b + "x " + c + " = 0");
    			}
    			else if (b==0)
    			{
    				if (c>1) System.out.println("equation is: " + c + " = 0");
    				else if (c==0) System.out.println("equation is: 0 = 0");
    				else if (c==1) System.out.println("equation is: " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: " + c + " = 0");
    			}
    			else if (b==1)
    			{
    				if (c>1) System.out.println("equation is: x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: x = 0");
    				else if (c==1) System.out.println("equation is: x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: x " + c + " = 0");
    			}
    			else if (b==-1)
    			{
    				if (c>1) System.out.println("equation is: - x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: - x = 0");
    				else if (c==1) System.out.println("equation is: - x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: - x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: - x " + c + " = 0");
    			}
    		}
    		
    		else if (a==1)
    		{
    			if (b>1)
    			{
    				if (c>1) System.out.println("equation is: x^2 + " + b + "x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: x^2 + " + b + "x = 0");
    				else if (c==1) System.out.println("equation is: x^2 + " + b + "x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: x^2 + " + b + "x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: x^2 + " + b + "x " + c + " = 0");
    			}
    			if (b<-1)
    			{
    				if (c>1) System.out.println("equation is: x^2 " + b + "x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: x^2 " + b + "x + " + c + " = 0");
    				else if (c==1) System.out.println("equation is: x^2 " + b + "x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: x^2 " + b + "x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: x^2 " + b + "x " + c + " = 0");	
    			}
    			else if (b==0)
    			{
    				if (c>1) System.out.println("equation is: x^2 + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: x^2 = 0");
    				else if (c==1) System.out.println("equation is: x^2 + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: x^2 " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: x^2 " + c + " = 0");
    			}
    			else if (b==1)
    			{
    				if (c>1) System.out.println("equation is: x^2 + x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: x^2 + x = 0");
    				else if (c==1) System.out.println("equation is: x^2 + x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: x^2 + x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: x^2 + x " + c + " = 0");
    			}
    			else if (b==-1)
    			{
    				if (c>1) System.out.println("equation is: x^2 - x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: x^2 - x = 0");
    				else if (c==1) System.out.println("equation is: x^2 - x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: x^2 - x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: x^2 - x " + c + " = 0");
    			}
    		}
    		
    		else if (a==-1)
    		{
    			if (b>1)
    			{
    				if (c>1) System.out.println("equation is: - x^2 + " + b + "x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: - x^2 + " + b + "x = 0");
    				else if (c==1) System.out.println("equation is: - x^2 + " + b + "x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: - x^2 + " + b + "x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: - x^2 + " + b + "x " + c + " = 0");
    			}
    			if (b<-1)
    			{
    				if (c>1) System.out.println("equation is: - x^2 " + b + "x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: - x^2 " + b + "x + " + c + " = 0");
    				else if (c==1) System.out.println("equation is: - x^2 " + b + "x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: - x^2 " + b + "x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: - x^2 " + b + "x " + c + " = 0");
    			}
    			else if (b==0)
    			{
    				if (c>1) System.out.println("equation is: - x^2 + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: - x^2 = 0");
    				else if (c==1) System.out.println("equation is: - x^2 + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: - x^2 " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: - x^2 " + c + " = 0");
    			}
    			else if (b==1)
    			{
    				if (c>1) System.out.println("equation is: - x^2 + x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: - x^2 + x = 0");
    				else if (c==1) System.out.println("equation is: - x^2 + x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: - x^2 + x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: - x^2 + x " + c + " = 0");
    			}
    			else if (b==-1)
    			{
    				if (c>1) System.out.println("equation is: - x^2 - x + " + c + " = 0");
    				else if (c==0) System.out.println("equation is: - x^2 - x = 0");
    				else if (c==1) System.out.println("equation is: - x^2 - x + " + c + " = 0");
    				else if (c==-1) System.out.println("equation is: - x^2 - x " + c + " = 0");
    				else if (c<-1) System.out.println("equation is: - x^2 - x " + c + " = 0");
    			}
    		}
    		else System.out.println("Equation is invalid.");
    	}
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Yuck, try to implement a little method that prints out one term of the polynomial given the coefficient and the exponent and apply that method on each term of the polynomial. That by itself will reduce the size of your code enormously.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    _Jk_ is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Sorry not very good in english. Could you show a little example of what you just said

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    It rather appears that you aren't aware of how to write and use methods, apart from the main(...) method. Here's where you can learn more about that:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Or start at the top of that section of the tutorial and work your way down.
    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

    db

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by _Jk_ View Post
    Sorry not very good in english. Could you show a little example of what you just said
    I already expected such a reply; it has nothing to do with English but I wrote a little spoiler for you:

    Java Code:
    public class T {
    
    	private static String printExp(int exp) {
    		String r= "";
    		if (exp > 0) r= "x";
    		if (exp > 1) r+= "^"+exp;
    		
    		return r;
    	}
    	
    	private static String printCoef(double coef) {
    		if (coef > 0) return "+"+coef;
    		return ""+coef;
    	}
    	
    	private static void printTerm(double coef, int exp) {
    		
    		if (coef == 0) return;
    		if (coef == -1)
    			if (exp == 0) System.out.print("-1");
    			else System.out.print("-"+printExp(exp));
    		else if (coef == 1)
    			if (exp == 0) System.out.print("+1");
    			else System.out.print(printExp(exp));
    		else
    			System.out.print(printCoef(coef)+printExp(exp));
    	}
    	
    	public static void main(String[] args) {
    		printTerm(2, 2);
    		printTerm(3, 1);
    		printTerm(-1, 0);
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    _Jk_ is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    @JosAH Many thanks, i now have completed the little rest of the code but there is one thing left.
    When I put 0 1 -2,5
    The output is +x-2,5 = 0

    Since I don´t need that plus sign in the beginning do I have to create another metod for this or add some code on an existing metod?

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by _Jk_ View Post
    Since I don´t need that plus sign in the beginning do I have to create another metod for this or add some code on an existing metod?
    I left that as an exercise for the reader (*)

    kind regards,

    Jos

    (*) I love writing that ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. equation
    By bobo67 in forum New To Java
    Replies: 5
    Last Post: 09-06-2010, 07:40 PM
  2. Need help with math equation
    By annabellastorm in forum New To Java
    Replies: 4
    Last Post: 01-10-2010, 06:12 PM
  3. differential equation RK4
    By arvindmer in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 02:27 PM
  4. Quadratic Equation
    By jpnym15 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 04:29 AM
  5. Help with an equation in java
    By coco in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •