9
Assignment 2, question 1 Assignment 2, question 1

Assignment 2, question 1. Assignment 2, Question 1 Idea #1: The Pirate Ship Author: SBT

Embed Size (px)

Citation preview

Page 1: Assignment 2, question 1. Assignment 2, Question 1 Idea #1: The Pirate Ship Author: SBT

Assignment 2, question 1Assignment 2, question 1

Page 2: Assignment 2, question 1. Assignment 2, Question 1 Idea #1: The Pirate Ship Author: SBT

Assignment 2, Question 1

Idea #1: The Pirate Ship

Author: SBT

Page 3: Assignment 2, question 1. Assignment 2, Question 1 Idea #1: The Pirate Ship Author: SBT

Assignment 2, Question 1

« Pirates often find themselves immobilized while in calm, windless seas. Each modernized pirate ship is outfitted with several paddles.Recent pirate-technological developments have proven, through the use of Navier-Strokes equations, that for a pirate ship of length n there is only one way to move the most fuildly through water.

This is done by placing the strongest rower at the back of the ship and then descending towards the weakest at the front. However, the rowing hall is not well lit and each pirate can only communicate with the pirate in front or behind him. As the captain, it is your responsability to decide which method to use to assign the rowers. »

Page 4: Assignment 2, question 1. Assignment 2, Question 1 Idea #1: The Pirate Ship Author: SBT

Assignment 2, Question 1

Approach #1

Since each pirate can only communicate with the one next to him and we want to sort them, we have to use the Bubble Sort on the LinkedList.

Approach #2

Each rower climbs to the deck of the boat where the captain will sort them, and then they proceed to filing back into the rowing hall in this order.

This method is faster because they can easily be sorted. However, it involves the use of the deck, thereby requiring more space.

Page 5: Assignment 2, question 1. Assignment 2, Question 1 Idea #1: The Pirate Ship Author: SBT

Assignment 2, Question 1

The codeimport java.util.Arrays;

public LinkedList arraySort(LinkedList L1){

int[] tmp = new int[L1.size];

Node n = head;

for(int i = 0; i<L1.size; i++){ tmp[i] = n.data; n = n.next; }

Arrays.sort(tmp);

n = head;

for(int i = 0; i<L1.size; i++){ n.data = tmp[i]; n = n.next; }

return L1;

}

Page 6: Assignment 2, question 1. Assignment 2, Question 1 Idea #1: The Pirate Ship Author: SBT

Assignment 2, Question 1

Idea #2: The Zoo

Author: dja6

Page 7: Assignment 2, question 1. Assignment 2, Question 1 Idea #1: The Pirate Ship Author: SBT

Assignment 2, Question 1

« Keeping zoo animals in cages that are nearby one another is a challenging task. You will create a simulation program, demonstrating what happens when animals of different natures’ (nature as in disposition) are placed beside each other. Animals in different circumstances have different values for your zoo (i.e. a cute animal could be worth 1).

Animals in adjacent cages interact causing a loss or gain in value for your zoo. Your simulation will model different one dimensional arrangements of animals and determine the final value of the net « worth » to the zoo in this arrangement. »

Page 8: Assignment 2, question 1. Assignment 2, Question 1 Idea #1: The Pirate Ship Author: SBT

Assignment 2, Question 1

Cute

Worth+2

Very Aggressive

Worth+2

Aggressive

Worth+1

Boring

Worth-1

Cute Very aggressive Aggressive Boring

-1 -1

+2

+3-1

+1

+2

0

0 0

0

0

0 0 0 0

Page 9: Assignment 2, question 1. Assignment 2, Question 1 Idea #1: The Pirate Ship Author: SBT

Assignment 2, Question 1

The approach

Each animal is represented as a Node, with an information on the kind (cute / very aggressive / aggressive / boring) and a worth.

public int worth(){

int total = 0; Node n = head;

while(n.next != null){ // stop one before the end

total = total + n.getWorth() + interact(n.getKind(),n.next.getKind());

n = n.next;

}

total += n.getWorth(); // the last one has a worth but no more interaction

return total; }