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

Thread: Beginner having trouble with OOP programming

  1. #1
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Beginner having trouble with OOP programming

    Hello everyone, I am very new here and even newer when it comes to Java. In my current course, my final assignment, I have to create a game, quick summary, you have a 5x5 grid, a sheep is in one spot, a wolf in another and a dog in another. The wolf randomly goes from spot to spot looking for the sheep and the dog does the same looking for the wolf. If the user finds the sheep that level is complete. I have this all working OK, my problem is when it starts a new level, I cannot get the sheep, wolf and dog to refresh and start again in another random spot, the grid just continues from where it left off on the previous level. There is a lot of code and 6 different classes (my instructor wants it this way to get used to OOP). There is a lot of code, so I only put some of it here, I hope it is enough.

    This is the game set up code;

     /**
     * GameSetUp class - sets up the first round of the game
     */
    public class GameSetUp {
        Wolf newWolf = new Wolf();
        Dog newDog = new Dog();
        Sheep newSheep = new Sheep();
     
        int levelNumber;
        int startWolfXPosition;
        int startWolfYPosition;
        int startSheepXPosition;
        int startSheepYPosition;
        int startDogXPosition;
        int startDogYPosition;
     
        /**
         * GameSetUp constructor - sets all the needed variables
         */
        public GameSetUp(){
            //setting the initial start locations for all animals
            startWolfXPosition = newWolf.getWolfXPosition();
            startWolfYPosition = newWolf.getWolfYPosition();
            startSheepXPosition = newSheep.getSheepXPosition();
            startSheepYPosition = newSheep.getSheepYPosition();
            startDogXPosition = newDog.getDogXPosition();
            startDogYPosition = newDog.getDogYPosition();
        } //end of GameSetUp constructor
     
        /**
         * newGameSetUp method - prints the header with current level number
         */
        public void newGameSetUp(){
            //adds 1 to the current level number
            levelNumber++;
     
            // if level number is over 1, this refreshes all the information so we can start another round
            if(levelNumber>1){
     
    //THIS IS WHERE I AM HAVING TROUBLES
     
            } // end of if statement
     
            System.out.println("*********************");
            System.out.println("*   SHEEP HERDER    *");
            System.out.println("*      LEVEL " + levelNumber + "\t\t*");
            System.out.println("*********************");
        } // end of newGameSetUp method
     
    } // end of GameSetUp class

    and this is the code that creates the grid

    public class Grid extends GameSetUp {
     
        //initializing animal starting location variables
        int wolfXPosition = startWolfXPosition;
        int wolfYPosition = startWolfYPosition;
        int sheepXPosition = startSheepXPosition;
        int sheepYPosition = startSheepYPosition;
        int dogXPosition = startDogXPosition;
        int dogYPosition = startDogYPosition;
     
        int newWolfXPosition;
        int newWolfYPosition;
        int newSheepXPosition;
        int newSheepYPosition;
        int newDogXPosition;
        int newDogYPosition;
     
        int currentLevel = levelNumber;
     
        // link this to the current level above
        int gridSizeMax = currentLevel + 6;
     
        /**
         * createStartGrid - initializes the first grid will all animal locations
         */
        public void createStartGrid()  {
            System.out.println("Current grid size is " + (gridSizeMax-1));
     
            do {
                //******************************************************************************
                //*     CHECKING TO SEE IF ANY ANIMAL LANDS ON TOP OF ANOTHER AT THE START     *
                if ((dogXPosition == sheepXPosition & dogYPosition == sheepYPosition)
                        || (wolfXPosition == sheepXPosition && wolfYPosition == sheepYPosition)
                        || (dogXPosition == wolfXPosition && dogXPosition == wolfYPosition)) {
     
                    //******************************************************************************
                    //******************************************************************************
     
                    Wolf newWolf = new Wolf();
                    wolfXPosition = newWolf.getWolfXPosition();
                    newWolfXPosition = wolfXPosition;
                    wolfYPosition = newWolf.getWolfYPosition();
                    newWolfYPosition = wolfYPosition;
                    Dog newDog = new Dog();
                    dogXPosition = newDog.getDogXPosition();
                    newDogXPosition = dogXPosition;
                    dogYPosition = newDog.getDogYPosition();
                    newDogYPosition = dogYPosition;
                    Sheep newSheep = new Sheep();
                    sheepXPosition = newSheep.getSheepXPosition();
                    newSheepXPosition = sheepXPosition;
                    sheepYPosition = newSheep.getSheepYPosition();
                    newSheepYPosition = sheepYPosition;
                } // end of if statement
                else {
                    wolfXPosition = startWolfXPosition;
                    wolfYPosition = startWolfYPosition;
                    sheepXPosition = startSheepXPosition;
                    sheepYPosition = startSheepYPosition;
                    dogXPosition = startDogXPosition;
                    dogYPosition = startDogYPosition;
                } // end of else statement
     
     
                for (int xValue = 1; xValue < gridSizeMax; xValue++) {
                    System.out.print("  " + xValue);
                } // end of for statement
     
                System.out.println("");
     
                for (int yValue = 1; yValue < gridSizeMax; yValue++) {
                    System.out.print(yValue + " ");
                    for (int xValue = 1; xValue < gridSizeMax; xValue++) {
                        if (xValue == wolfXPosition && yValue == wolfYPosition) {
                            System.out.print("W  ");
                        }  // end of if statement
                        else if (xValue == dogXPosition && yValue == dogYPosition) {
                            System.out.print("D  ");
                        } // end of if statement
                        else if (xValue == sheepXPosition && yValue == sheepYPosition) {
                            System.out.print("S  ");
                        } // end of if statement
                        else {
                            System.out.print("*  ");
                        } // end of else statement
                    } // end of inner for statement
     
                    System.out.println("");
                } // end of outer for statement
     
     
            }while (((dogXPosition == sheepXPosition & dogYPosition == sheepYPosition)
                    || (wolfXPosition == sheepXPosition && wolfYPosition == sheepYPosition)
                    || (dogXPosition == wolfXPosition && dogXPosition == wolfYPosition)));
        } // end of createStartGrid method
    } // end of Grid class

    and lastly this is the code to generate the wolfs location

    public class Wolf{
        int wolfXPosition;
        int wolfYPosition;
     
    public int getWolfXPosition() { //link the 6 with the gridMaxSize in the Grid class
            wolfXPosition = (int) (Math.random() * (6)); //change 5 to starting grid size once you import that
            if (wolfXPosition == 0) {
                wolfXPosition++;
            }
            return wolfXPosition;
     
    public int getWolfYPosition() { //link the 6 with the gridMaxSize in the Grid class
            wolfYPosition = (int) (Math.random() * (6)); //change 5 to starting grid size once you import that
            if (wolfYPosition == 0) {
                wolfYPosition++;
            }
            return wolfYPosition;

    I cannot figure out how to refresh or reload either the wolfs class or the grids class or both. (sheep and dog classes as well)

    Any advise would be greatly appreciated and possible get this handed in on time.

    Thanks,

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner having trouble with OOP programming

    how to refresh or reload either the wolfs class or the grids class or both
    Where in the code is that being attempted?
    It would seem that creating a new instance of the classes would reload the contents of that class.
    I see 2 calls to new Wolf: one in GameSetup and one in Grid.

    Why does Grid extend GameSetup? Is a Grid a kind of GameSetup?
    For example a Dog would extend Animal because a Dog is a kind of Animal.

    There should be a base Animal class that Dog, Sheep and Wolf extend.
    That class should have the x,y locations and methods to retrieve.
    There should NOT be specific methods for each animal:
    //Instead of this:
            startWolfXPosition = newWolf.getWolfXPosition();
            startWolfYPosition = newWolf.getWolfYPosition();
    // it should be this
            startWolfXPosition = newWolf.getXPosition();
            startWolfYPosition = newWolf.getYPosition();
    // etc for the other two animals
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Beginner having trouble with OOP programming

    I put in the game setup class, a method that is supposed to create a new instance of the grid,
     public void newGameSetUp(){
            //adds 1 to the current level number
            levelNumber++;
     
            // if level number is over 1, this refreshes all the information so we can start another round
            if(levelNumber>1){
     
    //THIS IS WHERE I AM HAVING TROUBLES
     
            } // end of if statement

    I tried putting in Grid newLevel = new Grid();, (Grid class is where the starting locations variables are) but this did nothing, I will take a look at your suggestion to see how that works out.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner having trouble with OOP programming

    I don't know why the Grid class extends the GameSetup class. What are their functions? Should they be separate classes?
    The Game class could have an instance of the Grid class inside it.

    Before you write any more code you need to make a design that describes what the program is supposed to do.
    From that you decide what classes are needed to do it. Just writing code without a design is not productive.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Beginner having trouble with OOP programming

    The use of the 'extends' are most likely not needed. Like I said, I am very new to Java and am just starting to learn it. As I was researching my current problem, I saw someone in a you tube video use the 'extend' and thought that may help me (I'm not even sure I understand what they are even doing at this point, as they are not something we have learned about yet). As for the separate classes, no they are not necessary, however my instructor wants us to use different classes just so we get used to OOP and connecting and retrieving information from them.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner having trouble with OOP programming

    my instructor wants us to use different classes just so we get used to OOP
    Start with making these classes:
    There should be a base Animal class that Dog, Sheep and Wolf extend.
    That class should have the x,y locations and methods to retrieve.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2021
    Location
    Ontario, Canada
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Beginner having trouble with OOP programming

    Thanks Norm, I took your suggestions and have made great stride. I now only have an Animal class, GameSetUp class and a SheepHerder class. None of them extends and the program now resets after each level. There are a few bugs still to work out bug and I have to add a StringBuilder to the program (manitory for a good grade), but I have to study up more on that as it was not 100% clear to what it does and how it works. In the mean time, I am going to mark this thread as solved. Thanks again.

Similar Threads

  1. NEW JAVA BEGINNER PROGRAMMING TUTORIAL!!!! A PROGRAMMING CHALLENGE!!
    By OrbtialStudios in forum Java Programming Tutorials
    Replies: 0
    Last Post: January 21st, 2018, 08:38 PM
  2. Beginner Java Student Having Some Trouble
    By csprung in forum Loops & Control Statements
    Replies: 1
    Last Post: April 25th, 2012, 08:14 AM
  3. Beginner having some trouble with two parts in my code
    By csprung in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2012, 04:23 AM
  4. beginner programmer trouble
    By scottey313 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2011, 01:41 PM
  5. another assignment from beginner programmer trouble
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2011, 01:13 PM

Tags for this Thread