Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    d


  2. #2
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    this java method combines the two equations and gives me a logic error and ultimately the wrong answer... so it's combining the two equations into "7x + 9y -14 = 0" , which evaluates to x=2 and y=0. This does not hold true for each individual equation

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    I don't think I could offer much help at the moment but first of all, your title kind of turns people off from helping you. We understand that you're confused about your problem but it doesn't make you different from anyone else. Anyways, to begin addressing your problem, you're confused about how to create a java program that will solve a two-variable equation. From a quick glance at your code, I'm not sure if you understand what you programmed.

    From what I see, you're printing an initial statement with the two equations but your loops are a bit confusing. Do you know what a for loop or while does?

    I suggest reading The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) and the while loop one in the same page.

    Instructor says that I will need to use string methods to extract the coefficients so that I can solve the equation.
    I think you should take your instructors advice and think about what string methods you've learned that will enable you to do this program.

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    I haven't reached to the loop section yet, well, we did a little bit of "for" and "while"... Here's what I got so far. But I am wondering if java will allow me to subtract two equations with two variables. Ex "5x+6y =9" - "6x+10y=3". However, I multiplied one of my equations so that when you subtract, it cancels out the "y" variable and you can solve for x.. then as you know, you would plug that into "Y' to solve for both equations... here is a copy of the code... it might not make sense, as I am in the middle of it:

    <import java.util.Scanner;
     
     public class TwoVariable
    	{
    		public static void main(String[] args)
    		 {
    			double a,b,c,d,e,f,g,h,i,j,k,l,m
    			Scanner ps = new Scanner(System.in);
     
    			System.out.println("Enter an \'X\' and a \'Y\' integer for the following equations: \n5x+3y=9 2x+6y=5");
    			System.out.println();
     
    			a = 5x+3y=9;
    			b = 2x+6y=5;
     
    			c = a.indexOf(5);
    			d = a.indexOf(3);
    			e = a.indexOf(9):
    			f = b.indexOf(2);
    			g = b.indexOf(6);
     
    			h = c*2;
    			i = d*2;
    			j = e*2;
     
    			k = a.replace(c,h);
    			l = a.replace(d,i);
    			m = a.replace(e,j);
     
     
     
     
    		 }
     
    	}>

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    Well, looking at your current code, I think you might have some issues. Logically, I can understand what you're trying to do. You want a and b to be the variables holding the equation. C through g will hold the coefficients and h through j will multiply them by two. K through l will replace the coefficients, allowing you to cancel out the y variable. Now what I think you would have a problem with is using the a.indexOf(int) method.

    I'd like you to try the following and see what happens:
    c = a.indexOf(5);
    System.out.println(c);
    d = a.indexOf(3);
    System.out.println(d);
    e = a.indexOf(9):
    f = b.indexOf(2);
    g = b.indexOf(6);
    and etc. for the variables that you're using. Are they doing what they want you to? If not, do you understand why they aren't?

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    so how do i take the index of an integer expression then? use parseInt, then covert it back to integer????? i do not want to use the substring method...yes, i guess my variables are doing what i want them to do. But I am stuck here... ok so after i multiply the coefficients, and replace them in the original "a" integer expression, I am getting ready to subtract expression "b" from expression "a"... how do I do that? and after that (when the "y"'s get subtracted out), how do I divide the right side of the equation by the coefficient of x, so that I can get the x value? Once I know how to do this, I can mimic this to get the "y" value. then solve this equation.

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    Well, I tried to compile the code you have now and it won't compile. There quite a bit of errors but I'd like to point out that you won't get the values that you're expecting. If you followed what I suggested, you'd find that your variables will return -1. Also, you declared all your variables as type double. I honestly think you should restart this and go step by step with a pencil and paper. I'd like to help you more but I'm about to go to bed, so I'd have to help you tomorrow. Hopefully someone else will take over and give you some support.

    What I'll leave off with is this:

    When you declared
    a = 5x+3y=9;
    b = 2x+6y=5;

    This is not a working code. To make it how you want it to be, I'd suggest making a String, so that you'd be able to use String methods, *possibly* the charAt() method *wink* *wink*

    Example: String a = "Expression here";
    *note* The index starts from left to right, 0 to whatever size it is. Like "5x+3y=9" will have 5 at index 0, 3 at index 3, and 9 at index 6.

    I hope that you'll be able to straighten this out and don't worry, coding is not easy and it takes time. It's good to make these mistakes early and to learn from them.

  8. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    hello, I just bumped to your thread, and did a fast read... I dont know if i could give a help and I'm not sure if this would be a good suggestion. but you could check with converting infix-to-postfix notation, to evaluate any expression easily, Its what im doing right now, and I found that instead of evaluating infix expression directly, its better to convert it to postfix first then later evaluate it...

  9. #9
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    The ultimate goal is to find numerical values of x and y defined by the set of linear algebraic equations

    ax + by = e
    cx + dy = f

    Where LHS (left-hand side) coefficients a, b, c, d and RHS (right-hand side) variables e, f are, somehow, extracted from user input Strings.

    For example

    3x+3y=9

    Or

    1.2x+3.4y=5.6



    Now, saying that the strings are "such as "3x+3y=9" and "4x+6y=11"" is not a particularly good specification.

    Suppose we know that they are exactly defined by the following, where "(number)" means a decimal integer or floating point number and there are no spaces between the tokens in the string. (The parentheses are not part of the input, they are there to show the nature of the individual tokens.)


    (number)(character 'x')(character '+')(number)(character 'y')(character '=')(number)

    If this is the input specification, then you can use the String.indexOf() method to find the positions of the 'x', 'y', '+', and '=' characters and then use the String.substring() method to get numerical substrings and then use Double.parseDouble() on each substring to get numerical values of a, b, e from the first user input String and numerical values of c, d, f from the second user input String. Such a method could also handle slight variations such as "4x-6y=11" for this particular input specification.

    On the other hand if optional spaces are allowed, then the simplest code for extracting coefficients from "4 x + 6 y = 11" might not work for "4 x - 6 y = 11" (That is to say, you might have to do a little more work to get an extraction algorithm that would work for all such cases with optional spaces in the Strings and with operator that could be either '+' or '-' for a given equation.)

    What if someone gave equations in terms of, say, u and v instead of x and y? Are you supposed to handle that also?

    My point is: Define input as precisely as is required so that you can create code to get numerical values of a, b, c, d, e, f into your program. If the input specification changes, then you may have to change the extraction algorithm, but the point is, you need to get numerical values of a, b, c, d, e, f into your program

    That was the hard part. The rest is easy, but only if we think about what we are doing and, maybe, practice a little before trying to write the code.

    So...

    Suppose you have, somehow, extracted numerical values of a, b, c, d, e, f from the input Strings. Now what?

    Well, it's relatively easy to write Java code to handle numerical operations. Not so easy to manipulate symbolic algebraic expressions mathematically. In other words, for numerical solutions, you deal with numbers, not equations.

    A simple system of two linear equations and two unknowns is easy to solve "by hand," right? You already know how, right? So, how about practicing with the example you were given?

    3x+3y=9
    4x+6y=11

    Wait a minute while I get a pencil and some paper to whip out a solution. Since this will be my test case for the program, we will need to know the solution if we are going to write a program to solve the system anyhow, right? I mean, I always (always always) work out a test strategy before writing code. Really. Always. For numerical problems that means that I will throw the program a problem for which I already know the answer.

    So while I am getting my stuff together, why don't you solve it?

    I'll be right back...

    Tick
    Tick
    Tick

    OK! Well, I got x = 3.5, y = -0.5. How about you?

    My ninth grade algebra teacher, Mrs. Watson, taught us always to check our work. (Thanks, Mrs. Watson, wherever you are. You taught the importance of logical thinking. You taught us not to panic when we are confronted with a problem for which we don't see an immediate solution. You taught us not to get mad if a teacher gave us a problem on a test or homework assignment that wasn't exactly, word for word, like one in the book. I remember every single thing you taught us. Really.)

    So...

    When I checked the residuals by plugging these solution values into the original equations, I got

    3x+3y-9 = 10.5-1.5-9 = 0

    4x+6y-11 = 14-3-11 = 0

    Taa-daa!

    Now, how did I solve the equations?

    ax+by=e
    cx+dy=f

    I'll show it symbolically, but, remember, in my computer program, I'll use the results that I derive here to just plug in numerical values for a, b, c, d, e, f. In other words, I will derive equations 'by hand,' and write code that simply evaluates the expressions that I come up with.


    Symbolically: "solve" the first equation for x in terms of y:

    x = (e-by)/a

    Symbolically, plug this "value" into the second equation:

    c(e-by)/a + dy = f

    Simple algebra (simple for us brilliant Humans; not so simple for not-so-brilliant computers) lets me get an expression for y in terms if the input numerical values.

    Namely, y = (af-ce)/(ad-bc)

    Note that this works if (and only if) the expression ad-bc is not equal to zero. If that expression is zero, the equations are not linearly independent. In that case there are either


    1. No solutions (equations are inconsistent)

    or

    2. An infinite number of solutions (they are consistent but one equation is simply a multiple of the other).


    Those cases aren't as interesting (numerically) as the case where that expression is not zero, since a non-zero value of (ad-bc) leads to unique values of x and y. That's what the program will find.

    Anyhow, assuming that expression has a non-zero value...

    I can "plug" that symbolic expression for y back into the first equation and get an expression for x in terms of a, b, c, d, e, f. (I'll leave that up to you).

    So here's the program flow:

    1. Get numerical values of a, b, c, d, e, and f from the user's input Strings.

    2. Calculate the value of (a*d - b*c). If the result is zero, the equations are not linearly independent. No solution will be attempted.

    3. If the value of (a*d - b*c) is not equal to zero, calculate numerical values of x and y by plugging values of a, b, c, d, e, f into the equations previously derived.

    4. Taa-daa! Print the solutions


    A general-purpose method for solving systems of linear algebraic equations can be found in this Reference: Cramer's Rule - Wikipedia.

    In fact, the expression for y that I give above is the same as shown in that article for the two-equation example. (It had better be the same, right? I mean the solutions are unique under the conditions that I spelled out above.)

    Now, if the input variables are all integers and you want rational number outputs (instead of floating point numbers), just calculate the two numerators and the denominator. Then print each "numerator / denominator." (Don't forget to reduce the rational number fraction to lowest terms by dividing by Greatest Common Divisor of numerator and denominator for each output.) So my test case would have solution x = 7/2 and y = -1/2.


    A final note:
    Cramer's rule might be perfectly appropriate for small systems (like our system of two equations and two unknowns), but the number of calculations grows as N factorial (where N is the number of equations). Numerical analyists never (really, never) use Cramer's rule for systems of, say, ten equations and ten unknowns. (Well, they might use such a system in a pedagogical setting to illustrate that it's really Not The Thing To Do, but they wouldn't actually use it to solve a "real" problem.) Other, more efficient methods are well known and characterized.


    Cheers!

    Z
    Last edited by Zaphod_b; September 25th, 2012 at 04:13 PM.

  10. The Following User Says Thank You to Zaphod_b For This Useful Post:

    ChicoTheMan94 (September 25th, 2012)

  11. #10
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    ok guys, i got a little bit further. First here is the code:
    < import java.util.Scanner;
     
     public class TwoVariable
    	{
    		public static void main(String[] args)
    		 {
    			double v,w,x,y,z,z1,z2,z3;
    			int c,e,g,j,l,n,p,q,r,s,t,u;
    			String a,b,d,f,h,i,k,m,o;
    			Scanner ps = new Scanner(System.in);
     
     
    			// Two equations in strigns
    			a = "5x+3y+9 ==0";
    			b = "2x+6y+5==0";
     
    			//Take index of each coefficients 
    			c = a.indexOf("5");
    			d = a.substring(c, c+1);
     
    			e = a.indexOf("3");
    			f = a.substring(e, e+1);
     
    			g = a.indexOf("9");
    			i = a.substring(g, g+1);
     
    			j = b.indexOf("2");
    			k = b.substring(j, j+1);
     
    			l = b.indexOf("6");
    			m = b.substring(l, l+1);
     
    			n = b.indexOf("5");
    			o = b.substring(n, n+1);
     
    			// Parse the integer so I can apply math
    			p = Integer.parseInt(d);
    			q = Integer.parseInt(f);
    			r = Integer.parseInt(i);
    			s = Integer.parseInt(k);
    			t = Integer.parseInt(m);
    			u = Integer.parseInt(o);
     
    			//Multiply the coefficients in the first equation
    			v = p*2;
    			w = q*2;
    			x = r*2;
     
    			//Subtract second coefficient from first coefficient
    			y = v-s;  //8
    			z = w-t; //0
    			z1 = x-u; //13
     
    			//Evaluate x
    			z2 = y/z1; 
     
    			//Evaluate y
    			z3 = (-5 - (2*z2))/6; // y
     
    			//Print out solutions
    			System.out.println("The solution for \'X\' and \'Y\' are: " +z2 + " " + z3);
     
     
    		 }
     
    	}>

    This code only codes one set of equations. I know I have to repeat it for another set of equations to fulfill my assignment... But here is my assignment
     
    <Write a java program that receives two Strings as arguments (like "2x+3y=8" and "3x+4y=11"), and solve the two-variable equation and prints the solution of the equation on the screen. Name your class TwoVariable. You will need to use String methods in order to extract the equations coefficients in order to solve the equations
     
        Variable names always will be "x" and "y".
        Coefficients can be any number from the range 1 to 99 (when the coefficent is 1 we will write it and won't omit it).
        There will be no extra space inside the equations, and two equations are separated using one space character.
     
    Sample input:
     
        java TwoVariable 2x+3y=8 3x+4y=11
        java TwoVariable 22x+20y=-2 1x+2y=1
     
    Sample output:
     
        x=1, y=2
        x=-1, y=1
     
        Submit your java source to the submit server.
        Assignment tag for this assignment is ass02.
        Do not forget to add the proper header (as instructed at the bottom of this page) at the beginning of your program.
    >


    Basically I have to enter two sets of equations at the command prompt (yea it's stupid). But for example, after I compile it, I would type "java TwoVariables n5x+3y=9 2x+6y=5" . And when I click enter, the command prompt will display the solution. Then I will have to make another equation with another set of solutions. So this program will have two different equations, but whatever one I type, will display the solution (at least that's how I interpret this f**ker's instructions). One person told me that I will need to use the args[] but I don't know how to do that...

  12. #11
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    zaphod, i just read what you typed. but is my way similar to yours? and after I type it after it compiles, say, "java TwoVariable 5x+3y=9 2x+6y=5", how do I get it to print out the solution? I can change the class to "TwoVariable 5x+3y=0 2x+6y=5", but this doesn't work, because I'm supposed to have to different sets of equations in the program... and if I type "java TwoVariables 5x+3y=9 2x+6y=5" it prints one results, and if I type "java TwoVariables "8x+7y=6 3x+8y=9", it prints another result. At least that's how I interpret the instructions above. Some guy was mentioning using args[] , and reading the input at the command prompt...

  13. #12
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    I didn't show how to extract coefficients from strings. but I gave some hints, as have other helpers.

    If you had given the exact input specification at first, my previous post could have been a little less wordy. (That would have been a blessing, right?)

    Anyhow...

    Here's the assignment in a nutshell:

    You are going to solve a system of two equations and two unknowns.
    The user enters two strings. Each string represents one of the equations. The form of the strings is in your assignment.

    Get it? The program solves one problem. The user gives the equation coefficients and other values in a particular form. It takes two strings to specify values for the two equations. The program solves this system for for x and y. That's one solution. A value of x and a value of y.


    To solve the equations, you must first extract the four left-hand side coefficients and the two right-hand side values from the two user input strings.

    I called the variables a, b, c, d, e, f. (Which conveniently corresponds to the variables in the Wikipedia article that I linked to.)

    Now, once you get the six numeric values for those variables, you solve the system for x and y. I indicated one way. The complete Cramer's rule formulas for two equations and two unknowns are in the Wikipedia reference that I gave you. If you don't want to use those expressions, do it any way that you want to. Well, do it any way that is mathematically correct (and, in particular that gives the correct answers that we so carefully worked out ahead of time).

    Bottom line: There are lots of ways to write a program that solves two equations and two unknowns, but if your program reads input values correctly and gives incorrect answers, then it the algorithm is wrong. That's why I always (yes always) start with a problem for which I know the answer. (But I said that already.) Period. Full stop.

    Some variation of Gauss reduction and back substitution using matrix variables is the most common way that "real" numerical analysts do it, especially for systems with more than a couple of equations. You can look them up.

    Anyhow...

    Here is my vision of a run. User input is black, program output is blue.


    Enter the first equation in the form ax+by=e: 3x+3y=9
    Enter the second equation in the form cx+dy=f: 4x+6y=11
    Here are the equations to be solved:
    3 * x + 3 * y = 9
    4 * x + 6 * y = 11

    Here is the solution:
    x = 3.500000, y = -0.500000


    The program skeleton looks like this:
    public class Z {
        public static void main(String [] args)
        {
            int a, b, c, d, e, f;
            double x, y;
            Scanner keyboard = new Scanner(System.in);
            System.out.printf("Enter the first equation in the form ax+by=e: ");
            String first = keyboard.nextLine();
            System.out.printf("Enter the second equation in the form cx+dy=f: ");
            String first = keyboard.nextLine();
            //
            // Your code goes here:
            //   Extract values of a, b, e from the first  String
            //   Extract values of c, d, f from the second String
            //
            //
     
            // Print out the equations so that we know the values
            // were read in correctly.
            //
            System.out.println("Here are the equations to be solved:");
            System.out.printf("%2d * x + %2d * y = %2d\n", a, b, e);
            System.out.printf("%2d * x + %2d * y = %2d\n", c, d, f);
            //
            // Your code goes here:
            //   Calculate x and y from the values of a, b, c, d, e, f
            //
            //
     
     
            //
            // Now print out the answer.
            System.out.println("Here is the solution:");
            System.out.printf("x = %f, y = %f\n", x, y);
        }
    }


    While debugging, you can just set the values of the Strings internally instead of reading from user input, so that you won't have to type the blasted stuff each and every stinking time that you run it while correcting bugs.

    public class Z {
        public static void main(String [] args)
        {
            int a, b, c, d, e, f;
            double x, y;
            // Comment out user input while debugging
            //Scanner keyboard = new Scanner(System.in);
            //System.out.printf("Enter the first equation in the form ax+by=e: ");
            //String first = keyboard.nextLine();
            //System.out.printf("Enter the second equation in the form cx+dy=f: ");
            //String first = keyboard.nextLine();
     
     
            // While debugging, just use fixed Strings instead of user input
            //
            String first = "3x+3y=9";
            String second = "4x+6y=11";
            //
            // Your code goes here:
            //   Extract values of a, b, e from the first  String
            //   Extract values of c, d, f from the second String
            //
            //
    .
    .
    .



    Cheers!

    Z

  14. The Following User Says Thank You to Zaphod_b For This Useful Post:

    ChicoTheMan94 (September 26th, 2012)

  15. #13
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    problem here though.. the coefficients can be a one or two digit number. So the substring method will not catch the two digit coefficients since I am programming it for one. How do I get it to read one or two digits? use the .length method?

  16. #14
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    ok I got my code working, but it has many logic errors... the coefficients can only be one number. how can i get it to read if it's one of two numbers. The substring is only coded for one digit coefficients. Here is the code

    <import java.util.Scanner;
     
     public class TwoVariable1
    	{
    		public static void main(String[] args)
    		 {
    			double g,i,j,k,l,m,n,o,p,z1,z2;
    			int t,u,v,w,x,y;
    			String a,b,c,d,e,f,a1,a2;
    			Scanner ps = new Scanner(System.in);
     
    			System.out.println("Enter two equations in the \'ax+by=c\' form, separating by a single space: ");
    			a1 = ps.next();
    			a2 = ps.nextLine();
     
     
    			a = a1.substring(0,1);
    			b = a1.substring(3,4);
    			e = a1.substring(6,7);
    			c = a2.substring(1,2);
    			d = a2.substring(4,5);
    			f = a2.substring(7,8);
     
    			t = Integer.parseInt(a);
    			u = Integer.parseInt(b);
    			v = Integer.parseInt(e);
    			w = Integer.parseInt(c);
    			x = Integer.parseInt(d);
    			y = Integer.parseInt(f);
     
    			// Second coefficent multiply by all coefficents first equation
    			g = x*t; //a
    			i = x*u; //b
    			j = x*v; //e
     
    			// First coefficent multiply by all coeffiecents second equation
    			k = u*w; //c
    			l = u*x; //d
    			m = u*y; //f
     
    			n = g-k; //a-c
    			o = j-m; //e-f
    			p = o/n; //value of x
     
    			z1 = m - (g*p);
    			z2 = z1/i; //value of y
     
     
    			//Print out solutions
    			System.out.println("The solution for \'X\' and \'Y\' are: " +z1 + " " + z2);
     
     
    		 }
     
    	}>

  17. #15
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    I thought you had been given some hints. Oh, well...

    1. Make the first substring dependent on the position of the character 'x' in each equation's String.

    2. Make the second substring dependent on the position of the character '+' and the position of the character 'y' in each equation's String.

    3. Make the third substring dependent on the position of the character '=' in each equation's String.


    Here's a start:
    public class Z {
        public static void main(String [] args) {
            String s1 = "1x+2y=3";
            String s2 = "12x+34y=56;
     
            int indx1 = s1.indexOf('x');
            System.out.printf("indx1 = %d\n", indx1);
            int indx2 = s2.indexOf('x');
            System.out.printf("indx2 = %d\n", indx2);
        }
    }

    How can you use indx1 as an argument to s1.substring() to get the String for the first coefficient of the first equation?
    How can you use indx2 as an argument to s2.substring() to get the String for the first coefficient of the second equation?

    Next step:
    Find the indexOf('+') and indexOf('y') for each equation. Use those values, somehow, to get the correct substrings for the second coefficients.

    Next step:
    Find the indexOf('=') for each equation. Use those values, somehow, to get the correct substrings for the right-hand side values.


    Your program does not work for me even within the limitation of one digit per constant. I mean, it does not give correct answers for anything I tried. What did you test it with?

    Try it with, say "1x+2y=5 2x+3y=8"

    What does the program tell you is the answer? What is the actual answer? (Solve by hand. Compare with program output. In each case, check the answer by calculating residuals as I did in my previous post.)

    Solve a problem manually with whatever method you think appropriate for the program. Then, go the program, step by step and print out the intermediate values being calculated. Compare each and every one with your manual calculations and make sure they agree.

    That way, if your manual calculations are correct, so will be the program's.

    Bada-bing, bada-boom!


    Cheers!

    Z
    Last edited by Zaphod_b; September 25th, 2012 at 11:55 PM.

  18. The Following User Says Thank You to Zaphod_b For This Useful Post:

    ChicoTheMan94 (September 26th, 2012)

  19. #16
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    I AMMMMMMMMMMM A GENIUSSSSSSSSSSSSSSSSS MUAHAHHA BUHAHAHA BUHAHA MUHAHAHHA.... I AM DA BEST PROGRAMMER EVER.

    real talk, thanks for your help me...

    unfortunately I have a lot of unfinished questions that I need to ask;
    1. what is "printf" , and why not use "print" or "println"
    2.
    <
            int indx1 = s1.indexOf('x');
            System.out.printf("indx1 = %d\n", indx1);
            int indx2 = s2.indexOf('x');
            System.out.printf("indx2 = %d\n", indx2);>

    What is the %d? i understand % is modula, and is used to find the remainder.

    And thanks for the hints. Here's my code:
    <import java.util.Scanner;
     
     public class TwoVariable
    	{
    		public static void main(String[] args)
    		 {
    			//Declare integers, doubles, strings
    			double g,h,t,u,v,w,x,y;
    			int a3,a4,a5,a6,a7,a8,a9,a10,a11,a12;
    			String a,b,c,d,e,f,a1,a2;
    			Scanner ps = new Scanner(System.in);
     
    			//Take the user input
    			System.out.println("Enter two equations in the \'ax+by=c\' form, separating by a single space: ");
    			a1 = ps.next();
    			a2 = ps.nextLine();
     
    			//Get coefficients in 1st string
    			a3 = a1.indexOf("x");
    			a4 = a1.indexOf("+");
    			a5 = a1.indexOf("y");
    			a6 = a1.indexOf("=");
    			a7 = a1.length();
     
    			//Get coefficients in 2nd string
    			a8  = a2.indexOf("x");
    			a9  = a2.indexOf("+");
    			a10 = a2.indexOf("y");
    			a11 = a2.indexOf("=");
    			a12 = a2.length();
     
     
    			//Take substring of all coefficients
    			a = a1.substring(0,a3); //a (ax+by=e cx+dy=f)
    			b = a1.substring(a4+1,a5); //b
    			e = a1.substring(a6+1,a7); //e
     
    			c = a2.substring(1,a8); //c
    			d = a2.substring(a9+1,a10); //d
    			f = a2.substring(a11+1,a12); //f
     
    			//Parse all integers
    			t = Double.parseDouble(a);
    			u = Double.parseDouble(b);
    			v = Double.parseDouble(e);
    			w = Double.parseDouble(c);
    			x = Double.parseDouble(d);
    			y = Double.parseDouble(f);
     
    			// Equations for y and x
    			g = ((t*y)-(w*v))/((t*x)-(u*w)); //y     y=(af-ce)/(ad-bc)
    		    h = ((v - (u*g)))/t; //x                 x=(e-by)/a
     
    			//Print out solutions
    			System.out.println("The solution for \'X\' and \'Y\' are: " + h + " " + g);
     
    		}
    	}
    		>

    Does that look solid to you? And is there an easier way to do this? I barely beat my deadline of 11:59, so any changes that you recommend will probably be for my personal learning

  20. #17
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    also, if you look at string 'a1", i used ps.next() instead of ps.nextLine()... I thought nextLine was unnecessary. Why did you use nextLine for both of yours in your example?

  21. #18
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    Quote Originally Posted by redbull View Post
    ..
    1. what is "printf" , and why not use "print" or "println"
    Cheat Sheet for printf format specifier
    Use whatever the heck you want.

    public class Z {
        public static void main(String [] args) {
            int x = 12;
            int y = 12345;
     
            System.out.printf("First  example: x = %d, y = %d\n", x, y);
            // Same as
            System.out.println("Second example: x = " + x + ", y = " + y);
            System.out.println();
     
            // Suppose you want them to line up the numbers on successive rows.
            // Each can be 1-5 digits, so make the field width = 5;
            System.out.printf("x = %5d\n", x);
            System.out.printf("y = %5d\n", y);
     
            // Try lining them up using fixed field widths with print()
            // and/or println().
            //
            // You have to use a separate formatter string, which (to me) is
            // more trouble than using printf.
            // Chacun à son goût
        }
    }

    Output:
    First  example: x = 12, y = 12345
    Second example: x = 12, y = 12345
     
    x =    12
    y = 12345



    Cheers!

    Z
    Last edited by Zaphod_b; September 26th, 2012 at 01:04 AM.

  22. #19
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Helpppp meeeee, I am stuckkkk and probably going to get violent any minute!!!!

    Quote Originally Posted by redbull View Post
    ... i used ps.next() instead of ps.nextLine()... I thought nextLine was unnecessary. Why did you use nextLine for both of yours in your example?
    I use nextLine() as a matter of habit since it empties out the stream input buffer so that I can do another nextLine() later to get another user input string and not be faced with a leftover terminating character or other stuff from previous input. I mean, I usually develop programs incrementally, and even though I'm not doing any more input at this stage, I might add some later.

    It doesn't cost anything extra to leave things in a clean state, right? So why not do it?


    Cheers!

    Z
    Last edited by Zaphod_b; September 26th, 2012 at 01:17 AM.

Similar Threads

  1. Help with Clock (advance and reverse day,hour,minute)
    By whyld4900 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 13th, 2011, 02:39 AM
  2. Game Program HELPPPP
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 19
    Last Post: July 20th, 2011, 04:26 AM

Tags for this Thread