Jump to content

  • Log In with Google      Sign In   
  • Create Account

Nested Class not working properly


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Czar05   Members   -  Reputation: 930

Like
0Likes
Like

Posted 16 November 2013 - 04:44 AM

Can somebody help me figure out the problem in the following code sample. My IDE says I have a mistake but I can't detect it. I am having trouble with the Inner Class.

 

I am using eclipse if it helps.



public class Outer {

    

    int nums[];

    

    Outer(int n[])

    {

        nums = n;

    }

    

    void Analyze()

    {

        Inner inOb = new Inner();

        

        System.out.println("Minimum: " + inOb.min());

        System.out.println("Maximum: " + inOb.min());

        System.out.println("Average: " + inOb.min());

        

    }



    

 class Inner()

    {

    

        int min()

        {

            int m = nums[0];

            for(int i = 1; i < nums.length; i++)

                if(nums[i]<m)

                    m = nums[i];

            return m;

        }

        

        int max()

        {

            int m = nums[0];

            for(int i = 1; i < nums.length; i++)

                if(nums[i] > m)

                    m = nums[i];

            

            return m;

        }

        

        int avg()

        {

            int a = 0;

            for(int i = 0; i < nums.length; i++)

                a += nums[i];

            

            return a / nums.length;

        }

    }

}


Sponsor:

#2 Dragonsoulj   Crossbones+   -  Reputation: 2111

Like
0Likes
Like

Posted 16 November 2013 - 04:57 AM

I didn't think you nested classes. I thought you used something like extend.



#3 Glass_Knife   Moderators   -  Reputation: 4447

Like
0Likes
Like

Posted 16 November 2013 - 06:32 AM

Instead of this:

class Inner() {

}

you need this:

class Inner {

}

I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#4 Glass_Knife   Moderators   -  Reputation: 4447

Like
0Likes
Like

Posted 16 November 2013 - 06:37 AM

I didn't think you nested classes. I thought you used something like extend.

 

Java requires only on class per file, but inside that class you can declare other classes.  Check out the Point2D.Double class from Java to see an example:

http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Point2D.Double.html

 

Extend is for using inheritance, which is something different.


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#5 Czar05   Members   -  Reputation: 930

Like
0Likes
Like

Posted 16 November 2013 - 03:14 PM

Thanks @Glass_Knife, I feel silly now because the error looks obvious now. Guess that's what I get for coding late Lol.



#6 Dragonsoulj   Crossbones+   -  Reputation: 2111

Like
0Likes
Like

Posted 16 November 2013 - 03:59 PM

 

I didn't think you nested classes. I thought you used something like extend.

 

Java requires only on class per file, but inside that class you can declare other classes.  Check out the Point2D.Double class from Java to see an example:

http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Point2D.Double.html

 

Extend is for using inheritance, which is something different.

 

 

It's been a while since I worked with Java. Guess I need a refresher project.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS