Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Instantiate an array of objects and class stuff
Index -> Programming, Java -> Java Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Justin_




PostPosted: Sat Apr 08, 2006 12:56 pm   Post subject: Instantiate an array of objects and class stuff

The main problem I'm having is on line 13, where I'm trying to instantiate 26 Memory objects. But the other problem is I'm not sure if I should have put the Memory class with my SmarterHoudini or if I should have put it in the implementation program which simply instantiates SmarterHoudini and calls the "ownThisMaze" function.


import becker.robots.*;

Java:

public class SmarterHoudini extends RobotSE
{       
        private boolean inMemory = false;
        private int placeInMemory = 0;
        private Memory[] memory = new Memory[25];
        public SmarterHoudini(City theCity, int street, int avenue, int dir, int numThings )
        {
                super(theCity, street, avenue, dir, numThings);
                for (int i = 0; i < 25; i++)
                {
                        memory[i](this);
                }
        }       

        public void ownThisMaze()
        {
                proceed(scan());
        }
        public void proceed(int waysOpen)
        {
                if (waysOpen == 1)
                {
                        move1();
                        return;   
                }
                if (waysOpen == 2)
                {
                        memory[placeInMemory].chooseDirection();
                        inMemory = true;
                }
                if (waysOpen == 3)
                {
                        memory[placeInMemory].chooseDirection();
                }
                if (ways == 0)
                {
                        memory[placeInMemory].backToInitial()//possible that we need to address this because it will go back to the last place where it had to choose a direction.  Easy fix, make it choose the direction its on first.
                        while(true)
                        {
                                if (memory[placeInMemory].chooseDirection())
                                        return;
                                else
                                {
                                        memory[placeInMemory].reset();
                                        placeInMemory--;  //we are finished with the previous place in memory, no more directions left to go.
                                        memory[placeInMemory].backToInitial();
                                }
                        }       
                }
               
        }
        public int scan()
        {
                int count = 0;
                if(this.frontIsClear())
                {
                        count +=1;
                        memory.newDirection(this.getDirection());
                }
                this.turnLeft();
                if(this.frontIsClear())
                {
                        count +=1;
                        memory.newDirection(this.getDirection());
                }
                this.turnAround();     
                if(this.frontIsClear())
                {
                        count +=1;
                        memory.newDirection(this.getDirection());
                }
                this.turnLeft();
                if (count == 1)
                {
                        memory.reset();
                        placeInMemory--;
                }
                placeInMemory++;                       
        }

        private void move1()
        {
                if(this.frontIsClear)
                        this.move();
        }
}






class Memory
{
        private Directions dir;
        private Directions[] waysToGo = new Directions[2]
        private int directions_count = 0;
        private int moves = 0;
        private int current_direction = 0;
       
        public Memory(SmarterHoudini karel)
        {}
        public void newDirection(Directions clearDirection)
        {
                dir = clearDirection;
                addAWayToGo();
        }
        public void rememberMove();
        {
                moves += 1;
        }
        public boolean chooseDirection()//turns robot in a direction he has not been and starts it off down it.
        {
                if (current_direction == directions_count)
                        return false;
                else
                {
                        while(karel.getDirection() != waysToGo[current_direction])
                        {
                                karel.turnLeft();                                   
                        }
                        karel.move();
                        current_direction++;
                }
                return true;
        }
        private void addAWayToGo()
        {
                waysToGo[directions_count] = dir;
                directions_count++;
        }              
       
}
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Sat Apr 08, 2006 1:07 pm   Post subject: (No subject)

oh okay, i see what's going on.

see when you had private Memory[] memory = new Memory[25];, you already instanciated the object.

so when you cannot do memory[i](this); because you can only call the constructor when you're making the object..

Java:


public class SmarterHoudini extends RobotSE
{       
        private boolean inMemory = false;
        private int placeInMemory = 0;
        private Memory[] memory = new Memory[25];
        public SmarterHoudini(City theCity, int street, int avenue, int dir, int numThings )
        {
                super(theCity, street, avenue, dir, numThings);
                for (int i = 0; i < 25; i++)
                {
                        memory[i] = new Memory(this);
                }
        }       

        public void ownThisMaze()
        {
                proceed(scan());
        }
        public void proceed(int waysOpen)
        {
                if (waysOpen == 1)
                {
                        move1();
                        return;                       
                }
                if (waysOpen == 2)
                {
                        memory[placeInMemory].chooseDirection();
                        inMemory = true;
                }
                if (waysOpen == 3)
                {
                        memory[placeInMemory].chooseDirection();
                }
                if (ways == 0)
                {
                        memory[placeInMemory].backToInitial()//possible that we need to address this because it will go back to the last place where it had to choose a direction.  Easy fix, make it choose the direction its on first.
                        while(true)
                        {
                                if (memory[placeInMemory].chooseDirection())
                                        return;
                                else
                                {
                                        memory[placeInMemory].reset();
                                        placeInMemory--;  //we are finished with the previous place in memory, no more directions left to go.
                                        memory[placeInMemory].backToInitial();
                                }
                        }       
                }
               
        }
        public int scan()
        {
                int count = 0;
                if(this.frontIsClear())
                {
                        count +=1;
                        memory.newDirection(this.getDirection());
                }
                this.turnLeft();
                if(this.frontIsClear())
                {
                        count +=1;
                        memory.newDirection(this.getDirection());
                }
                this.turnAround();       
                if(this.frontIsClear())
                {
                        count +=1;
                        memory.newDirection(this.getDirection());
                }
                this.turnLeft();
                if (count == 1)
                {
                        memory.reset();
                        placeInMemory--;
                }
                placeInMemory++;                       
        }

        private void move1()
        {
                if(this.frontIsClear)
                        this.move();
        }
}






class Memory
{
        private Directions dir;
        private Directions[] waysToGo = new Directions[2];
        private int directions_count = 0;
        private int moves = 0;
        private int current_direction = 0;
       
        public Memory(SmarterHoudini karel)
        {}
        public void newDirection(Directions clearDirection)
        {
                dir = clearDirection;
                addAWayToGo();
        }
        public void rememberMove();
        {
                moves += 1;
        }
        public boolean chooseDirection()//turns robot in a direction he has not been and starts it off down it.
        {
                if (current_direction == directions_count)
                        return false;
                else
                {
                        while(karel.getDirection() != waysToGo[current_direction])
                        {
                                karel.turnLeft();                                       
                        }
                        karel.move();
                        current_direction++;
                }
                return true;
        }
        private void addAWayToGo()
        {
                waysToGo[directions_count] = dir;
                directions_count++;
        }               
       
}


that should work
Justin_




PostPosted: Sat Apr 08, 2006 1:11 pm   Post subject: (No subject)

thanks, the keyword new confuses me a bit. In C++ it is used to declare memory allocated on the freestore, but I do not think this is the case in java.
Justin_




PostPosted: Sat Apr 08, 2006 1:16 pm   Post subject: (No subject)

I have another question. In the Memory class, there is an array of objects called Directions. I want to make a method that wipes out the information stored in this array. How do I do this?

Just the information, not the array itself.
rizzix




PostPosted: Sat Apr 08, 2006 1:20 pm   Post subject: (No subject)

It's the same in java.

this:
Java:
Memory[] memory = new Memory[25];
simply creates an object (an array) that can hold 25 Memory objects.

You can then fill it up with Memory objects, as you wish. Just think of arrays in java as Objects. Do not think of them as arrays in c++ (which are basically pointers).
rizzix




PostPosted: Sat Apr 08, 2006 1:29 pm   Post subject: (No subject)

Justin_ wrote:
I have another question. In the Memory class, there is an array of objects called Directions. I want to make a method that wipes out the information stored in this array. How do I do this?

Just the information, not the array itself.


If your array is declared like this:
Java:
Directions[] dir = new Directions[..];


to get rid of the objects it contains simply fill the array with "null". Java's garbage collection will free those objects for you (if they are not refered to elsewhere in your code)

so it would look something like this:
Java:
java.util.Arrays.fill(dir, null);
Justin_




PostPosted: Sat Apr 08, 2006 1:31 pm   Post subject: (No subject)

Aight. Was that also aimed at answering my question on how to delete the data in the Directions array? I need to delete the values but not the array itself. Since I don't know what Directions really are, this is why I'm confused about getting rid of the data. Also, I have to compare if a direction = a direction but since getDirection() returns a direction object I can't compare them. Is it possible for me to be able to create a method to compare them without being able to see what is inside Directions?
Justin_




PostPosted: Sat Apr 08, 2006 1:32 pm   Post subject: (No subject)

Sorry I just got your latter post, please disregard all enquiries relating to the deletion of data in the directions array that took place in my last post.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sat Apr 08, 2006 1:34 pm   Post subject: (No subject)

Justin_ wrote:
Also, I have to compare if a direction = a direction but since getDirection() returns a direction object I can't compare them. Is it possible for me to be able to create a method to compare them without being able to see what is inside Directions?


You can't compare something you can't see, there's got to be some kind of public method etc, that you can access so that you can compare "Directions".

If there is, then it is possible.
wtd




PostPosted: Sat Apr 08, 2006 2:15 pm   Post subject: (No subject)

Creating an object that happens to be assigned to an element in an array is no different from creating an object that will be bound to any other variable.

How did you come up with:

code:
memory[i](this);
wtd




PostPosted: Sat Apr 08, 2006 2:46 pm   Post subject: (No subject)

While I'm at it, how about some code formatting that doesn't make wtd want to gouge his eyes out with a rusty spork? Wink That is, code which follows Sun's guidelines, and perhaps more importantly follows the general rule about no line being longers than 80 characters (or shorter).

Horizontal scrollbars are the devil's playthings.

Java:
public class SmarterHoudini extends RobotSE {       
   private boolean inMemory = false;
   private int placeInMemory = 0;
   private Memory[] memory = new Memory[25];
   
   public SmarterHoudini(City theCity, int street,
                         int avenue, int dir, int numThings ) {
      super(theCity, street, avenue, dir, numThings);
               
      for (int i = 0; i < 25; i++) {
         memory[i](this);
      }
   }       

   public void ownThisMaze() {
      proceed(scan());
   }
       
   public void proceed(int waysOpen) {
      if (waysOpen == 1) {
         move1();
         return;                       
      }

      if (waysOpen == 2) {
         memory[placeInMemory].chooseDirection();
         inMemory = true;
      }
               
      if (waysOpen == 3) {
         memory[placeInMemory].chooseDirection();
      }
               
      if (ways == 0) {
         // Possible that we need to address this because it will
         // go back to the last place where it had to choose a
         // direction.  Easy fix, make it choose the direction its
         // on first.
         memory[placeInMemory].backToInitial()
         
         while(true) {
            if (memory[placeInMemory].chooseDirection())
               return;
            else {
               memory[placeInMemory].reset();
               
               // We are finished with the previous place in memory,
               // no more directions left to go.
               placeInMemory--; 
               
               memory[placeInMemory].backToInitial();
            }
         }       
      }           
   }
       
   public int scan() {
      int count = 0;
      if (this.frontIsClear()) {
         count +=1;
         memory.newDirection(this.getDirection());
      }
               
      this.turnLeft();
               
      if (this.frontIsClear()) {
         count +=1;
         memory.newDirection(this.getDirection());
      }
               
      this.turnAround();       
               
      if (this.frontIsClear()) {
         count +=1;
         memory.newDirection(this.getDirection());
      }
               
      this.turnLeft();
               
      if (count == 1) {
         memory.reset();
         placeInMemory--;
      }
     
      placeInMemory++;                       
   }

   private void move1() {
      if (this.frontIsClear)
         this.move();
   }
}

class Memory {
   private Directions dir;
   private Directions[] waysToGo = new Directions[2];
   private int directions_count = 0;
   private int moves = 0;
   private int current_direction = 0;
       
   public Memory(SmarterHoudini karel) {
   
   }
       
   public void newDirection(Directions clearDirection) {
      dir = clearDirection;
      addAWayToGo();
   }
       
   public void rememberMove(); {
      moves += 1;
   }
   
   // Turns robot in a direction he has not been and starts
   // it off down it.
   public boolean chooseDirection(); {
      if (current_direction == directions_count)
         return false;
      else {
         while (karel.getDirection() != waysToGo[current_direction]) {
            karel.turnLeft();                                       
         }
                       
         karel.move();
         current_direction++;
      }
               
      return true;
   }
       
   private void addAWayToGo() {
      waysToGo[directions_count] = dir;
      directions_count++;
   }                   
}


Now...

Line 10 concerns me. In this case, 25 is a magic number. Where does it come from? Oh, yes... it comes from the upper bound of the "memory" array. Well, how about we replace that with "memory.length"? If you go back and decide to change it in the future, you'll only have to change it once.

Line 11 I have already referred to.

Line 16: "scan" is called. It's supposed to return an int, but looking at the method, it never returns anything. Even if it did, what is the significance of the int it is returning? The method name isn't expressive enough to tell us that, nor is any intermediate assignment of the return value of "scan" to a variable used to tell us that information.

The "proceed" method has control flow that fills me with horror. Since "waysOpen" is theoretically not modified by the methods called within the "proceed" method, there is no reason for separate conditional statements. They should be using "else", or perhaps even better... "switch".

Line 34: where does this "ways" come from? Is this simply a typo? Using a switch statement would have made this mistake avoidable.

General statement: In some places you explicitly prefix variable accesses and method calls with "this", and in others you do not. I suggest picking one style and sticking with it. Consistency counts for a lot.

Line 57: the "scan" method. You have one piece of code repeated several times. This strikes me as redundant. You should probably factor it out into a separate method with an expressive name.

Line 101: Why the empty constructor?

Lines 110 and 116: Is that an extraneous semi-colon or are you trying to do something that's beyond me?

General statement: with the amount of manipulation you do with the "directions" array, are you sure you do not want some resizeable container, like an ArrayList?
wtd




PostPosted: Sat Apr 08, 2006 3:37 pm   Post subject: (No subject)

As an additional note, I am convinced that functional programming would allow you to learn a great deal, if you are willing to leave behind (for a time) what you think you know about how to program.

I am not insulting you with that suggestion, by the way. It was a very humbling experience for me, and yet, very rewarding.

I would suggest in your case that you use Objective-Caml.
Justin_




PostPosted: Sat Apr 08, 2006 9:39 pm   Post subject: (No subject)

The code there is just me writing code without testing it. I do appreciate the advanced insights, but the crap about syntax is not needed, I mean, a smart programmer should identify that this is code that has been untested.

You strike me as a smart enough individual, so I must conclude that you harp on the syntactic errors and things that I have clearly displayed a knowledge of in previous posts, to be condescending rather than pedagogical.

I had not even tried to compile this code at the time of posting it because I wasn't submitting a book to a publisher.

Again, thank you for the advanced insight on programming architecture, just know that you make yourself look like a fool when you point out things that I have already displayed knowledge of.

I hope in the future you will concentrate less on being a debugger.

You seem to have the impression I think I am a good programmer. My opinion is that I am not nor will I ever be.
wtd




PostPosted: Sat Apr 08, 2006 9:56 pm   Post subject: (No subject)

If you understand the importance of writing code that has syntactic clarity, then you would do it. I do not harp on this to be condescending, but rather because it is absolutely crucial.

The best debugger ever created is the human brain, but for that debugger to come into play, we have to be able to parse the code. Having well-formatted code is not something we do to please others... it's something we do to make our own lives easier.
Justin_




PostPosted: Sat Apr 08, 2006 10:00 pm   Post subject: (No subject)

My code is flawlessly formatted when it is complete. When i copy and paste it into the forum it jarbles it, but this is beyond my control.

My syntax is also flawless, when it is complete.

If you like to excercise your debugger, do so, but do not point out things that I already know, it is condescending.

It's like if Dan Brown gave me his unedited novel and asked me to read it to tell him what I thought about the characters, and I started circling all the punctuation errors and spelling errors that are clearly not spelling errors but typing errors.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 35 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: