• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

BASIC java programming help

I am in a Software Architecture course and part of the curriculum involved learning some very basic Java programming. Seeing as the Java piece is really ancillary to the course, they don’t spend much time explaining the language and therefore I’m stuck, being this is my first exposure to programming of any type.

We are learning out of a book called “Introduction to Programming with Greenfoot” and creating a little game. The code pasted below is what I have so far, and it seems to work. My problem is with where to put { and } (I don’t understand their purpose) with respect to our next steps having to do with new methods to make the code easier to read. I managed to create a method called “lookForWorm”, but also need to create on called “randomTurn” (the method has no parameters and returns nothing) to reflects the code I have that makes the crab turn randomly and “turnAtEdge” (also no parameters and returns nothing).

The final result should look like this, and be able to be compiled, but I can’t get the syntax right for the life of me…

Public void act ()
{
     turnAtEdge();
     randomTurn();
     move();
     lookForWorm();
}

The code I have so far is below. Could someone reply with a copy of the code that incorporates these methods so I can see how the heck it is supposed to look?

Thank you!


import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * This class defines a crab. Crabs live on the beach. They like sand worms
 * (very yummy, especially the green ones).
 *
 * Version: 2
 *
 * In this version, the crab walks around the beach more or less randomly.
 */

public class Crab extends Animal
{
    /**
     * Act - do whatever the crab wants to do. This method is called whenever
     *  the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
 
   
    {
        if ( atWorldEdge() )
        {
            turn(17);
        }

        if ( Greenfoot.getRandomNumber(100) < 10 )
        {
            turn( Greenfoot.getRandomNumber(20)-55 );
        }

        move();
        lookForWorm();

    }
    /**
     * Check to see if crab hit a worm. If so, eat it. If not, don't.
     */
    public void lookForWorm()
    {
       
        if (canSee(Worm.class) )
        {
            eat(Worm.class);}
           
           
    }
}
0
Dopher
Asked:
Dopher
  • 3
  • 3
  • 3
1 Solution
 
for_yanCommented:

You can look here at snake game - not a crab, but at least something.
http://zetcode.com/tutorials/javagamestutorial/snake/

0
 
objectsCommented:
you need something like this
need to fill in the methods with the required actions
public class Crab extends Animal {
	/**
	 * Act - do whatever the crab wants to do. This method is called whenever
	 * the 'Act' or 'Run' button gets pressed in the environment.
	 */
	public void act() {
		turnAtEdge();
		randomTurn();
		move();
		lookForWorm();
	}

	private void move() {

	}

	private void randomTurn() {

	}

	private void turnAtEdge() {

	}

	/**
	 * Check to see if crab hit a worm. If so, eat it. If not, don't.
	 */
	public void lookForWorm() {

		if (canSee(Worm.class)) {
			eat(Worm.class);
		}

	}

	private void eat(Class class1) {

	}

	private boolean canSee(Class class1) {
		return false;
	}
}

Open in new window

0
 
DopherAuthor Commented:
Thanks for your comments. Any chance you could add the proper code to the code I posted, so I can see exactly what needs to be done?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
thats what I've posted above. What specifically is it you are unclear about?
0
 
for_yanCommented:

You may start thinking somehow in this direction -
say you have randomly travelling Crab and a random herd
of Worms, whom Crab may encounter and then eat.


This is a small beginning sketch for you - you may look at it and
continue thinking:

public class Crab {
    int x;
    int y;
    static int numWorms = 10;
    static int maxRight = 10;
    static int maxDown = 10;
    static int maxLeft = 0;
    static int maxUp = 0;

    int direction;

    Worm [] worms;


    public Crab(){
        x = (int)Math.random()*maxRight;
        y = (int)Math.random()*maxDown;
        direction = (int)Math.random()*5;
         worms = new Worm[numWorms];
        for(int j=0; j<numWorms; j++){
            worms[j] = new Worm();
        }
    }

    public void act() {
        turnAtEdge();
        randomTurn();
        move();
        lookForWorm();
    }

    private void move() {
        if(direction == 0){
            if(x+1 == maxRight) turnAtEdge();
            else
            x++; return;}
        if(direction == 1){if (x-1 == 0 )turnAtEdge();
            else
            x--; return;}
        if(direction == 2){... }
        if(direction == 3){... }
        if(direction == 4)randomTurn();

    }

    private void randomTurn() {
direction= 5-(int)Math.random()*5;
move();
move();
    }

    private void turnAtEdge() {

    }
    public void lookForWorm() {
    for(int j=0; j<worms.length; j++){
//checking if any worm is in current Crab position
}

            if (canSee(Worm.class)) {
                eat(Worm.class);
            }

        }

        private void eat(Class class1) {

        }

        private boolean canSee(Class class1) {
            return false;
        }

    

}

class Worm{
    double x;
    double y;
   Worm(){
         x = (int)Math.random()*Crab.maxRight;
        y = (int)Math.random()*Crab.maxDown;
   }

}

Open in new window

0
 
DopherAuthor Commented:
Objects, thanks for your help and patience.  I tried to add
one more method for "atWorldEdge();". When I compile I get " ';'
expected" regarding "public void act()"near the top. Below is the
code. Would you mind having one more look?

import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)


public class Crab extends Animal {
/**
* Act - do whatever the crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/

public void act()
/**
* if at world edge, turn
*/
public void atWorldEdge(){
if (atWorldEdge()) {
turn(17);
}
}


/**
* Random Turn.
*/
public void randomTurn() {
if (Greenfoot.getRandomNumber(100) < 10) {
turn(Greenfoot.getRandomNumber(20) - 55);


move();
lookForWorm();
randomTurn();
atWorldEdge();


}
}


/**
* Check to see if crab hit a worm. If so, eat it. If not, don't.
*/
public void lookForWorm() {
if (canSee(Worm.class)) {
eat(Worm.class);
}
}
}
0
 
for_yanCommented:
The way it was:

      public void act() {
            turnAtEdge();
            randomTurn();
            move();
            lookForWorm();
      }

after "public void act()" comes the brace and then definition
of the act() method (which happens to contain calls to other methods);
that made sense

The way it is in your code:

public void act()
/**
* if at world edge, turn
*/
public void atWorldEdge(){
if (atWorldEdge()) {
turn(17);
}
}

after  
public void act()

comes immediately definition
of another method

public void atWorldEdge(){...

and there is not content of method act()

So compiler is confused.
You should modify this part.










0
 
objectsCommented:
you have put the methd inside the other method
should be more like this:


public void act() {
}

/**
* if at world edge, turn
*/
public void atWorldEdge(){
   if (atWorldEdge()) {
   turn(17);
}

0
 
DopherAuthor Commented:
Sorry for the delay and thank you all for your assistance!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now