Results 1 to 4 of 4
  1. #1
    frostkarrotor is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default need help with factor quadratic...

    Java Code:
    //it displays the error java.util.ConcurrentModificationException when it runs. i believe something is wrong with the getQuadraticFactors or getIntegerFactors. please give me advices...
    
    import java.util.ArrayList;
    
    public class QuadraticEqn {
    	public static void main(String[] args) {
    		QuadraticEqn eqn = new QuadraticEqn();
    		eqn.setABC(1, -2, 3);
    		System.out.println(eqn.toString());
    
    		QuadraticEqn eqn2 = new QuadraticEqn();
    		eqn2.setABC(1, -6, 9);
    		//System.out.println(eqn2.getQuadraticFactors));
    		System.out.println(eqn2.toString());
    
    	}
    
    	private int coeA;
    	private int coeB;
    	private int coeC;
    	ArrayList<Integer> listA = new ArrayList<Integer>();
    	ArrayList<Integer> listB = new ArrayList<Integer>();
    
    	public void setABC(int a, int b, int c) {
    		coeA = a;
    		coeB = b;
    		coeC = c;
    
    	}
    
    	public String toString() {
    		return coeA + "x^2" + " + " + coeB + "x" + " + " + coeC;
    	}
    
    	public ArrayList<Integer> getIntegerFactors(int n) {
    		for (int a = 1; a <= n; a++) {
    			if (a == 1 || a == n || n % a == 0) {
    				listA.add(n);
    				listA.add(-1 * n);
    			}
    		}
    		return listA;
    
    	}
    
    	public ArrayList<Integer> getIntegerFactors2(int c) {
    		for (int a = 1; a <= c; a++) {
    			if (a == 1 || a == c || c % a == 0) {
    				listB.add(c);
    				listB.add(-1 * c);
    
    			}
    		}
    		return listB;
    	}
    
    	public String getQuadraticFactors() {
    		String str = "";
    		for (Integer P : getIntegerFactors(coeA)) {
    			for (Integer Q : getIntegerFactors2(coeC)) {
    				for (Integer R : getIntegerFactors(coeA)) {
    					for (Integer S : getIntegerFactors(coeC)) {
    						if (P*S + Q*R== coeB 
    								&& P*R == coeA 
    								&& Q*S== coeC) {
    					
    							str = "(" + P + "x +" + Q + ")" + "(" + R + "x + " + S + ")";
    							return str;
    						}		
    					}
    				}
    			}
    		}
    		
    		return "PRIME";
    		
    
    	}
    }
    Last edited by frostkarrotor; 11-29-2010 at 02:32 AM. Reason: Moderator Edit: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Do listA and listB have to be instance variables? If not declare them in the methods that create and return them.

    The inner for loops are altering the contents of listA while the outer ones are still iterating over it, which doesn't make a lot of sense.

  3. #3
    frostkarrotor is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    declare them in the quadraticFactors or getIntegerFactors? when u say the outer ones are still iterating, do you mean the one in getQuadraticFactor or getIntegerFactors?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    listA and listB are created and returned from the getIntegerFactors() and getIntegerFactors2() methods. What I am suggesting is that you not use them as instance variables.

    The nested for loops occur in the getQuadraticFactors() method. Do you see how the R and S loops will be altering the contents of listA while P loop is still trying to iterate over it?

Similar Threads

  1. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 09:25 PM
  2. greatest prime factor
    By java_prgr in forum New To Java
    Replies: 2
    Last Post: 07-23-2010, 09:28 PM
  3. Returning the Greatest Prime Factor
    By BJ1110 in forum New To Java
    Replies: 15
    Last Post: 10-23-2009, 11:06 PM
  4. Quadratic Equation
    By jpnym15 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 04:29 AM
  5. Quadratic GUI
    By ryn21 in forum New To Java
    Replies: 1
    Last Post: 10-30-2008, 06:58 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
  •