9 Replies - 3736 Views - Last Post: 25 November 2009 - 10:51 PM Rate Topic: -----

#1 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

making a method run a certain amount of times

Posted 23 November 2009 - 05:37 PM

im trying to make an array list with some numbers, i want to use the numbers in the arraylist to tell a method how many times to run. with code it will make sense.
public class DancingBug extends Bug
{
	private int steps;
	private int sideLength;

	/**
	 * Constructs a box bug that traces a square of a given side length
	 * @param length the side length
	 */
	
	public DancingBug(int length)
	{
		steps = 0;
		sideLength = length;
	}

	/**
	 * Moves to the next location of the square.
	 */
	public void act()
	{
		if(steps < sideLength && canMove())
		{
			move();
			turn() * length;
			steps++;
			
		}
		
		else
		{
			turn();
			turn();
			steps = 0;
		}
		
	}
}


/* 
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * @author Cay Horstmann
 * @author Chris Nevison
 * @author Barbara Cloud Wells
 */
import java.util.*;
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import java.awt.Color;

/**
 * This class runs a world that contains box bugs. <br />
 * This class is not tested on the AP CS A and AB exams.
 */
public class DancingBugRunner
{
	public static void main(String[] args)
	{
		ArrayList Dancing = new ArrayList(); 
			Dancing.add(new DancingBug(5));
			Dancing.add(new DancingBug(4));
			Dancing.add(new DancingBug(3));
			Dancing.add(new DancingBug(2));
			Dancing.add(new DancingBug(1));
			
		ActorWorld world = new ActorWorld();
		DancingBug alice = new DancingBug(6);
		alice.setColor(Color.BLUE);
		alice.setDirection(90);
		world.add(new Location(0,0), alice);
		world.show();
	}
}


i know the thing i did with turn() * length is incorrect, obviously. what i am trying to figure out is how to do something similar to this. if anyone has any ideas they are greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: making a method run a certain amount of times

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: making a method run a certain amount of times

Posted 23 November 2009 - 06:23 PM

That's the reason I really hated GridWorld last year..The only reason I got a 4, not a 5.

Do you mean something like this?
ArrayList<Integer> list;
//...intiate Array list
for (int i = 0; i < list.size(); i++) {
	int num = list.get(i);
	for (int i = 0; i < num; i++)
		turn();
}


Was This Post Helpful? 0
  • +
  • -

#3 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: making a method run a certain amount of times

Posted 23 November 2009 - 06:36 PM

i tried adding your code into mine, but it tells me it cannot find variable arraylist. gridworld is hell and stupid. who knew bugs running around on a grid could be made so aggravating.
public class DancingBug extends Bug
{
	private int steps;
	private int sideLength;

	/**
	 * Constructs a box bug that traces a square of a given side length
	 * @param length the side length
	 */
	
	public DancingBug(int length)
	{
		steps = 0;
		sideLength = length;
	}

	/**
	 * Moves to the next location of the square.
	 */
	public void act()
	{
		for (int i = 0; i < ArrayList.size(); i++) {
			int num = list.get(i);
			for (int i = 0; i < num; i++){
				turn(); 
			}
		}
	}
	
}

This post has been edited by gibson.nathan: 23 November 2009 - 06:37 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: making a method run a certain amount of times

Posted 23 November 2009 - 06:39 PM

Quote

i want to use the numbers in the arraylist

Arraylist as in the array Object or something else?

ArrayList is an object that acts like an array. It's in java.util.ArrayList. http://java.sun.com/.../ArrayList.html

If that's not it, could you clarify your question?
Was This Post Helpful? 0
  • +
  • -

#5 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: making a method run a certain amount of times

Posted 23 November 2009 - 07:01 PM

alright, sorry for not giving 100% detail. sometimes it is difficult to format a question to make sense.

anyways...

im writing a class that makes the bug "dance." i have to make a constructor that has an integer array as a parameter. the integer entries represent how many times the bug turns before it moves. when a dancing bug acts it should turn the number of times given by the current array entry. each move should carry out the next number in the array. after carrying out the last number in the array the bug should start over again, so that it continually repeats the same "dance" over and over.

overall the main thing is that, the dancingbugrunner should create an array and pass it as a parameter to the dancingbug constructor.

hope this helps clarify what i was saying.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: making a method run a certain amount of times

Posted 23 November 2009 - 07:08 PM

  
  private int steps;
	private int[] numofturns;

	/**
	 * Constructs a box bug that traces a square of a given side length
	 * @param length the side length
	 */
   
	public DancingBug(int[] turns) <-- need to pass an array.
	{
		steps = 0;
		numofturns = turns
	}



Then in your act(), you do this sort of thing to tell it the number of times to turn
for (int i = 0; i < numofturns.length; i++) {
	int nt = numofturns[i];
	for (int j = 0; j < nt; i++) 
		// Since this is homework, I'll let this line be a mystery, and see if you can figure it out
}


Was This Post Helpful? 0
  • +
  • -

#7 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: making a method run a certain amount of times

Posted 23 November 2009 - 08:43 PM

i have tried to incorporate the code into my program before trying to figure the rest out. im getting an error that int cannot be dereferenced on my for loop inside my act method. everything looks right, any ideas what could be causing this?

 public class DancingBug extends Bug
{
	private int steps;
	private int numOfTurns; 
	

	/**
	 * Constructs a box bug that traces a square of a given side length
	 * @param length the side length
	 */
	
	public DancingBug(int turns)
	{
		steps = 0;
		numOfTurns = turns;
	}

	/**
	 * Moves to the next location of the square.
	 */
	public void act()
	{
		for (int i = 0; i < numOfTurns.length; i++) {
			int nt = numofturns[i];
			for (int j = 0; j < nt; i++){
			}
			}
		}
	}

/* 
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * @author Cay Horstmann
 * @author Chris Nevison
 * @author Barbara Cloud Wells
 */
import java.util.*;
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import java.awt.Color;

/**
 * This class runs a world that contains box bugs. <br />
 * This class is not tested on the AP CS A and AB exams.
 */
public class DancingBugRunner
{
	public static void main(String[] args)
	{
		ArrayList turns = new ArrayList(); 
			turns.add(new DancingBug(5));
			turns.add(new DancingBug(4));
			turns.add(new DancingBug(3));
			turns.add(new DancingBug(2));
			turns.add(new DancingBug(1));
			
		
			
		ActorWorld world = new ActorWorld();
		DancingBug alice = new DancingBug(6);
		alice.setColor(Color.BLUE);
		alice.setDirection(90);
		world.add(new Location(0,0), alice);
		world.show();
	}
}

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: making a method run a certain amount of times

Posted 23 November 2009 - 08:49 PM

Gracious ME! I thought you were trying to do something else!
The act method is actually simple, because I messed up, here you go:
// This turns it
for (int i = 0; i < numOfTurns; i++)
	turn();
// Do other stuff


Was This Post Helpful? 0
  • +
  • -

#9 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: making a method run a certain amount of times

Posted 24 November 2009 - 11:17 AM

i have been working on the code trying to figure out exactly where to put the move() method. i have gotten the program to do the turning like it is supposed to, but every time i add the move() method i end up getting strange results. if anyone has any advice it is appreciated.

/* 
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * @author Cay Horstmann
 * @author Chris Nevison
 * @author Barbara Cloud Wells
 */

import info.gridworld.actor.Bug;

/**
 * A <code>BoxBug</code> traces out a square "box" of a given size. <br />
 * The implementation of this class is testable on the AP CS A and AB exams.
 */
public class DancingBug extends Bug
{
	private int steps;
	private int numOfTurns; 
	

	/**
	 * Constructs a box bug that traces a square of a given side length
	 * @param length the side length
	 */
	
	public DancingBug(int turns)
	{
		steps = 0;
		numOfTurns = turns;
	}

	/**
	 * Moves to the next location of the square.
	 */
	public void act()
	{
		for (int i = 0; i < numOfTurns; i++){
			turn();
			move();
			}
		
	}
}

Was This Post Helpful? 0
  • +
  • -

#10 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: making a method run a certain amount of times

Posted 25 November 2009 - 10:51 PM

i have my bug turning, but getting it to move and then repeat is presenting a problem. im using code like this to try to accomplish the task:
public void act()
	{
	
			for (int i = 0; i < numOfTurns; i++){
				turn();
				{
					for (int j = 0; j < numOfTurns; j++){
						move();
			}


my output is strange:


Attached Image

i know that my inner loop has a strange counter, but i dont really know what the counter should be. if anyone has advice it is appreciated. thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1