| |||||||||||
| |||||||||||
Calculating BMI of various stars
this example from a Well House Consultants training course
Source code: Person.java Module: J715
import java.io.*;
import java.util.*; import java.text.*; /* An excercise from our Java Programming course: * Take the data in the file http://www.wellho.net/data/stars.txt * Read that data and use a factory to create person objects * calculate their bmis * produce a sorted list of stars from skinniest up to thickest See http://melksh.am/j715 (example Station.java) for a template to work from Sample data: Scarlett Johansson 5 feet 3.5 inches / 162 cms 110.23-114.64 lbs Sample answer: */ public class Person implements Comparable { public String forename, surname; private float h,w; private int coverage; boolean cached; float bmi; public static int manufacturerNumber = 0; public static Person factory(String raw) { String [] pieces = raw.split(" "); Person added = null; manufacturerNumber++; if (raw.startsWith("#")) return added; if (pieces.length < 11) return added; try { String [] weights = pieces[9].split("-"); float weight = Float.parseFloat(weights[0]) * 0.453592f; added = new Person(pieces[0],pieces[1], Float.parseFloat(pieces[7]),weight); } catch (Exception e) { System.err.println("FAILED on line " + manufacturerNumber +":\n" +raw); } return added; } public Person(String forename, String surname, float h, float w) { this.forename = forename; this.surname = surname; this.h = h / 100.0f ; this.w = w; coverage = 0; cached = false; } public float getBMI() { coverage++; // Look at how many savings made if (! cached) { bmi = w/h/h; cached = true; } return bmi; } public String printBMI() { float bmi = this.getBMI(); DecimalFormat value = new DecimalFormat("#0.00"); String pretty = value.format(bmi); return pretty; } public String toString() { String say = forename + " " + surname + " " + coverage; return say; } public int compareTo(Object that) { return ((this.getBMI() > ((Person)that).getBMI()) ? 1 : -1); } // ---------------------------- public static void main(String [] args) throws FileNotFoundException, IOException { Vector<Person> movieStar = new Vector<Person>(); BufferedReader source = new BufferedReader(new FileReader(args[0])); String lyne; while ((lyne = source.readLine()) != null) { Person current = Person.factory(lyne); if (current == null) continue; movieStar.add(current); } System.out.println("Count of little people " + movieStar.size()); Collections.sort(movieStar); System.out.println("Attending the Oscars"); for (int k=0; k<movieStar.size(); k++) { System.out.print(movieStar.get(k).printBMI()); System.out.println(" " + movieStar.get(k)); } } } /* ------------------------- Sample Output ---------------------------- WomanWithCat:dvla_20150202 grahamellis$ java Person stars.txt FAILED on line 35: Catherine Zeta Jones 5 feet 8 inches / 172.72 cms 123.46-134.48 lbs FAILED on line 55: Fergie / Stacy Ferguson 5 feet 2.5 inches / 158.75 cms 103.62-114.64 lbs FAILED on line 74: Jennifer Love Hewitt 5 feet 2.5 inches / 158.75 cms 114.64-125.66 lbs FAILED on line 113: Pam / Pamela Anderson 5 feet 7 inches / 170.18 cms 99.21-110.23 lbs FAILED on line 118: Rachael Leigh Cook 5 feet 2 inches / 157.48 cms 97-108.03 lbs FAILED on line 132: Sarah Michelle Gellar 5 feet 3 inches / 160.02 cms 97-108.03 lbs Count of little people 119 Attending the Oscars 14.87 Amber Valletta 9 14.94 Calista Flockhart 10 15.15 Patrick Swayze 13 15.28 Carmen Kass 11 15.30 Shannon Elizabeth 13 15.37 Taylor Swift 10 15.50 Naomi Campbell 14 15.54 Keira Knightley 11 15.76 Penelope Cruz 12 15.82 Adriana Lima 15 15.82 Christy Turlington 13 15.99 Gisele Bundchen 17 16.28 Gwyneth Paltrow 9 16.33 Jessica Biel 12 16.37 Kate Moss 15 16.37 Teri Hatcher 12 16.43 Rebecca Gayheart 13 16.45 Cindy Crawford 15 16.53 Nicole Kidman 11 16.72 Kirsten Dunst 14 16.72 Victoria Beckham 13 16.74 Elle MacPherson 16 16.76 Jennifer Garner 12 16.76 Paris Hilton 13 16.76 Tyra Banks 11 16.93 Heidi Klum 12 16.93 Katie Holmes 14 16.98 Jodie Kidd 12 17.08 Claire Danes 15 17.18 Cameron Diaz 12 17.18 Cate Blanchett 10 17.18 Rose McGowan 19 17.22 Rebecca Romijn-Stamos 10 17.24 Lindsay Lohan 17 17.25 Faith Hill 19 17.25 Katherine Heigl 13 17.26 Sandra Bullock 17 17.36 Rachelle Lefevre 10 17.43 Anna Kournikova 18 17.43 Kate Beckinsale 14 17.48 Miley Cyrus 11 17.49 Nicole Richie 11 15.07 Fiona Apple 12 17.61 Heather Locklear 10 17.61 Tara Reid 14 17.61 Julia Stiles 12 17.69 Jenna Jameson 14 17.69 Kristen Stewart 13 17.71 Lucy Lawless 12 17.71 Mandy Moore 8 17.77 Angelina Jolie 15 17.77 Julia Roberts 14 17.77 Lisa Kudrow 10 17.77 Milla Jovovich 12 17.79 Britney Spears 18 17.79 Shania Twain 19 17.79 Gwen Stefani 16 17.91 Mariah Carey 14 17.95 Cindy Margolis 15 18.06 Halle Berry 12 18.10 Blake Lively 10 18.15 Eva Longoria 12 18.15 Lacey Chabert 15 18.15 Jewel Kilcher 15 18.15 Kate Hudson 11 18.16 Natalie Portman 10 18.23 Courtney Love 13 18.26 Jennifer Aniston 12 18.29 Charlize Theron 13 18.30 Jenny McCarthy 11 18.34 Leann Rimes 17 18.44 Christina Aguilera 12 18.44 Megan Fox 11 18.50 Bar Rafaeli 13 18.50 Alicia Keys 13 18.50 Denise Richards 19 18.50 Jessica Alba 11 18.50 Zooey Deschanel 21 18.55 Geri Halliwell 13 18.65 Ashley Judd 11 18.65 Laetitia Casta 11 18.65 Emily Osment 12 18.79 Daniel Radcliffe 12 18.98 Rupert Grint 13 19.05 Elisha cuthbert 11 19.05 Scarlett Johansson 11 19.07 Rachel Miner 16 19.14 Carmen Electra 16 19.14 Carrie Underwood 7 19.30 Keri Russell 18 19.51 Jennifer Lopez 16 19.76 Avril Lavigne 14 20.06 Drew Barrymore 12 20.16 Reese Witherspoon 15 20.28 Beyonce Knowles 13 20.57 Salma Hayek 12 20.65 Justin Timberlake 11 20.70 Jessica Simpson 13 20.89 Orlando Bloom 13 21.83 Johnny Depp 12 22.14 Matt Damon 11 22.44 Leonardo Dicaprio 8 22.64 Rodrigo Santoro 12 22.75 Raven Symone 11 22.79 Robert Pattinson 9 22.79 Ryan Seacrest 14 22.91 Hilary Duff 22 23.79 Emma Bunton 10 24.42 Jason Statham 8 24.44 Mark Wahlberg 16 25.26 Daniel Craig 12 25.83 George Clooney 13 25.90 Al Pacino 17 26.29 Ben Stiller 9 27.06 Brad Pitt 8 27.89 Bruce Willis 16 29.30 Vin Diesel 16 31.00 Sylvester Stallone 8 35.96 Hulk Hogan 14 WomanWithCat:dvla_20150202 grahamellis$ */ Learn about this subject
This module and example are covered as required on private courses.
Should you wish to cover this example and associated subjects, and you're attending a public course
to cover other topics with us, please see our extra topic program.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering Java and associated technologies are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "this" training module. You'll find a description of the topic and some
other closely related examples on the "this" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |