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 7 of 7

Thread: Triangles problem - creating triangle objects

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Triangles problem - creating triangle objects

    I want to create Triangle objects and use them, what am I doing wrong? I have triangle.java here, and CTriangle and STriangle have similar issues.

    public class Triangle extends Object {
     
        // Include some appropriate fields.  These should not be public.
        // Your triangles are defined by side lengths, which are positive ints.
     
        // Put your own Javadoc comments for all methods please.
        // Here a, b, c should be the side lengths.
        // Throw an IllegalArgumentException if any of the following happen:
        // a, b, c are not all positive
        // a, b, c do not obey the triangle inequality.
     
    	private static int a;
    	private static int b;
    	private static int c;
     
    	public Triangle (int a, int b, int c) throws IllegalArgumentException {
    		if (isTriangle (a,b,c)==false) throw new IllegalArgumentException("Not a Triangle!");
    		Triangle.a = a;
    		Triangle.b = b;
    		Triangle.c = c;
    	}
     
    	public static int[ ] getSideLengths ( ) {
    		Triangle T1 = new Triangle(1,1,1);
            Triangle T2 = new Triangle(1,1,1);
    		System.out.println("Please enter the six inputs, each followed by hitting the enter key.");
        	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String input = null;
            try {
            	input = br.readLine();
            	T1.a = Integer.parseInt(input);
            	System.out.println("T1.a = " + T1.a);
            	input = br.readLine();
            	T1.b = Integer.parseInt(input);
            	System.out.println("T1.b = " + T1.b);
            	input = br.readLine();
            	T1.c = Integer.parseInt(input);
            	System.out.println("T1.c = " + T1.c);
            	input = br.readLine();
            	T2.a = Integer.parseInt(input);
            	System.out.println("T2.a = " + T2.a);
            	input = br.readLine();
            	T2.b = Integer.parseInt(input);
            	System.out.println("T2.b = " + T2.b);
            	input = br.readLine();
            	T2.c = Integer.parseInt(input);
            	System.out.println("T2.c = " + T2.c);
            } 
            catch (IOException e) {
            	System.out.println("Error!");
            	System.exit(1);
            }
    		int[] sidelengths = {T1.a, T1.b, T1.c, T2.a, T2.b, T2.c}; 
           	for (int i=0; i<sidelengths.length; i++) {
           		System.out.print(sidelengths[i] + " , ");
           	}
    		return sidelengths;
    	}
     
     
        	/*public boolean STriangle (int a, int b, int c){
        	boolean striangle = false;
        	if (a==b && b==c) striangle = true;
        	return striangle;
        }*/
     
    	// We explicitly won't be using the following getAngles method.  
        // Please DON'T implement it.
        // Using angles to test for similarity is risky, since the values you
        // get back may be subject to rounding error.
        //
        // return the 3 angles, in radians.  These should sum to 3.14159...
        // public double[ ] getAngles ( ) {
        // }
     
        // do not include any setter methods.
        // Triangles are not mutable.
     
        // true if the triangle contains a right angle.
        public static boolean isRight (int a, int b, int c) {
        	if ((a*a)+(b*b)==(c*c)) return true;
        	if ((b*b)+(c*c)==(a*a)) return true;
        	if ((a*a)+(c*c)==(b*b)) return true;
        	else return false;
        	/* formula for a right triangle
        	 *  
        	 */
        }
     
        // true if the triangle contains an angle greater than 90 degrees.
        public static boolean isObtuse (int a, int b, int c ) {
        	if ((a*a)+(b*b)<(c*c)) return true;
        	else return false;
        	/* formula for an obtuse triangle
        	 *  
        	 */
        }
     
        // true if the triangle is not right or obtuse.
        public static boolean isAcute ( int a, int b, int c) {
        	if ((a*a)+(b*b)>(c*c)) return true;
        	else return false;    	
        	/* formula for an acute triangle
        	 *  
        	 */
        }
     
        // true if a,b,c are the side lengths for a legitimate triangle.
        // useful for testing arguments you want to pass to the constructor.
        public static boolean isTriangle (int a, int b, int c) {
        	if (a+b<c || a<=0) return false;
        	if (a+c<b || b<=0) return false;
        	if (b+c<a || c<=0) return false;
        	else return true;
        }
     
        // always want one of these.
        public static String toString(Triangle d) {
        	return "(" +d.a+" ,"+ d.b+" ,"+ d.c+ ")";
        }
     
     
        // This is the method you'll need in part 2.
        // @Override 
        // public boolean equals (Object o) {
        // }
     
        public static void main(String [] args){
        	Triangle T1 = new Triangle(1,1,1);
            Triangle T2 = new Triangle(1,1,1);
        	int[] a1 = (getSideLengths());
        	/*test*/ System.out.println("To String - " + toString(T1));
        	if (T1.isTriangle(a, b, c)==true){
            	System.out.println("The first one is a triangle");
            	System.out.print("The second one is "); 
            	if (T1.isTriangle(a, b, c)==false) System.out.print("not ");
            	System.out.println("a triangle");
            	if (T1.isAcute(a, b, c)==true && T1.isRight(a, b, c)==false) System.out.println("The first one is an acute triangle");
            	if (T2.isAcute(a, b, c)==true && T2.isRight(a, b, c)==false) System.out.println("The second one is an acute triangle");
            	if (T1.isObtuse(a, b, c)==true && T1.isRight(a, b, c)==false) System.out.println("The first one is an obtuse triangle");
            	if (T2.isObtuse(a, b, c)==true && T2.isRight(a, b, c)==false) System.out.println("The second one is an obtuse triangle");
            	if (T1.isRight(a, b, c)==true) System.out.println("The first one is a right triangle");
            	if (T2.isRight(a, b, c)==true) System.out.println("The second one is a right triangle");
            	if (Triangle.CTriangle(a, b, c, a, b, c)==true) {
            		System.out.println("Those are congruent triangles");
            		System.out.println("Those are similar triangles");
            	}
            	else if (Triangle.STriangle(a, b, c, a, b, c)==true) System.out.println("Those are similar triangles");
            	}
            else System.out.println("The first one is not a triangle");
        	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,106
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Triangles problem - creating triangle objects

    Can you explain what the "issues" are?

    Where are CTriangle and STriangle defined?
    Last edited by Norm; May 15th, 2011 at 07:46 AM.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Triangles problem - creating triangle objects

    A Triangle object is suppose to be able to be defined as three sides, then be able to run the methods isacute(), isobtuse(), isright(), istriangle(), and CTriangle() is a class defined as a two comparable triangles where the side lengths are the same, regardless of the order. STriangle() means two similar triangles which have the same ratio of side lengths regardless of order. I think I need to implement arrays for the side lengths instead of three ints, but I'm not sure. Thanks.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,106
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Triangles problem - creating triangle objects

    Your posted code does NOT compile because those two classes I mentioned are NOT defined.

    Why did you post code that doesn't compile without questions about the problems with the code?
    what am I doing wrong?
    You are not coding definitions for the two classes.

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Triangles problem - creating triangle objects

    CTriangle class below:

    I know my code doesn't compile, that's why I need help. I have been trying different ways and now I'm confused about what to take out and what to put in so that I'm doing it one way that works.

    public class CTriangle {
     
    	boolean isCTriangle=false;
     
    	public static boolean CTriangle (int a1, int a2, int b1, int b2, int c1, int c2){
        	boolean ctriangle = false;
        	if (a1==a2 && b1==a2 && c1==a2) ctriangle = true;
        	if (a1==a2 && b1==a2 && c1==b2) ctriangle = true;
        	if (a1==a2 && b1==a2 && c1==c2) ctriangle = true;
        	if (a1==a2 && b1==b2 && c1==a2) ctriangle = true;
        	if (a1==a2 && b1==b2 && c1==b2) ctriangle = true;
        	if (a1==a2 && b1==b2 && c1==c2) ctriangle = true;
        	if (a1==a2 && b1==c2 && c1==a2) ctriangle = true;
        	if (a1==a2 && b1==c2 && c1==b2) ctriangle = true;
        	if (a1==a2 && b1==c2 && c1==c2) ctriangle = true;
        	if (a1==b2 && b1==a2 && c1==a2) ctriangle = true;
        	if (a1==b2 && b1==a2 && c1==b2) ctriangle = true;
        	if (a1==b2 && b1==a2 && c1==c2) ctriangle = true;
        	if (a1==b2 && b1==b2 && c1==a2) ctriangle = true;
        	if (a1==b2 && b1==b2 && c1==b2) ctriangle = true;
        	if (a1==b2 && b1==b2 && c1==c2) ctriangle = true;
        	if (a1==b2 && b1==c2 && c1==a2) ctriangle = true;
        	if (a1==b2 && b1==c2 && c1==b2) ctriangle = true;
        	if (a1==b2 && b1==c2 && c1==c2) ctriangle = true;
        	if (a1==c2 && b1==a2 && c1==a2) ctriangle = true;
        	if (a1==c2 && b1==a2 && c1==b2) ctriangle = true;
        	if (a1==c2 && b1==a2 && c1==c2) ctriangle = true;
        	if (a1==c2 && b1==b2 && c1==a2) ctriangle = true;
        	if (a1==c2 && b1==b2 && c1==b2) ctriangle = true;
        	if (a1==c2 && b1==b2 && c1==c2) ctriangle = true;
        	if (a1==c2 && b1==c2 && c1==a2) ctriangle = true;
        	if (a1==c2 && b1==c2 && c1==b2) ctriangle = true;
        	if (a1==c2 && b1==c2 && c1==c2) ctriangle = true;
        	return ctriangle;
        }
    	//return isCTriangle;
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,106
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Triangles problem - creating triangle objects

    What is the CTriangle method you posted supposed to do?
    Can you explain why you have all the if tests. What are they supposed to determine?
    Is it possible for more than one of them to be true?
    Do you need to make further if tests after on if test is true? In other words, use if/else if so that you stop testing when you find the first true results.

  7. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Triangles problem - creating triangle objects

    If you get errors, post up the full text of the error message(s) and the relevant code.

Similar Threads

  1. Problem with automatically creating new objects with unique names
    By oniamien in forum Java Theory & Questions
    Replies: 2
    Last Post: December 4th, 2010, 01:38 PM
  2. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 08:22 PM
  3. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM
  4. Loop Patterns (triangles) [Beginner]
    By me1010109 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 24th, 2010, 05:13 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM

Tags for this Thread