Question

In: Computer Science

JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...

JAVA

  • The class will have a constructor BMI(String name, double height, double weight).
  • The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ).
  • The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting).
  • Implement this class (if you wish you may implement a main method that demonstrates its use).

Here is my code:

public class BMI {
    private String name;
    private Double height;
    private Double weight;


    Double getBMI(){
        return weight/(height*height);
    }
    public BMI(String name, Double height, Double weight){
        this.name = name;
        this.height = height;
        this.weight = weight;
    }

    @Override
    public String toString(){
        return name + " is " + height +"m tall and " + weight + "kg and has a BMI of" + getBMI() ;
    }
}

Solutions

Expert Solution

Have a look at the below code. I have put comments wherever required for better understanding.

// create class
class BMI{
    // create instance variables
    public String name;
    public double height;
    public double weight;
    // create constructor
    BMI(String name,double height,double weight){
        this.name = name;
        this.height = height;
        this.weight = weight;
    }
    // create get BMI method
    public double getBMI(){
        double numerator = this.weight;
        double denominator = this.height*this.height;
        return numerator/denominator;
    }
    // create toString method
    public String toString(){
        return this.name + " is " + this.height +"m tall and " + this.weight + "kg and has a BMI of " + getBMI();
    }
}

// create main class
class Main {
  public static void main(String[] args) {
    // create an object BMI class
    BMI bmi = new BMI("Fred",1.9,87.0);
    // call the toString method...
    System.out.println(bmi.toString());
  }
}

Happy Learning!


Related Solutions

IN JAVA PLEASE The Palindrome class will have a single constructor that accepts a String argument...
IN JAVA PLEASE The Palindrome class will have a single constructor that accepts a String argument and checks to see if the String is a palindrome. If it is a palindrome it will store the palindrome and the original String as state values. If is not a palindrome it will throw a “NotPalindromeError” and not finish the creation of the new Palindrome object. The constructor will use a single stack to evaluate if the String is a palindrome. You must...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance);...
Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance); const string & getName() const; double getDistance() const; void setDistance(double); // Passes in the departure time (as minute) and returns arrival time (as minute) // For example: // 8 am will be passed in as 480 minutes (8 * 60) // 2:30 pm will be passed in as 870 minutes (14.5 * 60) virtual unsigned computeArrivalTime(unsigned minute) const = 0; }; #endif Derived Classes...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and address (String). Implement the initialization constructor . Implement the setters and getters for all attributes. Implement the toString() method to display all attributes. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. Implement the compareTo (Entry other) method that returns 0 if the two...
you will find a class called Employee. This class should include a constructor that sets name...
you will find a class called Employee. This class should include a constructor that sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will create a...
A constructor, with a String parameter representing the name of the item.
USE JAVA Item classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.Inventory classA constructor which takes a String parameter indicating the item type, an int parameter indicating the initial...
Physiological class. In measurement. How and why might the Weight/height ratio be more valuable than BMI?
Physiological class. In measurement. How and why might the Weight/height ratio be more valuable than BMI?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT