This is my Add method:
public void add (Message msg)
{
messageQueues.get(msg.getPriority()).add(msg);
total++;
}
This is my remove method:
public Message remove()
{
Message temp = null;
for (int i = 9; i>-1; i--)
{
if (messageQueues.get(i).size()!=0)
{
temp = messageQueues.get(i).get(0);
messageQueues.get(i).remove(0);
return temp;
}
total--;
}
return temp;
}
}
Monday, 7 February 2011
Sunday, 6 February 2011
Gridworld Part 1
Gridworld: Set 1 page 6
1. Does the bug always move to a new location? Explain. | Not always, it sometimes turns or moves in a circle. |
2. In which direction does the bug move? | Straight until something obstructs it which in that case it turns until it finds a free cell. |
3. What does the bug do if it does not move? | turns |
4. What does a bug leave behind when it moves? | Flowers |
5. What happens when the bug is at an edge of the grid? (Consider whether the bug is facing the edge as well as whether the bug is facing some other direction when answering this question.) | It turns clockwise and continues its route |
6. What happens when a bug has a rock in the location immediately in front of it? | It turns and moves to a different cell which is free |
7. Does a flower move? | No, they just change colors |
8. What behavior does a flower have? | It changes colors(into a darker color and eventually to black) |
9. Does a rock move or have any other behavior? | Nope |
10. Can more than one actor (bug, flower, rock) be in the same location in the grid at the same time? | Nope |
Gridworld: Part1: Exercises page 8
1. Test the setDirection() method with the following inputs and complete the table, giving the compass direction each input represents. |
| ||||||||||||||||||||
2. Move a bug to a different location using themoveTo() method. In which directions can you move it? How far can you move it? What happens if you try to move the bug outside the grid? | |||||||||||||||||||||
3. Change the color of a bug, a flower, and a rock. Which method did you use? | setColor method | ||||||||||||||||||||
4. Move a rock on top of a bug and then move the rock again. What happened to the bug? | The bug is gone. The rock probably squished it.. |
**********************************************************************************
MR DALY,
I DID THESE EXERCISES IN CLASS WITH AMIT ON HIS COMPUTER SINCE I DID NOT HAVE MINE AT THE TIME. HOWEVER, I AM NOT ABLE OF MAKING GRIDWORLD FUNCTION PROPERLY ON MY LAPTOP WHICH IS WHY I CANNOT COMPLETE PART 2PLEASE TAKE THIS INTO CONSIDERATION AND HOPEFULLY WE WILL FIX THE PROBLEM TOMORROW IN CLASS
THANKS,
JONATHAN
**********************************************************************************
Monday, 31 January 2011
Introductory Java Language Features:
By Monday (31) 8:00pm in your blog
How did you do on the multiple choice questions?
I did quite well, got about 80% of them correct
Did you understand your errors?
I understood most of my errors but I still have some question I would like to go over in class
What do you not understand?
I did not understand anything regarding the Hexadecimal and binary section, The book only gives a few lines of informations and explanations on this subject and I was unable to understand it whatsoever. I even tried reading the explanations to the multiple choice questions about hexadecimals but they were useless as well. Hopefully we will go over this subject in class and clarify this issue.
Can you convert between hexadecimal and decimal (and binary)?
Not at all, see answer to previous question.
Sunday, 23 January 2011
Sorting and Searching
How did you do on the multiple choice questions?
I did the multiple choice question with Sungho, which was extremley helpful and as a result I did very well on the questions and got almost all of them correct.
Did you understand your errors?
I understood all of my errors thanks to the book explanations and Sungho's explanations.
What do you not understand?
I am not completely sure about how binary search works
To what extent did you succeed in implementing the sorting and searching algorithm exercises?
A high extent, me and Amit Maor were succesful in implementing almost all of the algorithms in the array exercise that was given.
Sunday, 9 January 2011
Recursion and what not..
How did you do on the multiple choice questions?
I got 11 question right and 10 wrong
Did you understand your errors?
I understood most of them, although the explanations in the book aren't helpful enough for me to truly understand my mistake in order to avoid it in the future.
I have written down the question which i did not understand even after reading the explanations in the book and I will go over them during class.
Did you use run-time stacks to understand the progress of the recursion?
Only when I could not figure out the answer right away, I used the stacks in order to calculate the output of the recursive method.
How many codingBat recursion exercises have you done? Have the exercises helped you to understand recursion?
I have done 9 codingBat recursion exercises. The exercises have helped me a lot since i was able to get the idea of basic coding and format of simple recursion methods and qeustions.
What do you not understand?
I am not sure what I do not understand, but I know the type of questions I keep getting wrong and I am planning on going over them with you and my peers so I can truly understand.
Wednesday, 8 December 2010
Chapter 10 Test
Chapter Ten Test
1. Open a clean version of "foxes-and-rabbits-v2".
DONE
Your task is to modify the project so that gender can be taken into account as animals breed "foxes-and-rabbits-v3".
2. To do this you should create an Interface Gender..
The Gender interface has been created
public interface Gender
{
public boolean isMale();
public boolean isFemale();
}
public interface Gender { public boolean isMale(); public boolean isFemale(); } |
3. The Animal class should be modified to implement Interface Gender.
public abstract class Animal implements Gender
In order to do this you will need to add a field and to modify the constructor.
Do not change the parameter list of the constructor. Each time an Animal is constructed a random gender is assigned. The probability of male or female is equal.
THIS IS MY NEW ANIMAL CLASS:
import java.util.List;
import java.util.Random;
/**
* A class representing shared characteristics of animals.
*
* @author David J. Barnes and Michael Kolling
* @version 2008.03.30
*/
public abstract class Animal implements Gender
{
// Whether the animal is alive or not.
private boolean alive;
// The animal's field.
private Field field;
// The animal's position in the field.
private Location location;
private boolean gender;
/**
* Create a new animal at location in field.
*
* @param field The field currently occupied.
* @param location The location within the field.
*/
public Animal(Field field, Location location)
{
alive = true;
this.field = field;
setLocation(location);
Random Gender = new Random();
gender = Gender.nextBoolean();
}
/**
* Make this animal act - that is: make it do
* whatever it wants/needs to do.
* @param newAnimals A list to add newly born animals to.
*/
abstract public void act(List<Animal> newAnimals);
/**
* Check whether the animal is alive or not.
* @return true if the animal is still alive.
*/
public boolean isAlive()
{
return alive;
}
/**
* Indicate that the animal is no longer alive.
* It is removed from the field.
*/
public void setDead()
{
alive = false;
if(location != null) {
field.clear(location);
location = null;
field = null;
}
}
/**
* Return the animal's location.
* @return The animal's location.
*/
public Location getLocation()
{
return location;
}
/**
* Return the animal's field.
* @return The animal's field.
*/
public Field getField()
{
return field;
}
/**
* Place the animal at the new location in the given field.
* @param newLocation The animal's new location.
*/
public void setLocation(Location newLocation)
{
if(location != null) {
field.clear(location);
}
location = newLocation;
field.place(this, newLocation);
}
public boolean isMale()
{
return gender;
}
public boolean isFemale()
{
if(gender)
return false;
else return true;
}
}
4. Test your implementation by creating some rabbits (male and female) and foxes (male and female), then checking that isMale() and isFemale() work. Does the frequency of males and females seem to be about 50% in the population.
THIS WORKS!
The frequency is almost 50% since there are a little more males than females at the end.Show your code to the supervisor.
I was able to do this thanks to the Count class I created. Here is the code for it:
public class Count
{
static int males;
static int females;
/**
* Constructor for objects of class Count
*/
public Count()
{
males = 0;
females = 0;
}
static void count()
{
System.out.println(males + " :: " + females);
}
static void addMale()
{
males++;
}
static void addFemale()
{
females++;
}
static void removeMale()
{
males--;
}
static void removeFemale()
{
females--;
}
}
The simulation should now work exactly as before because information regarding gender is not used.
5. Working only with class Rabbit: modify the canBreed() method so that it only allows a Rabbit to breed if it is next to another Rabbit of the opposite sex.
return ( (age >= BREEDING_AGE) && foundOppositeSex);
hints....
foundOppositeSex is a local boolean variable initially set to false
foundOppositeSex is set to true if a Rabbit of the opposite sex is found in one of the adjacent locations in the field
6. Now run your simulation. How does the introduction of gender impact the changing populations? Make a quantitative comparison of V2 and V3.
7. Given what you have learned from V3, describe one way of extending the simulation in "V4". What hypothesis will you be testing with V4?
In v4, in order to create a more accurate simulation of the "circle of life" and the survival of the rabbits and foxes in nature, we should create a Disease class which randomly chooses either a fox or a rabbit and kills it due to disease. At the moment the simulation assumes that the rabbits only die because of old age or being eaten by a fox which is false, they may also die of diseases. The simulation also assumes that foxes only die due to hunger and old age, which is also not true. Just as any animal on earth they might die due to disease.
Also, we may want to include food for rabbits since at the moment, the simulation assumes that the rabbits can survive without food which is completely WRONG! Therefore, we might want to create an Herb or a Grass/ Vegetable class which is eaten by the rabbits.
Subscribe to:
Posts (Atom)