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, 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...
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...
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...
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...
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...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
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?
calculate the BMi. weight 138lb, height 5ft and 5 inches
calculate the BMi. weight 138lb, height 5ft and 5 inches
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT