0

Ok, I am very close to the solution but I just can not figure this out. For some reason my code is not generating random ages, heights, weights etc. Additionally, I am getting an error "non static method getBodyMassIndex() can not be referenced from static context lines 43 & 45. I have done research and read that the error is because I need to create an instance to apply this to but I am lost. I thought I had already done that and I can't get anything to work. This is basically a self-taught java course and I'm really struggling. Any help greatly appreciated!! Code follows:

package classlab3b;

import classlab3B.BodyMassIndex;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author ccity
 */
public class ClassLab3B {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Please enter the number of people:");
        //asks user to input number of people
        int numberOfPeople;
        //declares integer variable for number of people
        Scanner input = new Scanner(System.in);
        //creates system input scanner
        numberOfPeople = input.nextInt();
        //captures user input for number of people
        BodyMassIndex[] a = new BodyMassIndex[numberOfPeople];
        //creates an array of BodyMassIndex the size of numberOfPeople
        String name = loadRandomNames(a);
        //loads object with random name
        int age = loadRandomAges(a, numberOfPeople);
        //loads object with random age
        double weight = loadRandomWeights(a);
        //loads object with random weight
        double height = loadRandomHeights(a);
        //loads object with random height
        createObjectsToFillArray(a, name, age, weight, height, numberOfPeople);
        //creates "x" objects to fill the array
        double BMI = BodyMassIndex.getBodyMassIndex();
        //gets BMI from getBodyMassIndex method in BodyMassIndex.java
        String status = BodyMassIndex.getStatus();
        //gets status from getStatus method in BodyMassIndex.java
        //double BMI = BodyMassIndex.bmix.getBodyMassIndex();
        //String status = BodyMassIndex.bmix.getStatus();
        printArray(a, name, age, weight, height, BMI, status);
        //prints array


        System.out.println(" ");
        System.out.println("Current Population");
        System.out.println(" ");
        System.out.println("Obese: " + BodyMassIndex.numberOfObese);
        System.out.println("Overweight: " + BodyMassIndex.numberOfOverweight);
        System.out.println("Normal: " + BodyMassIndex.numberOfNormal);
        System.out.println("Underweight: " + BodyMassIndex.numberOfUnderweight);
        System.out.println("================");
        System.out.println("Total: " + BodyMassIndex.totalNumberOfPeople);


    }

    public static void createObjectsToFillArray(BodyMassIndex[] data, String name, int age, double weight, double height, int numberOfPeople) {
        for (int i = 0; i < numberOfPeople; i++) {
            data[i] = new BodyMassIndex(name, age, weight, height);
        }


        //creates new BodyMassIndex objects with generated variables from methods within
    }

    public static String loadRandomNames(BodyMassIndex[] data) {

        String[] arrayOfFirstNames = {"Joe", "Donna", "Ronald", "Sarah", "David", "Courtney", "Irwin", "Linda", "Michael", "Cindy", "Tom", "Rebekah", "Todd", "Tracy", "Peter", "Nicole", "Marcelo", "Jennifer", "Rick", "Andrea", "Bruce", "Jaclyn", "Doug", "Shirley", "Steve", "Liz", "Waldo", "Theresa", "Scott", "Colby", "Beth", "Larry", "Emily", "Paul", "Kate", "Sam", "Dianne", "Dustin", "Alethea", "Wayne", "Kristina", "Christian", "Danny", "Breya", "Andrew", "Alison", "Tim", "Mary", "Chris", "Susie", "Jeremy", "Willy", "Jessica", "Marcus", "Kelly", "Kyle", "Stephanie", "Isaiah", "Hillary", "Eric", "Julia", "Donald", "Meredith", "Kevin", "Leslie", "Blake", "Angela", "Cliff", "Debbie", "Dylan", "Erin", "Alex", "Monica", "Nathan", "Wendy", "Josh", "Megan", "Adam", "Michelle", "Carey", "Ashley", "Brian", "Jason", "Melanie", "Jim", "Monica", "Jamie", "Rhonda", "Steven", "Perry", "Byron", "Laura", "Harry", "Brooke", "Drew", "Vicki", "Gary", "Anita", "Felipe", "Josie"};
        String[] arrayOfLastNames = {"Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor", "Washington", "Jefferson", "Lincoln", "Hamilton", "Jackson", "Grant", "Franklin", "McKinley", "Cleveland", "Madison", "Chase", "Nicholson", "Fauver", "Doe", "Southard", "Schmidt", "Hodson", "McDonald", "Stickley", "Miller", "Combs", "Bohus", "Krippner", "Amtower", "Banks", "Wallace", "Bannister", "Dehaven", "Yost", "Still", "Timbrook", "Peters", "Vaught", "Shellhammer", "Andrews", "Krippner", "McAlister", "Wright", "Kensinger", "McClellan", "Ganoe", "Shiley", "Layman", "Gearhart", "Yost", "Kushnir", "Bush", "Lowder", "Connolly", "Lowman", "Terveen", "Staton", "Settle", "Tinsman", "Nichols", "Baker", "Walters", "Dawe", "Renner", "Michaels", "Faircloth", "Looker", "Hastings", "Vaughan", "Anderson", "Zimmerman", "Deere", "Daher", "Lauck", "Stottlemyer", "Clinton", "Obama", "Reagan", "Montgomery", "Pugh", "Gavis", "Clark", "Bowers"};

        String first = get(arrayOfFirstNames);
        String last = get(arrayOfLastNames);
        String name = first + " " + last;

        return name;
    }

    public static String get(String[] array) {
        Random generator = new Random();
        int rnd = generator.nextInt(array.length);
        return array[rnd];
    }

    public static int loadRandomAges(BodyMassIndex[] data, int numberOfPeople) {
        double min = 13;
        double max = 99;
        int age = 0;
        for (int i = 0; i < numberOfPeople; i++) {
            age = (int) randomInt(min, max);
        }

        return age;
    }

    public static double randomInt(double min, double max) {

        double random = (double) ((max - min + 1) * Math.random() + min);
        return random;

    }

    public static double loadRandomWeights(BodyMassIndex[] data) {
        double min = 100;
        double max = 300;
        double weight = randomInt(min, max);
        for (int row = 0; row < data.length; row++) {
        }
        return weight;
    }

    public static double loadRandomHeights(BodyMassIndex[] data) {
        double min = 55;
        double max = 80;
        double height = randomInt(min, max);
        for (int row = 0; row < data.length; row++) {
        }
        return height;
    }

    public static void printArray(BodyMassIndex[] data, String name, int age, double weight, double height, double BMI, String status) {
        System.out.println("    Name           " + "Age    " + "Height    " + "Weight    " + "BMI    " + "Status");
        for (int i = 0; i < data.length; i++) {

            DecimalFormat format = new DecimalFormat();
            format.setMinimumFractionDigits(2);
            format.setMaximumFractionDigits(2);


            System.out.println(name + "      " + age + "      " + format.format(height) + "    " + format.format(weight) + format.format(BMI) + "   " + status);
        }
    }
}

And here's the BodyMassIndex.java code:

package classlab3B;

/**
 *
 * @author Liz
 */
public class BodyMassIndex {

    private String name;
    private int age;
    private double weight;
    private double height;
    public static final double OBESE_BMI = 30.0;
    public static final double OVERWEIGHT_BMI = 25.0;
    public static final double NORMAL_BMI = 18.5;
    public static int numberOfObese;
    public static int numberOfOverweight;
    public static int numberOfNormal;
    public static int numberOfUnderweight;
    public static int totalNumberOfPeople;

    public static void main(String[] args) {


        BodyMassIndex bmi1 = new BodyMassIndex("John Doe", 18, 145, 70);
        //BodyMassIndex bmix = new BodyMassIndex(ClassLab3B.name, ClassLab3B.age, ClassLab3B.weight, ClassLab3B.height);


        System.out.println("The BMI for " + bmi1.getName() + " is " + bmi1.getBodyMassIndex() + " " + bmi1.getStatus());

        System.out.println(" ");
        System.out.println(" ");
        System.out.println("Current Population");
        System.out.println(" ");
        System.out.println("Obese: " + numberOfObese);
        System.out.println("Overweight: " + numberOfOverweight);
        System.out.println("Normal: " + numberOfNormal);
        System.out.println("Underweight: " + numberOfUnderweight);
        System.out.println("================");
        System.out.println("Total: " + totalNumberOfPeople);
    }

    public BodyMassIndex(String name, int age, double weight, double height) {
        this.name = name;
        this.age = age;
        this.weight = weight;
        this.height = height;
        totalNumberOfPeople++;
    }

    public double getBodyMassIndex() {
        double bmi = ((weight * 703) / (height * height));
        return Math.round(bmi * 100) / 100.0;
    }

    public String getStatus() {
        double bmi = getBodyMassIndex();
        if (bmi < 16) {
            numberOfUnderweight++;
            return "seriously underweight";
        } else if (bmi < 18) {
            numberOfUnderweight++;
            return "underweight";
        } else if (bmi < 24) {
            numberOfNormal++;
            return "normal weight";
        } else if (bmi < 29) {
            numberOfOverweight++;
            return "overweight";
        } else if (bmi < 35) {
            numberOfObese++;
            return "grossly overweight";
        } else {
            numberOfObese++;
            return "gravely overweight";
        }
    }

    @Override
    public String toString() {
        return "BodyMassIndex{" + "name=" + name + ", age=" + age + ", weight=" + weight + ", height=" + height + '}';
    }

    public String getName() {
        return name;
    }
}
Alexander
  • 23,432
  • 11
  • 63
  • 73
user1309594
  • 25
  • 1
  • 1
  • 5
  • You need to highlight the line where the error occurs (I'm not going to count it manually) - also loadRandomAges() doesn't do anything onces it has chosen the age. – John3136 Apr 03 '12 at 06:00

4 Answers4

1

double BMI = BodyMassIndex.getBodyMassIndex(); String status = BodyMassIndex.getStatus();

what are doing in this 2 methods you have create this 2 methods as non static but calling using class name. If you create these 2 method as static then in this 2 mehod you are using non static variable too.

double []BMI=new double[numberOfPeople] ;
String []status=new String[numberOfPeople] ;

for(int i=0;i<=numberOfPeople-1;i++) {

BMI[i]=a[i].getBodyMassIndex();

status[i]=a[i].getStatus();

}

public static void printArray(BodyMassIndex[] data, String name, int age, double weight, double height, double[] BMI, String[] status)

Ronak Jain
  • 2,402
  • 2
  • 24
  • 38
0
  • You need to mark public double getBodyMassIndex() as static - you do not really need an instance of BodyMassIndex to call the method since you do not use any member data inside.

So, the signature of this method should be:

public static double getBodyMassIndex() {...}
  • The standard pattern to get Integer inside some range is:

    Min + (int)(Math.random() * ((Max - Min) + 1))

The java Math library function Math.random() generates a double value in the range [0,1). Notice this range does not include the 1.

In order to get a specific range of values first you need to multiply by the magnitude of the range of values you want covered.

Math.random() * ( Max - Min )

This returns a value in the range [0,Max-Min). If you want [5,10] you need cover 5 integer values so you use:

Math.random() * 5

This would return a value in the range [0,5)

Now you need to shift this range up to the range that you are targeting. You do this by adding the Min value.

Min + (Math.random() * (Max - Min))

You now will get a value in the range [Min,Max). Following our example, that means [5,10):

5 + (Math.random() * (10 - 5))

But, this is still doesn't include Max and you are getting a double value. In order to get the Max value included, you need to add 1 to your range parameter (Max - Min) and then truncate the decimal part by casting to an int. This is accomplished via:

Min + (int)(Math.random() * ((Max - Min) + 1))

And there you have it. A random integer value in the range [Min,Max], or per the example [5,10]

5 + (int)(Math.random() * ((10 - 5) + 1))

stolen from here

Good luck!

Community
  • 1
  • 1
aviad
  • 8,229
  • 9
  • 50
  • 98
  • Thank you soooo much! I fixed that. Something so simple has been so frustrating. Now any idea why I'm getting the same name, age, weight, etc for every entry? – user1309594 Apr 03 '12 at 06:11
0

Yes you cannot access the BodyMassIndex.getBodyMassIndex(); and BodyMassIndex.getStatus(); from your class called ClassLab3B since these two methods are not static..

If u make them static, u wont be able to access the non static members of ur class BodyMassIndex..

So u can return the BodyMassIndex[] from the createObjectsToFillArray() and then access each instance in a for loop and call the printArray()

something like this

BodyMassIndex[] x= createObjectsToFillArray(a, name, age, weight, height,   numberOfPeople);
    //creates "x" objects to fill the array

    for(int i=0;i<x.length;i++)
        {
        double BMI = x[i].getBodyMassIndex();
        //gets BMI from getBodyMassIndex method in BodyMassIndex.java
        String status = x[i].getStatus();
        //gets status from getStatus method in BodyMassIndex.java
        //double BMI = BodyMassIndex.bmix.getBodyMassIndex();
        //String status = BodyMassIndex.bmix.getStatus();
        printArray(a, name, age, weight, height, BMI, status);
        }

and

public static BodyMassIndex[] createObjectsToFillArray(BodyMassIndex[] data, String   name, int age, double weight, double height, int numberOfPeople) {
    for (int i = 0; i < numberOfPeople; i++) {
        data[i] = new BodyMassIndex(name, age, weight, height);
    }
return data;

    //creates new BodyMassIndex objects with generated variables from methods within
}
Jatin
  • 318
  • 1
  • 2
  • 9
-1

you need an Instance of your BodyMassIndex:

BodyMassIndex bmi = new BodyMassIndex(...)
double BMI = bmi.getBodyMassIndex();

The way you use it, means that the methods are declared as static ones in the BMI class. like:

public static double getBodyMassIndex() {...}

but that would not be a great design.

boskop
  • 609
  • 5
  • 23
  • no instance of `BodyMassIndex` required to call its static method. – aviad Apr 03 '12 at 06:03
  • yeah, i meant there are two ways of calling getBodyMassIndex(). As an Instance-Method with an explicit instance or in a static way like he has tried, but then the method has to be static. – boskop Apr 03 '12 at 06:36
  • BTW if the method is `static` the `bmi` does not even need to be instantiated: `BodyMassIndex bmi = null;` and later call to the `bmi.getBodyMassIndex()` will still work (check this out :) – aviad Apr 03 '12 at 06:59