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...
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"); } }...
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?
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
calculate the BMi. weight 138lb, height 5ft and 5 inches
calculate the BMi. weight 138lb, height 5ft and 5 inches
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem 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.It must have a default constructor.It must have a constructor which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT