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

Thread: Need help with debugging

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with debugging

    Any of my methods from Triangle with parameters don't work with their calls in TriangleDriver. I get an error:
    method validSides cannot be applied to given types;
    required: int, int, int found : no arguments; reason: actual and formal argument lists differ in length

    Triangle
    import java.util.Scanner;
     
    public class Triangle {
     
        private int a, b, c;
     
        public Triangle() {
     
            a = 0;
            b = 0;
            c = 0;
        }
     
        public String  toString(int sideA, int sideB, int sideC) {
     
            return sideA + " " + sideB + " " + sideC;
        }
     
        public boolean validSides(int sideA, int sideB, int sideC) {
     
            boolean valid = true;
            if (sideA < 0 || sideB < 0 || sideC < 0)
                valid = false;
     
            return valid;
        }
     
        public void setSides() {
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("Enter three integers to form a triangle:  ");
            a = keyboard.nextInt();
            b = keyboard.nextInt();
            c = keyboard.nextInt();
     
        }
     
        public boolean displayType(int sideA, int sideB, int sideC) {
     
            boolean validTriangle = true;
            if ( sideA + sideB <= sideC ) {
                System.out.println("Triangle Type: DOES NOT FORM TRIANGLE");
                validTriangle = false;
            }
            else if ( sideA ==  sideB && sideB == sideC) 
                System.out.println("Triangle Type: EQUILATERAL");
            else if ( sideA ==  sideB)
                System.out.println("Triangle Type: ISOSCELES");
            else
                System.out.println("Triangle Type: SCALENE");
     
            return validTriangle;
        }
     
        public int getPerimeter(int sideA, int sideB, int sideC) {
     
            int perimeter = sideA + sideB + sideC;
     
            return perimeter;
        }
     
        private void sortSides(int a, int b, int c) {
            int swap;
            if ( a > b) {
                swap = a;
                a = b;
                b = swap;
            }
     
            if ( a > c) {
                swap = a;
                a = c;
                c = swap;
            }
     
            if ( b > c ) {
                swap = b;
                b = c;
                c = swap;
            }
        }
    }

    TriangleDriver
    /** Description: This program will continually get three integers from a user to 
     *               represent the three sides of a triangle. They are displayed in
     *               sorted order.  It is determined whether the three integers 
     *               don't form a triangle and if they do, whether it's equilateral, 
     *               isosceles, or scalene.  If the triangle is valid, its perimeter
     *               is displayed.         
     *  Filename:    TriangleDriver.java
     *  Project:     Assignment 5
     *  Author:      Joey Benitez
     *  Due Date:    11/06/2013
    */
     
    import java.util.Scanner;
     
    public class TriangleDriver {
     
        public static void main (String[] args) {
     
            Triangle theTriangle = new Triangle();
            Scanner keyboard = new Scanner(System.in);
            String playAgain;
            String sortedSides;
            boolean validEntries;
            boolean validTriangle;
     
            // Continue as long as the user wants to enter Triangle sides 
            do {
                theTriangle.setSides();
                validEntries = theTriangle.validSides();
                if ( validEntries ) {
                    theTriangle.sortSides();
                    sortedSides = theTriangle.toString();
                    System.out.println("Sides sorted:  " + sortedSides);
                    validTriangle = theTriangle.displayType();
                    if ( validTriangle ) {
                        System.out.println("Perimeter:  " + 
                                theTriangle.getPerimeter());
                    }
                }
                else {
                    System.out.println("INVALID ENTRY FOR SIDES"); 
                }
     
                System.out.println();
                System.out.print("Would you like to enter more triangles?  ");
                playAgain = keyboard.next();
                System.out.println();
     
            } while (playAgain.toUpperCase().startsWith("Y"));
     
            System.out.println("Program Completed");
       }
    }


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    347
    My Mood
    Busy
    Thanks
    48
    Thanked 105 Times in 88 Posts
    Blog Entries
    5

    Default Re: Need help with debugging

    Your Object Orientation is a little off in the Triangle class. You should accept the three sides as parameters for the constructor and set the members:
    public Triangle(int sideA, int sideB, int sideC) {
        a = sideA;
        b = sideB;
        c = sideC;
    }

    Now the other methods of Triangle can use the a, b & c and you can create instances of a triangle in your loop with:
    Triangle t = new Triangle(2, 3, 4);

Similar Threads

  1. Need help debugging
    By Zephyrean in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 13th, 2013, 08:06 AM
  2. Error when debugging
    By Montrell79 in forum Java IDEs
    Replies: 0
    Last Post: March 14th, 2012, 06:07 PM
  3. Help with Error Debugging
    By bananasplitkids in forum Object Oriented Programming
    Replies: 3
    Last Post: March 9th, 2010, 09:16 AM
  4. Debugging
    By Altaf in forum Java IDEs
    Replies: 0
    Last Post: December 6th, 2009, 09:16 PM
  5. Debugging your program
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 18th, 2009, 02:14 AM