// Anthony E. Capiral © 2012 // CST 112 Intro to Programming // Project 7 - Array of Objects // "Vital Statistics" // Array of person objects, vital statistics, random year, height and weight // body mass index //// instructions on how to load from a file was not discussed...!! \\\\ // using 10 object due to lack of time // key press 'f' fills array once with random values // how many people objects in the array int howManyPeople = 10; // declare an array called people, full of objects of class Person Person people[] = new Person[howManyPeople]; // informational text String title = "CST 112 Project 7"; String projectName = "- Vital Statistics -"; String projectNameTwo = "Array of Objects"; String author = "Anthony E. Capiral"; String date = "© 12.19.2012"; String keys = "- [ F ]ill the array (intialize) - Sort by: [ A ]ge, [ H ]eight, [ W ]eight, [ B ]ody Mass Index - [ Q ]uit -"; void setup() { smooth(); size(700, 400); createBg(); setupPeople(); } // stage background void createBg() { fill(0, 0, 150); rectMode(CORNERS); rect(0, 0, width, height); infoText(); } // informational text void infoText() { fill(255); text(title, 300, 20); fill(255, 0, 255); text(projectName, 305, 30); fill(255); text(projectNameTwo, 307, 40); text(author, 30, 350); text(date, 40, 360); fill(255, 0, 255); text(keys, 100, 300); } void setupPeople() { // temp random values // d: date born, h: height, w: weight, b: body mass index int d = 0; float h = 0.0; float w = 0.0; float b = 0.0; people[0] = new Person(1, "Andy", 'M', d, h, w, b); people[1] = new Person(2, "Beth", 'F', d, h, w, b); people[2] = new Person(3, "Chris", 'M', d, h, w, b); people[3] = new Person(4, "Debbi", 'F', d, h, w, b); people[4] = new Person(5, "Edgar", 'M', d, h, w, b); people[5] = new Person(6, "Frank", 'M', d, h, w, b); people[6] = new Person(7, "Gabby", 'F', d, h, w, b); people[7] = new Person(8, "Helen", 'F', d, h, w, b); people[8] = new Person(9, "Irene", 'F', d, h, w, b); people[9] = new Person(10, "Jason", 'M', d, h, w, b); // testing //println(people[0].idNum); //println(people[0].name); //println(people[0].gender); //println(people[0].born); //println(people[0].personHeight); //println(people[0].personWeight); //println(people[0].bodyMassIndex); } void draw() { } // Person class definition public class Person { // local Person variables int idNum; // unique number to indentify person String name; // name of person char gender; // male or female int born; // date of birth float personHeight; // height of person (in) float personWeight; // weight of person (lb) // bmi: Body Mass Index /* Formula: weight (lb) / [height (in)]2 x 703 Calculate BMI by dividing weight in pounds (lbs) by height in inches (in) squared and multiplying by a conversion factor of 703. */ float bodyMassIndex; // the Body Mass Index of the person // Person constructor Person(int id, String n, char g, int dob, float h, float w, float bmi) { idNum = id; name = n; gender = g; // randomize date of birth dob = int(random(1920, 2000)); born = dob; // create random values for person's height and weight // depending on gender if (g == 'M') { // if male h = random(60, 76); w = random(120, 200); } else { // female h = random(56, 72); w = random(100, 180); } personHeight = h; personWeight = w; // calculate body mass index bmi = ( w / (h *2) ) * 703; bodyMassIndex = bmi; } } // end Person definition // display(fill) the array of people, show all values void fillPeople(Person thePeople[], int number) { createBg(); // top header fill(175); text("ID# - Name - Sex - Year - Height - Weight - BMI -", 15, 55); // initial starting coordinates to show the array int pPosX = 25; int pPosY = 75; int pSpaceY = 20; // space between on y axis fill(255); // white text for ID numbers // loop to show array for (int i = 0; i < number; i++) { // id number fill(200, 200, 0); text(thePeople[i].idNum, pPosX, pPosY); // name fill(255); text(thePeople[i].name, pPosX + 25, pPosY); // gender if (thePeople[i].gender == 'M') { fill(0, 150, 255); } else { fill(255, 120, 120); } text(thePeople[i].gender, pPosX + 75, pPosY); // dob fill(255); text(thePeople[i].born, pPosX + 100, pPosY); // height, weight text(thePeople[i].personHeight, pPosX + 140, pPosY); text(thePeople[i].personWeight, pPosX + 190, pPosY); // body mass index fill(200, 200, 0); text(thePeople[i].bodyMassIndex, pPosX + 245, pPosY); // pPosY = pPosY + pSpaceY; } } // display the banners void showBMIBars(Person thePeople[], int number) { noStroke(); fill(255); // intial starting coords for the bars) // drawn horizontally int bPosX = 330; int bPosY = 62; int bSpace = 20; for (int i = 0; i < number; i++) { /* // draw banners with varying colors if (randInts[i] <= 25) { fill(25, 0, 100); } if ( (randInts[i] > 25) && (randInts[i] <= 50) ) { fill(50, 0, 150); } if ( (randInts[i] > 50) && (randInts[i] <= 75) ) { fill(75, 0, 200); } if (randInts[i] > 75) { fill(100, 0, 255); } */ // width of the BMI bar float bWidth = (thePeople[i].bodyMassIndex * 0.25); rect(bPosX, bPosY, (bPosX + bWidth), bPosY + 15); // increment space between bars bPosY = bPosY + bSpace; } } // end showBMIBars() // swap the people(indexes) in the array void swapPeople(Person thePeople[], int i, int j) { // swap each element in thePeople array int tmp, tmpFour; // ID number, year born String tmpTwo; // name char tmpThree; // gender float tmpFive, tmpSix, tmpSeven; // height, weight, bmi tmp = thePeople[i].idNum; thePeople[i].idNum = thePeople[j].idNum; thePeople[j].idNum = tmp; tmpTwo = thePeople[i].name; thePeople[i].name = thePeople[j].name; thePeople[j].name = tmpTwo; tmpThree = thePeople[i].gender; thePeople[i].gender = thePeople[j].gender; thePeople[j].gender = tmpThree; tmpFour = thePeople[i].born; thePeople[i].born = thePeople[j].born; thePeople[j].born = tmpFour; tmpFive = thePeople[i].personHeight; thePeople[i].personHeight = thePeople[j].personHeight; thePeople[j].personHeight = tmpFive; tmpSix = thePeople[i].personWeight; thePeople[i].personWeight = thePeople[j].personWeight; thePeople[j].personWeight = tmpSix; tmpSeven = thePeople[i].bodyMassIndex; thePeople[i].bodyMassIndex = thePeople[j].bodyMassIndex; thePeople[j].bodyMassIndex = tmpSeven; } // sort the array from lowest to highest void sortByAge(Person thePeople[], int number) { int n = number; while(n > 1) { int large = largestAge(thePeople, n); // get the index of the largest integer in the array swapPeople(thePeople, large, n-1); // swap the indexes, starting from the LAST, working backward // and move largest integer to end of the array n--; // decrement n, move to next element (shrink array) } } // find and return the largest Age int largestAge(Person thePeople[], int number) { int i = 0; // temp variable relating to the index of the array for (int j = 1; j < number; j++) { // start at j = 1, since first value dont need to compare with itself if (thePeople[j].born > thePeople[i].born) { i = j; // if integer at index j is larger than integer at index i, i becomes the index with the largest integer } } return i; // this index of the array corresponds to the largest } // sort by height void sortByHeight(Person thePeople[], int number) { int n = number; while(n > 1) { int large = largestHeight(thePeople, n); swapPeople(thePeople, large, n-1); n--; } } // find and return the largest Height int largestHeight(Person thePeople[], int number) { int i = 0; for (int j = 1; j < number; j++) { if (thePeople[j].personHeight > thePeople[i].personHeight) { i = j; } } return i; // } // sort by weight void sortByWeight(Person thePeople[], int number) { int n = number; while(n > 1) { int large = largestWeight(thePeople, n); swapPeople(thePeople, large, n-1); n--; } } // find and return the largest weight int largestWeight(Person thePeople[], int number) { int i = 0; for (int j = 1; j < number; j++) { if (thePeople[j].personWeight > thePeople[i].personWeight) { i = j; } } return i; // } // sort by BMI void sortByBMI(Person thePeople[], int number) { int n = number; while(n > 1) { int large = largestBMI(thePeople, n); swapPeople(thePeople, large, n-1); n--; } } // find and return the largest BMI int largestBMI(Person thePeople[], int number) { int i = 0; for (int j = 1; j < number; j++) { if (thePeople[j].bodyMassIndex > thePeople[i].bodyMassIndex) { i = j; } } return i; // } void keyPressed() { // show array of people with bmi bars if (key == 'f') { createBg(); fillPeople(people, howManyPeople); showBMIBars(people, howManyPeople); } // sort array by person's age if (key == 'a') { createBg(); sortByAge(people, howManyPeople); fillPeople(people, howManyPeople); showBMIBars(people, howManyPeople); } // sort by height if (key == 'h') { createBg(); sortByHeight(people, howManyPeople); fillPeople(people, howManyPeople); showBMIBars(people, howManyPeople); } // sort by weight if (key == 'w') { createBg(); sortByWeight(people, howManyPeople); fillPeople(people, howManyPeople); showBMIBars(people, howManyPeople); } // sort by BMI if (key == 'b') { createBg(); sortByBMI(people, howManyPeople); fillPeople(people, howManyPeople); showBMIBars(people, howManyPeople); } // quit if (key == 'q') { exit(); } }