Greetings to all experts,

I am a newbie here in Java OOP and is struggling in practicing how to code a OO program, using Java. I hope anyone can give me a head start please.

Here is the requirement which i am suppose to complete:

Input
The first line of the input contains an integer N (1 <= N <= 100) denoting the number of people in the group. The next N lines contain the information (name, height in centimeters, and weight in kilograms) of the people in the group.

Output
Output the name of the shortest and tallest people in the group with format:
Suppose A is the shortest and B is the tallest person in the group, then the output will be:
A is the shortest with BMI equals to C.
B is the tallest with BMI equals to D.
where C is the BMI for A and D is the BMI for B. Output the BMI with 2 digit after the decimal point rounded to the nearest integer.

Sample Input
5
Diamond 178 55
Jarod 160 80
Douglas 180 60
Rod 151 48
Joe 178 55

Sample Output
Rod is the shortest with BMI equals to 21.05.
Douglas is the tallest with BMI equals to 18.52.

Thus, I have created 2 classes namely Person and Measurement (main method can be found in here). Below are the codes which i am working on:

import java.util.*;

// create an object called Person that has attribute name, height, weight
class Person {
	// declare the attributes
	String name;
	double height, weight;
	
	// declare the constructor
 public Person (double h, double w)
 {
	height = h;
	weight = w; 
 }
	/* use this method to compute the BMI for that person 
	 * 		PRE-Condition  :
	 * 		POST-Condition :
	 */
	public double computeBMI() {
		return (weight / height * height);
		// implement the BMI formula
	}
	
}

public class Measurement {
	
	public static void main(String[] args) {
		// declare the necessary variables
		int count;
	
		// declare a Scanner object to read input
		Scanner scan = new Scanner(System.in);

		// read input and process them accordingly
		count = scan.nextInt();
		Person.name = scan.nextLine();
		Person.h = scan.nextDouble();
		Person.w = scan.nextDouble();

		// simulate the problem

		// output the result

		System.out.println(Person.computeBMI);
	}
}

Thank you so much for all your guidance.

Recommended Answers

All 2 Replies

The Person class seems ok. With the following corrections. You need to pass the name at the constructor as well. And you need to add get/set methods:

Example:

public double getHeight() {
  return height;
}

This is wrong:
Person.name
You need to declare an instance of the Person and call the constructor:

String name = scan.next(); // if you use here nextLine it will read the whole line not just the name!
double height = scan.nextDouble();
double weight = scan.nextDouble();

Person per = new Person(name, height, weight);
System.out.println(per.computeBMI());

I don't believe that the computeBMI is correct. You need to find the correct formula.

Also after you get the count, you need to create an array of Person objects, loop it and give each element a value:

int count = scan.nextInt();
scan.nextLine();
Person [] persons = new Person[count];

for (int i=0;i<persons.length;i++) {
  System.out.println("Enter Data "+i);

  String name = scan.next();
  double height = scan.nextDouble();
  double weight = scan.nextDouble();
  scan.nextLine();

  Person per = new Person(name, height, weight);
  persons[i] = per;

  // OR persons[i] = new Person(name, height, weight);
}

After you have the array, you can loop it, take each element: persons, from it take the height and apply any standard algorithm for taking the max/min.
You will use the height of each element for the comparison but you will be saving the whole person object at the temp variables:

Person maxPerson;

Also you will notice that I have added a few extra scan.nextLine() calls. Every time you call the nextInt, nextDouble it doesn't read the whole line, only the next number. So you need to call the nextLine in order to change lines after each one of them.
That is why after the last nextDouble before you call the next for the name you need the extra nextLine:


Diamond 178 55
Jarod 160 80
next: Diamond
nextDouble: 178
nextDouble: 55
nextLine (changes line)
next: Jarod
nextDouble: 160
nextDouble: 80

I could do it without an array. Can you? :-/

javaAddict is correct that there are issues with the formula in the computeBMI method. Get the rest of it working, and then the issues will be apparent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.