1 2 3 4 5 Previous Next 72 Replies Latest reply on Jun 4, 2007 9:01 PM by 807600 Go to original post
      • 45. Re: Have a method return an array
        807600
        ok then...
        • 46. Re: Have a method return an array
          807600
          When you write the code, do you use Notepad or Textpad?
          • 47. Re: Have a method return an array
            807600
            you might want to look into an open-source programmer's editor like jEdit or Notepad++. They are free and can help you format your code without having all of the overhead of a complex IDE such as Eclipse or Netbeans.
            • 49. Re: Have a method return an array
              807600
              I dont want to read the 5 page thread but would love to know the assignment details? May be you can catch up. i can see you are working hard in order to complete it and i am willing to help you. so please post your assignment.
              • 50. Re: Have a method return an array
                807600
                OK. This is the latest version of a program which factors a quadratic equation. There are a few minor bugs and some organization issues....
                anyway, here you go:
                class Factors{
                     private int value; 
                      int SolutionFac1, SolutionFac2;
                     private int FacArray[][];
                     public int SolutionArray[] = new int[2];
                
                public Factors(int value){
                     this.value = value;
                     FacArray = new int[2][value];
                }
                
                public int[][] FindFacs(int number){
                     int slot = 0;
                     float modulus;
                     int divisor, result;
                
                     for(divisor = 1; divisor <= (number/2); divisor++){
                          if(number%divisor==0){ 
                               result = number/divisor;
                               if(divisor <= result){
                                    FacArray[0][slot]=divisor;
                                    FacArray[1][slot]=result;
                                    slot++;
                               };
                          };
                     };
                     return FacArray;
                }
                
                public int[] TestA(int a){
                FacArray[0][0] = SolutionFac1;
                FacArray[1][0] = SolutionFac2;
                SolutionArray[0] = SolutionFac1;
                SolutionArray[1] = SolutionFac2;
                return SolutionArray;
                }
                
                public int[] TestC(int c, int b){
                     for(int retrieval = 0; retrieval < c; retrieval++){
                          FacArray[0][retrieval] = SolutionFac1;
                          FacArray[1][retrieval] = SolutionFac2;
                          if((SolutionFac1 + SolutionFac2) == b) break;
                     }
                     SolutionArray[0] = SolutionFac1;
                     SolutionArray[1] = SolutionFac2;
                     return SolutionArray;
                }
                
                }
                
                public class EqnFac{
                public static void main(String args[]){
                     int a = 6;
                     int b = 6;
                     int c = 9;
                
                     Factors aFac = new Factors(a);
                     Factors cFac = new Factors(c);
                
                     aFac.FindFacs(a);
                     cFac.FindFacs(c);
                
                     aFac.TestA(a);
                     cFac.TestC(c,b);
                
                     System.out.print("Factored form of equation is: ");
                     System.out.print( "(" + a + "x + " + cFac.SolutionFac1 + ")" );
                     System.out.print( "(x + " + cFac.SolutionFac2 + ")" );
                 
                }
                }
                • 51. Re: Have a method return an array
                  807600
                  The most recent bugs like a method in class "main" trying to access variables outside its class haven't been fixed, so...
                  • 52. Re: Have a method return an array
                    807600
                    In fact, before you try to edit it, let me revise.
                    • 53. Re: Have a method return an array
                      807600
                      System.out.print( "(" + a + "x + " + cFac.SolutionFac1 + ")" );
                           System.out.print( "(x + " + cFac.SolutionFac2 + ")" );
                      Where is SolutionFac1 and 2 method? I dont see any methods in your program.
                      • 54. Re: Have a method return an array
                        807600
                        areyou just trying to find a factorial of a given number?
                        • 55. Re: Have a method return an array
                          807600
                          Ok here is the kinda sorta better version. I'm trying to factor a quadratic equation, from "ax^2 + bx + c" to (_x + _)(_x +_). Look at this code (sorry it's such a mess, I write crappy code):
                          class Factors{
                               private int value; 
                               private int SolutionFac1, SolutionFac2;
                               private int FacArray[][];
                               public int SolutionArray[] = new int[2];
                          
                          public Factors(int value){
                               this.value = value;
                               FacArray = new int[2][value];
                          }
                          
                          public int[][] FindFacs(int number){
                               int slot = 0;
                               float modulus;
                               int divisor, result;
                          
                               for(divisor = 1; divisor <= (number/2); divisor++){
                                    if(number%divisor==0){ 
                                         result = number/divisor;
                                         if(divisor <= result){
                                              FacArray[0][slot]=divisor;
                                              FacArray[1][slot]=result;
                                              slot++;
                                         };
                                    };
                               };
                               return FacArray;
                          }
                          
                          
                          public void TestFacs(int c, int b){
                               for(int retrieval = 0; retrieval < c; retrieval++){
                                    FacArray[0][retrieval] = SolutionFac1;
                                    FacArray[1][retrieval] = SolutionFac2;
                                    if((SolutionFac1 + SolutionFac2) == b) break;
                               }
                          }
                          
                          public int GetSolution1(){
                               return SolutionFac1;
                          }
                          public int GetSolution2(){
                               return SolutionFac2;
                          }
                          
                          }
                          
                          public class EqnFac2{
                          public static void main(String args[]){
                               int a = 6;
                               int b = 6;
                               int c = 9;
                          
                               Factors aFac = new Factors(a);
                               Factors cFac = new Factors(c);
                          
                               aFac.FindFacs(a);
                               cFac.FindFacs(c);
                          
                               cFac.TestFacs(c,b);
                          
                          
                               System.out.print("Factored form of equation is: ");
                               System.out.print( "(" + a + "x + " + cFac.GetSolution1() + ")" );
                               System.out.print( "(x + " + cFac.GetSolution2() + ")" );
                           
                          }
                          }
                          • 56. Re: Have a method return an array
                            807600
                            I haven't followed the entire thread, but if you're factoring quadratic equations, why would you need a 2-d array?

                            First I would check to see if variable 'a' is equal to 1.
                            If so, then multiply 'a' * 'c'. Now you have to find what factors of (a * c) add/subtract to get the value of b.
                            Example
                            // 1x^2 + 7x + 6
                            int a = 1;
                            int b = 7;
                            int c = 6
                            
                            // Multiply a * c, which = 6
                            // So take note of the second sign in the equation which is a '+'
                            // So what factors of (a * c) add up to 'b'. I know they have to add because
                            // of the + sign in the second part.
                            // So the factors of six are (1 * 6), (2 * 3) 
                            // (1 * 6) those numbers add up to 7!
                            // so (x + 1) (x + 6) are your factors for  1x^2 + 7x + 6
                            This is the first case I would test for. (a value being 1)
                            If a isn't equal to one, I would divide the entire equation
                            by a in order to make the value a equal to one.

                            Are you trying to find the solution to this without using the quadratic equation?
                            • 57. Re: Have a method return an array
                              807600
                              Yes, but I suppose the quadratic equation would make it 100 times eisier. I don't inderstand why you multiply a and c. The two conditions the factors must meet are:
                              1) The factors of c add to b
                              2) The factors of a multiply to a (so any combination of them would work)
                              • 58. Re: Have a method return an array
                                807600
                                a and c are completely independent of each other, and a will most often be 1, as you said.
                                • 59. Re: Have a method return an array
                                  807600
                                  It's just a simple trick I learned in my Math class a few years back.

                                  Whatever the a values were, we multiplied it by c. Then we had to find the factors of that number that added/subtracted to get b.

                                  It would always. Though sometimes it is a decimal/fraction, so that's when you need to use the quadratic equation(or that's what I would do)