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.


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

Results 1 to 2 of 2

Thread: Problem with first attempt at using my own classes

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Thanked 0 Times in 0 Posts

    Default Problem with first attempt at using my own classes

    So in the code below I create an instance of my own triangle class and use one of its methods. The thing is I use one of my triangle classes methods in a method other the main method of my main program so I'm thinking it can't access it?

    anyway heres the code for my triangle class

    import java.util.Scanner;
    public class QudratullahMommandi_Triangle_06
      Scanner keyboard = new Scanner(System.in);
       private double side1;
       private double side2;
       private double side3;
      public QudratullahMommandi_Triangle_06()
      { side1 = 0;
        side2 = 0;
        side3 = 0;
      public QudratullahMommandi_Triangle_06(double sideOne, double sideTwo, double sideThree)
      { side1 = sideOne;
        side2 = sideTwo;
        side3 = sideThree;
      public void giveLength(double firstSide, double secondSide, double thirdSide)
      { while (firstSide <= 0 ){
          System.out.println("The first side must be greater then zero " 
                              + "please enter a new value");
           firstSide = keyboard.nextDouble();
        while (secondSide <=0){
          System.out.println("The second side must be greater then zero " 
                             + "please enter a new value");
           secondSide = keyboard.nextDouble();
        while (thirdSide <=0){
          System.out.println("The third side must be greater then zero " 
                              + "please enter a new value");
           thirdSide = keyboard.nextDouble();
         side1 = firstSide;
         side2 = secondSide;
         side3 = thirdSide;
       }// end giveLength
       public void outPut()
       { System.out.println("The first side is " + side1 +"\n" +
                            "The second side is " + side2 + "\n" +
                            "The third side is " + side3);
       }// end outPut
       public double getPerim()
       { double perimeter = side1 + side2 + side3;
         return perimeter;
       }// end getPerim
       public double getArea()
       { double semiP = (side1+side2+side3)/2;
         double area = Math.sqrt(semiP*(semiP-side1)*(semiP-side2)*(semiP-side3));
         return area;
       }// end getArea

    Here's the code for my other class in which I use the triangle class
    import java.util.Scanner;
    public class QudratullahMommandi_S_06
       public static void main (String[]args)
       QudratullahMommandi_Triangle_06 triangle1 = new QudratullahMommandi_Triangle_06();
       double perimeter;
       double area;
       perimeter = triangle1.getPerim();
       area = triangle1.getArea();
        }// end main
       public static void Hello()
        { System.out.println("This program uses the triangle class and its methods\n" 
             + "to calculate the perimeter and area of a triangle given three side lengths");
              System.out.println("Qudratullah Mommandi");
       public static String Output()
        { triangle1.outPut();
    }// end class

    and here's the error message

    QudratullahMommandi_S_06.java:46: error: cannot find symbol
    { triangle1.outPut();
    symbol: variable triangle1
    location: class QudratullahMommandi_S_06
    1 error

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: Problem with first attempt at using my own classes

    The variable traingle1 is declared and initialized inside the main() method so is local to main(). If you want to use triangle1 outside the main() method, then declare the variable as a class variable.

Similar Threads

  1. Affecting Point objects with methods - a beginner's failed attempt
    By thatolkevin in forum Object Oriented Programming
    Replies: 10
    Last Post: July 16th, 2013, 08:50 AM
  2. How would one attempt to create an algorithm for this?
    By RedHawkLuffy in forum Java Theory & Questions
    Replies: 2
    Last Post: March 30th, 2013, 01:51 PM
  3. Brand New/First Attempt
    By kevinco in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2011, 08:45 PM
  4. Java dialog box is blank in 2nd attempt while using timer.
    By mocherla81 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 17th, 2011, 09:49 AM