Class Notes (808,131)
Canada (493,086)
COMP 202 (186)

Assignment 4.pdf

6 Pages
Unlock Document

McGill University
Computer Science (Sci)
COMP 202
Mathieu Petitpas

ASSIGNMENT 4 Class Relationships, Overloading, Encapsulation, Static, Arrays COMP-202A, Fall 2008, All Sections Due: Wednesday, November 12, 2008 (23:55) You MUST do this assignment individually and, unless otherwise speci▯ed, you MUST follow all the general instructions and regulations for assignments. Graders have the discretion to deduct marks for deviations from the general instructions and regulations. Part 1, Question 1: 0 points Part 1, Question 2: 0 points Part 2, Question 1 class 1:50 points Part 2, Question 1 class 2:20 points Part 2, Question 1 class 3:30 points 100 points total Part 1 (0 points) Warm Up Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do the second part of the assignment, which will be graded. If you have di▯culties with the questions of Part 1, then we suggest that consult the TAs during their o▯ce hours; they can help you and work with you through the warm-up questions. Warm-up Question 1 (0 points) Search for the second best. You want to get a part time job as an assistant of Professor Secondary who is obsessed with what is second best in this world. Your future employer usually wants to know not only what is the biggest and best but also what is just next to it. He knows that you are capable of ▯nding the largest number among a bunch numbers. But are you able to ▯nd the second largest? To prove your abilities as Professor Secondary’s assistant, you made a bet with the professor that you can and you’re going to show it. ▯ Write a program that ▯rst creates an array of 10 integer values picked randomly from the range 0{99. It should display the numbers on a single line separated by a single space. ▯ Write a loop that searches the array for the largest number. Store the array index of this variable in largestNumberIdx. After the search is over display the number and the index of the array at which 1 the number was found. ▯ Declare another variable called secondLargestNumberIdx. Create another loop that searches the array for the largest number with one di▯erence: you should ignore the array element with index largestNumberIdx. Store the array index of the second largest number in secondLargestNumberIdx. After the search is over display that number and the index of the array at which the number was found. ▯ If two integers stored at indices a and b, where a < b, have the same, largest value then largestNumberIdx should end up being set to a and secondLargestNumberIdx to b. ▯ Make sure your program ▯nds the right values in arrays of other sizes that exercise speci▯c or special cases of the search you perform, for example in arrays of f10, 10g, f9, 7, 8g, f10, 7, 10, 9, 11g, etc. Note: there are more e▯cient ways of accomplishing this task than the simplistic approach described above. Feel free to use a di▯erent (and possibly better) method. Warm-up Question 2 (0 points) Finding soulmates on Decunia You ▯nd yourself visiting planet Decunia. It is inhabited by a race of intelligent creatures that have not two sexes, like us, but instead have 10 of them. What makes the matter even more complicated is the sexes are numbered 1{10 and a there is a potential for a pair creation only if the sum of their numbers is 11. That is a 1 is a potential match with a 10, a 2 is a potential match with a 9 and so on. You decide to help the friendly creatures of Decunia and organized a match-making event. You asked the Decunians who showed up to your event to sit at tables numbered from 0 to (the number of participants - 1). Each Decunian at each table can be asked about his number. You were also warned that you have to minimize the number of times you ask each Decunian for his number because Decunians above all despise redundancy and waste of time. Write a program that will look for possible matches among Decunians. See the example below on how you can easily create and initialize an array of integer values. Every time your program is trying to match two Decunians, it must display a message about the table number (array index) of each Decunian and their numbers. For example Trying to match table 2 (number 3) with table 3 (number 6) followed by either potential match or no match depending on whether the sum of the two numbers was 11 or not. Note that it makes no sense to try to mach a Decunian with itself. Count the number of matches found and display it at the end of the program. For example for 3 Decunians that carry numbers 1, 9 and 10. You will try the following matches: 1 and 9 -- Trying to match table 0 (number 1) and table 1 (number 9) -- no match. 1 and 10 -- Trying to match table 0 (number 1) and table 2 (number 10) -- potential match. 9 and 10 -- Trying to match table 1 (number 9) and table 2 (number 10) -- no match. Found 1 potential match(es). For example for 4 Decunians that carry numbers 4, 7, 8, 4. You will try the following matches (in a shorter notation of events): [0]->4 and [1]->7 -- potential match. [0]->4 and [2]->8 -- no match. [0]->4 and [3]->4 -- no match. [1]->7 and [2]->8 -- no match. [1]->7 and [3]->4 -- potential match. [2]->8 and [3]->4 -- no match. Found 2 potential match(es). Try your program on the following arrays. int[] array = { 1, 9, 10}; int[] array = { 4, 7, 8, 4}; int[] array = { 1, 3, 8, 5, 4, 2, 7, 10, 6, 9 }; Page 2 Part 2 (100 points) Tournament of Humans, Dwarfs and Elves In an alternate universe the evil Lord Of The Darkness (LOTD) must be defeated as it has been preparing a vast army against everything that exists. The three races peacefully inhabiting the Earth (Humans, Dwarves and Elves) decided to use the latest breakthrough in biotechnology and genetics to create an army of super-warriors. They select the best warrior from each race for a tournament. In this tournament, they will ▯ght against each other with di▯erent weapons. At the end of each round of the tournament, the two best warriors will be selected and their genetic makeup will be used to create a new warrior, which (due do the application of accelerated growth procedures) will immediately join the tournament. You need to make this tournament, as well as the creation of new and better warriors, happen. The fate of the World is in your hands! You need to create 3 classes de▯ning a Warrior, a Weapon and the Tournament. All ▯elds must be declared private and non-static unless stated otherwise. All methods are non-static, unless stated otherwise. All methods of Warrior and Weapon classes mentioned in the description below compose the interface of these classes and therefore MUST be public, unless stated otherwise. All methods of Warrior and Weapon classes not mentioned in this description MUST be declared private. All methods of class Tournament, with the exception of main(), MUST be private. The methods must accept the listed parameters in the order in which they are listed below. In many cases the assignment suggests speci▯c names for variables. Because of the substantial number of variables used, it is strongly suggested to choose clear, descriptive, non-ambiguous names. In some cases the names of variables can be inferred from the name of accessor methods or from code snippets. The names of private variables are not requirements but useful suggestions. The names of private methods given in the description below, especially in class Tournament, must be as described. It is suggested that you start your implementation with the simplest class, Weapon (testing it using the TestWeapon class), then implement the class Warrior (testing it using the TestWarrior class), and ▯nally implement the class Tournament. Question 1, class 1: Warrior class (50 points) In this question, you will implement the Warrior class. 1. The Warrior class has three static ▯elds, all of them private. One is a formatter class (as seen during the lectures) that is used to format all oating point variables output so as to only display 2 decimal digits. This ▯eld should be initialized at the time of declaration (in the declaration statement). The second ▯eld is an integer value with the next available ID for a Warrior. This ▯eld is initially set to value 1. The third ▯eld is a constant value (final) which is the Warrior’s memory size, and is initialized to 100. This is the number used to decide on the size of the arrays used to store opponents fought, weapons used, and results of the ▯ghts (all described later below). 2. Each Warrior carries a name (String) that is set once at its birth (creation) and an ID (int) that is unique to each Warrior. The ID of the ▯rst Warrior to be created is 1, the ID of the second Warrior to be created is 2, and so on. 3. Each Warrior has several oating point attributes given at its birth, representing maximal strength, maximal intelligence, and maximal con▯dence. It also has attributes representing current strength, current intelligence, and current con▯dence; these can never exceed their respective maximum values. The maximum values are assigned once in a constructor, at object creation time, and do not change. 4. Three integer ▯elds store the statistical data on the total number of ▯ghts fought, how many of these were won, and how many ▯ghts were won lately (that is, in the current tournament). This last number has an associated reset() method used to set it to 0 (described later along with accessor methods). 5. Three arrays are de▯ned to store opponents fought (Warrior array), weapons used (Weapon array) and the results of the ▯ghts (boolean array). All arrays need to be created with the new operator and need to be of the Warrior’s memory size, de▯ned earlier as an integer constant. It is suggested to allocate the arrays is at variable declaration time (in the declaration statement) but it can also be done in a constructor (but it must then be done in both constructors). Page 3 6. The ▯rst constructor takes 7 parameters (as can be seen in the sample code of Tournament class). The ▯rst parameter is the warrior’s name, the next 3 are maximum values for strength, intelligence, and con▯dence, followed by 3 values de▯ning the initial (current) strength, intelligence, and con▯dence. The constructor must use these values to initialize respective ▯elds of the object. The constructor is also required to set the ID ▯eld and increment the static ID counter. 7. The second constructor is used to \cross" two existing warriors to obtain a new, superior one. It takes two paramete
More Less

Related notes for COMP 202

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.