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

Thread: How can I access my secondary interface method in my for loop?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default How can I access my secondary interface method in my for loop?

    I can't access my el.print() in my for loop. These are four different files. I have two interfaces with separate names. I can't combine them or add or remove and new methods. I can change current methods however. There are similar classes as to triangle for square, rectangle, etc. Circle cannot access Printable.print, the other three can.

    public class InterfaceApp 
    {
        public static void main( String[] args )
        {
            Rectangle myRectangle = new Rectangle( 6, 3 );
            Square mySquare = new Square( 5, 5 );
            Triangle myTriangle = new Triangle( 6 );
            Circle myCircle = new Circle( 5 );
     
            System.out.println( myRectangle );
            System.out.println();
            System.out.println( mySquare );
            System.out.println();
            System.out.println( myTriangle );
            System.out.println();
            System.out.printf( myCircle + "%nDiameter: %d%nCircumference: %.1f%n%n",
                    myCircle.diameter(), myCircle.circumference() );
     
            System.out.printf( "Shape Array: %n-----------%n" );
     
            Shape[] myShapes = { mySquare, myRectangle, myTriangle, myCircle };
     
            mySquare.print();
            myRectangle.print();
            myTriangle.print();
     
            for ( Shape el : myShapes )
            {
     
                System.out.printf( el + "%nPerimeter: %.1f%nArea: %.1f%n", 
                        el.perimeter(), el.area() );
     
                if (!( el instanceof Circle ))
                {
                    el.print();
                }
     
            }
        }
    }
     
    public class Triangle implements Shape, Printable
    {
        private final int leg;
        private final double hypotenuse;
     
        public Triangle( int l )
        {
            leg = l;
            hypotenuse = leg*Math.sqrt( 2.0 );
        }    
     
        public int getLeg()
        {
            return leg;
        }
     
        public double getHypotenuse()
        {
            return hypotenuse;
        }
     
        @Override
        public String toString()
        {
            return String.format( "Triangle(%d)", leg );
        }
     
        @Override
        public double perimeter() 
        {
            return 2*leg + hypotenuse;
        }
     
        @Override
        public double area() 
        {
            return .5*(Math.pow( leg,  2 ));    
        }
     
        @Override
        public void print() 
        {
            String gap = "";
            System.out.println( "* " );
            for ( int i = 1; i < leg; i++ )
            {
     
                if ( i == leg - 1 )
                {
                    for ( int j = 0; j < leg; j++ )
                    {
                        if ( j == leg - 1 )
                        {
                            System.out.println( "* ");
                        }
                        else
                        {
                        System.out.printf( "* " );
                        }
                    }
                }
                else
                {
                    if ( i == 1 )
                    {
                        gap = "";
                    }
                    else 
                    {
                        gap += "  ";
                    }
                System.out.println( "* " + gap + "* " );
                }
            }
        }
    }
     
    public interface Shape 
    {
        double perimeter();
        double area();
    }
     
    public interface Printable 
    {
        void print();
    }


  2. #2
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I access my secondary interface method in my for loop?

    It could be because im tired but i dont see where el is declared.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I access my secondary interface method in my for loop?

    I can't access
    If there are error messages, copy the full text of the messages and paste it here.


    The Shape interface does not have a print() method.
    The Printable interface does have a print() method. Make el a Printable.
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    bdennin (February 12th, 2013)

  5. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How can I access my secondary interface method in my for loop?

    I just extended Shapes with Printable. Problem solved.

Similar Threads

  1. Opening new window to secondary monitor
    By cutemover in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 17th, 2013, 08:32 AM
  2. method not passing across actionlistener interface
    By flamewolf393 in forum AWT / Java Swing
    Replies: 9
    Last Post: November 16th, 2011, 07:16 AM
  3. Access Method
    By CompScienceStudent1 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 21st, 2011, 08:56 AM
  4. Method access problem
    By hello_world in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2011, 03:39 PM
  5. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM