Login to Your Account

Solution Posted by

- Requests: 0
- Solutions: 262

Solution Detail

Price: $100.00

- From: Computer Science, Programming
- Posted on: Mon 03 Feb, 2014
- Request id: None
- Purchased: 0 time(s)
- Average Rating: No rating

Request Description

Submit one sketch per whole number. In other words, the a, b, c, etc., steps are just to help you build your way to a solution. The final letter represents the final deliverable.

Question 1 - Randomness and array manipulations

You are a promising junior programmer in a video game business. You are part of a team working on an online gaming system for a first-person shooter game. In the game, players can register to an arena to play together. Sometimes, they play solo; sometimes, they play in teams.

You have been asked to implement a set of functionalities in Processing (!) to be able to shuffle and make subgroups of players to play in teams.

Each player is identified by its username (a String object).

Start from this code: A1_Q1.zip. The sketch declares empty methods that you need to implement (called "stubs"). It also contains a list of usernames on which it performs a series of tests. You can see the correct expected result of these tests here : undefined Screen Shot 2014-02-02 at 5.07.41 PM.png . If your functions are implemented correctly, you should get similar results. Note that the results of the last test may slightly vary since it involves randomness.

1a - Randomize list

Implement the following method without using ArrayList (ie. using only arrays).

/**

* This method takes a list of usernames and randomizes it.

* @param userNames the list of usernames

* @return a new array containing the randomized list of usernames

*/

String[] createShuffledList(String[] userNames) {}

Important:

There should be no repetitions in the usernames (unless there was a username twice in the list sent as a parameter).

The returned list should contain all and only the items from the original list. This means that it will have the same length (among other things).

The list of usernames sent as a parameter should not be modified (ie. be aware that you are passing by reference).

1b - Grouping

Implement the following method without using ArrayList (ie. using only arrays).

/**

* This method takes a list of usernames and separates them in different groups.

* @param userNames the list of usernames

* @param nGroups the number of groups

* @return an array of groups of usernames

*/

String[][] createGroups(String[] userNames, int nGroups) {}

Example:

String[] userNames = loadStrings("usernames.txt"); // you can define your own file with usernames for testing purposes

Stringp[][] groups = createGroups(list, 3);

will return an array "groups" where groups[i][j] contains the j-th username of the i-th group. Another way to say it is that groups[i] is an array of String (ie. String[]) containing the usernames of its group.

Important:

If nGroups is smaller than 1, it should be considered equal to 1 (ie. one group with everyone in it).

if nGroups is larger than the number of usernames in the list, it should be considered equal to the number of usernames (ie. one person per "group").

If the list has length 0 or is null, the method should return null.

The returned array length is equal to the number of groups ie. groups.length == nGroups.

There should be no repetitions of usernames anywhere in the returned array and all usernames should be there.

When the number of usernames sent as a parameter cannot be perfecly divided by nGroups, you will have to return a mix of groups of different sizes. When that happens:

You need to make sure that the difference in size between two groups does not exceed 1. For example, if you call createGroups(list, 3), the result should be an array of groups where each group contains either 2 or 3 usernames.

The bigger groups should be at the beginning of the returned array; the smaller groups at the end.

Groups are formed sequentially, in the order of the elements found in userNames. So for instance, userNames[0] should appear in the first group and userNames[length-1] should always be in the last group.

1c - Randomized groups

Add an optional boolean to the last method: when true, the usernames will be randomized instead of being chosen sequentially.

/**

* This method takes a list of usernames and separates them in different groups.

* @param userNames the list of usernames

* @param nGroups the number of groups

* @param randomize if true, the groups will contain shuffled usernames; if false, the usernames will be added sequentially to the groups

* @return an array of groups of usernames

*/

String[][] createGroups(String[] userNames, int nGroups, boolean randomize) {}

Tip: If you have completed 1.1 and 1.2 you should be able to use your createSuffledList() method to shuffle the usernames before adding them to the groups.

Question 2 - Superuniverse (or: Don't be afraid of formulas)

Your doppleganger from a parallel universe (let's call it the Superniverse) has to write their first assignment as part of their Creative Computation 2 class. In that universe, the basic laws of physics are slightly changed and your doppleganger needs to implement a working simulation (in 2D).

In the Superniverse, objects interact not based on forces, but based on superforces (don't look on Wikipedia for superforces: this is all fictional as you probably have already guessed). The basic formula for a superforce is the following:

\vec{S} = \sqrt{m} \cdot \vec{j}

where\vec{S} is the superforce, m is the mass and \vec{j} is the jerk.

The jerk is the instant variation of acceleration: in other words, the jerk is to acceleration what acceleration is to velocity (and what velocity is to position).

Objects in our universe move with forces, which are proportional to acceleration. When a force is applied, the object accelerates; acceleration changes velocity; velocity changes the object's position: so the object moves.

Objects in the Superuniverse move with superforces, which are proportional to jerk. When a superforce is applied, it affects the jerk; jerk is applied to acceleration, which in turn is applied to velocity, which changes the object's position: this is how the object moves in the Superuniverse. Notice that like forces are cumulative in our universe, so are superforces: each superforce modifies the jerk in a cumulative way.

2a - Super movers

Start with the Mutual attraction example from The Nature of Code examples. Make sure you understand the code and add comments wherever they are missing.

Rename the class Mover to SuperMover and adapt it to the Superniverse setting by adding a jerk vector and replacing the applyForce(PVector) method with an applySuperForce(PVector) method (you will need to respect the superforce formula). The update() method should also be changed to account for the influence of jerk.

Make sure you respect encapsulation by never accessing directly position, velocity, acceleration and jerk from outside the class. You should only change the position, velocity, acceleration and jerk by calling the applySuperForce(PVector) and the update() methods.

2b - Superdistance

The Superniverse lies in a non-Euclidian space and therefore, the Euclidian distance cannot be used to compute supergravity:

d = \sqrt{ (u_x - v_x)^2 + (u_y - v_x)^2 + (u_z - v_z)^2 }

Instead, the Superniverse uses the Manhattan distance (also called the 1-norm distance or the Taxicab distance) to compute the superdistance d_s between two objects:

d_s = | u_x - v_x | + | u_y - v_y | + | u_z - v_z |

Notes:

The symbol | a | refers to the absolute value of a (thus the function abs() in Processing).

This is only used to compute the distance between two objects for computing supergravity (see below): the magnitude of a vector remains the same (cf. the PVector.mag() and the PVector.normalize() methods).

Create a method in your sketch that returns the Manhattan distance between two vectors:

float superDistance(PVector u, PVector v) { ... }

2c - Supergravity

The Superniverse has two basic forces that are alway present: supergravity and superfriction. You will need to implement them.

The first superuniversal superforce is called supergravity.

You will recall that the formula in our universe to compute the force of gravity (p. 88) is:

\vec{F}_g = G m_1 m_2 / d^2 * \hat{r}

In the Superuniverse, the equivalent of the law of gravity is called supergravity and has the following formula:

\vec{S}_g = G_s (m_1 + m_2) / d_s * \hat{r}

where G_s is the superuniversal gravitational constant, m_1 and m_2 is the mass of the two objects, d_s is the superdistance (see subquestion 2.2) between the two objects and $\hat{r}$ is the unit vector going from object 1 to object 2.

Change the method attract(SuperMover m) for a method superAttract(SuperMover m) that implements the supergravity. The value for G_s can be adjusted at your own choice.

2d - Superfriction

The second superuniversal superforce is called superfriction. Unlike friction (which is related to velocity), superfriction is related to both velocity and acceleration. The formula of superfriction is:

\vec{S}_f = - \mu_s \parallel\vec{v}\parallel \parallel\vec{a}\parallel \hat{v}

where \mu_s is the superuniversal superfriction constant, \vec{v} is the velocity vector and \vec{a} is the acceleration vector. Remember that \parallel \vec{x} \parallel stands for the magnitude of vector \vec{x} and that \hat{x} is the normalized version of vector \vec{x}.

Add a method in SuperMover that returns the superfriction applicable on the SuperMover instance and use the function to apply friction on all mover instances. The value for \mu_s can be adjusted at your own choice.

This is an example of what the result looks like (quite super-crazy, heh?):

Bonus *

Replace the Manhattan distance in your calculations by the Minkovsky distance with a parameter that controls theorder of the distance (see below). The Minkovsky distance is defined as:

d(p) = \{ | u_x - v_x |^p + | u_y - v_y |^p + | u_z - v_z |^p \}^{1/p}

Where p is the order of the distance. Notice that if p = 1, the Minkovsky distance is equal to the Manhattan distance, whereas if p = 2 it is equal to the Euclidian distance.

Question 3 - Particle systems, inheritance and polymorphism

Back to our own (maybe not super but still quite amazing) universe.

Start from this code:A1_Q3.zip. Make sure you understand the code and add comments wherever they are missing.

3a - Different kinds of particles

Implement three different subclasses of class Particle. For each subclass, override the display() method to make the 3 kinds of particles look and behave differently. For at least one subclass, also override the update() method.

To test your subclasses, change the method createParticle() in ParticleSystem so that the system creates each time a different kind of particle from one of your 3 subclasses (could either be random or sequential).

3b - Wind particle system

Implement a subclass of ParticleSystem. Override the applyForces() method.

The applyForces() method is where you can have a particle system that applies one or many forces to its particles.

Your subclass should apply a wind force to its particles in the applyForces() method. A wind force is represented as a simple PVector that gets applied to all the particles of the system. At each call of applyForces(), the wind should also change its heading (ie. its angle) according to some random pattern. You have to change the heading using trigonometry. Make the wind as believable as possible. Use the randomGaussian() and/or the noise() (Perlin noise) function at least once in your code for controlling the wind.

3c - More particle systems

Now, implement two more subclasses of ParticleSystems (ie. there will be three subclasses of ParticleSystems in total).

For these two subclasses, override the createParticle() method. The createParticle() method will determine the kind of particles generated by the system. By overriding it, the kind of particles that each subclass of ParticleSystem generates will be different. For example, one subclass could generate only red, round-shaped particles will low mass, while another might generate a combination of round-shaped and star-shaped particles with random mass.

Once that is done, override the applyForces() method as well. Your two subclasses can apply any force you want on their particles. They can apply forces on all the particles or a subset of them. The only rule is that you should use at least two different forces that we have seen in The Nature of Code: gravitation, repulsion, friction, drag and spring.

Solution Description

Java an