problem after for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pucca
    New Member
    • Jul 2007
    • 11

    problem after for loop

    Hi !!
    This is my for loop:
    Shape[] shapes=new Shape[10];

    for(int i=0;i<shapes.le ngth;i++)
    {
    System.out.prin tln("\n");
    shapes[i].printState();
    area+=shapes[i].area();
    System.out.prin tln(area);

    }
    My problem is that when i put the System.out.prin tln() out of the for loop it doesn't run( won't print anything).I can't understand why is doing it??I would need some advise :) thanks
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by pucca
    Hi !!
    This is my for loop:
    Shape[] shapes=new Shape[10];

    for(int i=0;i<shapes.le ngth;i++)
    {
    System.out.prin tln("\n");
    shapes[i].printState();
    area+=shapes[i].area();
    System.out.prin tln(area);

    }
    My problem is that when i put the System.out.prin tln() out of the for loop it doesn't run( won't print anything).I can't understand why is doing it??I would need some advise :) thanks
    Your question can't be properly answered without a bit more context (code
    around the code you've shown us).

    Where is the 'area' variable defined? Does this compile and run?

    [code=java]
    Shape[] shapes=new Shape[10];

    for(int i=0;i<shapes.le ngth;i++)
    {
    System.out.prin tln("\n");
    shapes[i].printState();
    area+=shapes[i].area();
    }
    System.out.prin tln(area);
    [/code]

    Note that I put [ code=java ] ... [ /code ] tags around the code (without the spaces).
    It's more readable like that and it preserves the indentation.

    kind regards,

    Jos

    Comment

    • pucca
      New Member
      • Jul 2007
      • 11

      #3
      Code:
      public class Area{
         public static void main(String[] argv){
              Shape[] shapes=new Shape[10];
              shapes[0]=new Circle(40.0,40.0,30.0);
              shapes[1]=new Rectangle(20.0,20.0,80.0,120.0);
              double area=0.0;
                          for(int i=0;i<shapes.length;i++){      
                               shapes[i].printState();
                               area+=shapes[i].area();}
              System.out.println(area);
          }
      The program compiles and runs without printing....

      thanks for the advises about presenting the code ;)
      Last edited by JosAH; Jul 18 '07, 08:05 PM. Reason: I put in the [code] ... [/code] tags in again

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by pucca
        [code=java]
        public class Area{
        public static void main(String[] argv){
        Shape[] shapes=new Shape[10];
        shapes[0]=new Circle(40.0,40. 0,30.0);
        shapes[1]=new Rectangle(20.0, 20.0,80.0,120.0 );
        double area=0.0;
        for(int i=0;i<shapes.le ngth;i++){
        shapes[i].printState();
        area+=shapes[i].area();}
        System.out.prin tln(area);
        [/code]

        The program compiles and runs without printing....

        thanks for the advises about presenting the code ;)
        It throws a NullPointerExce ption at you, doesn't it?

        kind regards,

        Jos

        Comment

        • pucca
          New Member
          • Jul 2007
          • 11

          #5
          yes it throws me a Nullpointer!!

          Comment

          • prometheuzz
            Recognized Expert New Member
            • Apr 2007
            • 197

            #6
            Originally posted by pucca
            yes it throws me a Nullpointer!!
            That's because 8 of the 10 "slots" in your array are occupied by null references on which you're trying to invoke a method. WHen trying to do such a thing, a NPE will be thrown.

            Comment

            • pucca
              New Member
              • Jul 2007
              • 11

              #7
              thanks for the tip it worked...:) !! I would like also some advise of how to make a class...I'm doing a program using a Cicle class ,a Rectangle class and a stack.Without using an abstract Shape class I want a class that has two data fields one reference to the object and an int for the type,so every time that i put something out of the stack I know if it is a circle or a rectanle.....

              Comment

              • pucca
                New Member
                • Jul 2007
                • 11

                #8
                Code:
                class Object{
                private int type;
                   
                  whatObject(){
                    if(type==1)
                       System.out.println("circle");
                    else if(type==2)
                       System.out.println("rectangle");
                    
                    }
                   Circle  Object(Circle ob){type=1;return ob;}
                   Rectangle Object(Rectangle ob){type=2;return ob;}  
                }
                this is an effort to do the class.......... ..

                Comment

                • prometheuzz
                  Recognized Expert New Member
                  • Apr 2007
                  • 197

                  #9
                  Originally posted by pucca
                  Code:
                  class Object{
                  private int type;
                     
                    whatObject(){
                      if(type==1)
                         System.out.println("circle");
                      else if(type==2)
                         System.out.println("rectangle");
                      
                      }
                     Circle  Object(Circle ob){type=1;return ob;}
                     Rectangle Object(Rectangle ob){type=2;return ob;}  
                  }
                  this is an effort to do the class.......... ..
                  It is not a good idea to name your class Object since all classes (and interfaces... and enums) extend the java.lang.Objec t class.
                  But I'm not quite sure what it is you're trying to do with this class above. Care to elaborate a bit more about it?.

                  Comment

                  • pucca
                    New Member
                    • Jul 2007
                    • 11

                    #10
                    Code:
                    import java.util.Stack;
                    public class MyFirstProgramv2{
                        public static void main(String[] argv){
                    
                        Stack st=new Stack;
                        Shape[] shapes=new Shape[10];
                           
                        CIrcle c1=new Circle(1.0,3.0,2.0);  
                        st.push(c1);  
                        Rectangle rec1=new Rectangle(1.0,1.0,3.0,3.0);
                        st.push(rec1);
                        Circle c2=new Circle(2.0,4.0,1.0);
                        st.push(c2);
                        Rectangle rec2=new Rectangle(1.0,0.0,40.0,5.0);
                        st.push(rec2);
                        }
                    }
                    
                    class Circle{
                    private double x,y,radius;
                    public static final double PI=3.1415926535;
                    
                       public Circle(double x,double y,double radius){
                        this.x=x;
                        this.y=y;
                        this.radius=radius;
                        }   
                       public double circumference(){return 2*PI*radius;}
                       public double area(){return PI*radius*radius;}
                       public void printState(){
                        System.out.println("circle state:");
                        System.out.println("(x,y):"+"(" + x +"," + y+")");
                        System.out.println("radius="+radius+"\n");                                               }
                    
                    }
                    
                    class Rectangle{
                    private double x1,y1,x2,y2,w,h;
                      public Rectangle(double x1,double y1,double x2,double y2){
                          this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; w=x2-x1; h=y2-y1;
                         }
                      public double circumference() { return 2*(w+h);}       
                      public double area() { return (w*h);}
                    
                      public void printState()
                         {System.out.println("rectangle state:");
                          System.out.println("w="+w);
                          System.out.println("h="+h+"\n"); }
                    
                    }

                    This is my code(until now).I have to make a class that will help me uderstand the type of the object that pops out of the stack....every time...this class will have two data fields an int and a reference to an object
                    (circle,rectang le).Every time that my program makes an instance of a circle or rec an instance of this class will have to be also made updating its int data member with the type of the object(circle or rec)..Then the instance of this class will be stored in stack...I know it's a bit confusing sorry if i dizzled you ...I have to do it this way...thanks a lot lot..:) .

                    Comment

                    • prometheuzz
                      Recognized Expert New Member
                      • Apr 2007
                      • 197

                      #11
                      Two questions:
                      - why do you create an array of Shape objects while you're not doing anything with it?
                      - shouldn't Circle and Rectangle have a common (abstract) super class called Shape or implement a Shape interface?

                      As for your requirement, I still don't know exactly what you mean. But I think you can use the instanceof operator somewhere.

                      [CODE=java]Circle c = new Circle(/* ... */);
                      // ...
                      if(c instanceof Circle) { // for 'c', this is true
                      // ...
                      }[/CODE]

                      Comment

                      • emekadavid
                        New Member
                        • Mar 2007
                        • 46

                        #12
                        Originally posted by pucca
                        thanks for the tip it worked...:) !! I would like also some advise of how to make a class...I'm doing a program using a Cicle class ,a Rectangle class and a stack.Without using an abstract Shape class I want a class that has two data fields one reference to the object and an int for the type,so every time that i put something out of the stack I know if it is a circle or a rectanle.....
                        this is with regard to your last line "everytime i ... a circle or a rectangle". let's assume we're not working with regard to circles or rectangles here. i'll give you some principle on what you want to do. what you should use is a double ended queue (a deque) but am going to use a list interface to implement this.

                        Code:
                        //your class and other trivia came before. 
                            public static String yourObject(List<?> dList){
                                //assuming we're poping the object in list, LIFO
                                return dList.remove(dList.size()-1).getClass().toString();
                            }
                            public static void main(String[] args){
                                String str = new String("yourString");
                                Integer inter = new Integer(532);
                                List dList = new ArrayList(); //not generic
                                dList.add(str);
                                dList.add(inter);
                                dList.add(str);
                                //now our list contains three elements without an interface
                                //let's pop all last element from the list
                                System.out.println("The class of the last object in list is"+
                                        yourObject(dList));
                                
                            }
                        i hope you do get what i did on principle. implement your code that way. the returned string will tell you what your object was, whether it was a circle or rectangle

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by prometheuzz
                          It is not a good idea to name your class Object since all classes (and interfaces... and enums) extend the java.lang.Objec t class.
                          I have classes named If, While, Do, For etc. Any problems with that? ;-)

                          kind regards,

                          Jos

                          Comment

                          • prometheuzz
                            Recognized Expert New Member
                            • Apr 2007
                            • 197

                            #14
                            Originally posted by JosAH
                            I have classes named If, While, Do, For etc. Any problems with that? ;-)

                            kind regards,

                            Jos
                            Lemme guess, you named them like this:

                            Code:
                            If _if;
                            While _while;
                            // ...
                            ; )

                            But now you're here, care to have a look at my question at the Networking forums?

                            Thanks!

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by prometheuzz
                              Lemme guess, you named them like this:

                              Code:
                              If _if;
                              While _while;
                              // ...
                              ; )

                              But now you're here, care to have a look at my question at the Networking forums?

                              Thanks!
                              I bet your modem is a router too, isn't it? If so, make your wireless router a
                              simple access point (some of these vague settings in its web interface) and turn
                              off dhcp in the modem/router. That's the way I do it with my ISP's 'life box' and
                              my wireless router.

                              kind regards,

                              Jos

                              ps. yep: If, _if and all that for obvious reasons ;-)

                              Comment

                              Working...