Question

In: Computer Science

Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests...

Program Specifications:

Write a program that defines a class HumanBMI, implements it as required, and tests the class implementation. The class definition and implementation should be separated into HumanBMI.h and HumanBMI.cpp files.

A. The class HumanBMI consists of three private member variables: name of type string, height of type int in inches, and weight of type int in pounds. The class HumanBMI also includes the following public member functions:


1) setName to set the name member variable with a string argument.

2) setHeight to set the height member variable with an int argument. The value of height should be a positive number, otherwise use 1 as a valid value.

3) setWeight to set the weight member variable with an int argument. The value of weight should be a positive number, otherwise use 1 as a valid value.

4) getBMI to calculate and return the value of BMI (Body Mass Index), in floating-point value, based on the following formula:  

  BMI = 703 * weight /(height * height)

5) print to output the name, height, weight, and BMI of a person. If a BMI is lower than 18.5, print out "Underweight", otherwise, print out "Not underweight".

6) equals to compare two Human objects’ name, height, and weight values, respectively, and return true if they are the same, otherwise return false.

7)  An overloaded constructor with default parameters to initialize name, height and weight. The default values for name, height, and weight are "None", 1, and 1, respectively.

8) A copy constructor to create an object and initialize it with an existing HumanBMI object.

B. In the client program Main.cpp, you should declare class objects and test each constructor and member function.

--------------------------------------------------------------------------------------------------------------------------------------------------------------

>> HINT (Submit 3 Files): HumanBMI.h, HumanBMI.cpp, and Main.cpp

***DO NOT USE! Advanced Codinng Methods***

**Must Include Comments for FULL CREDIT**

Solutions

Expert Solution

Program Code [C++]

HumanBMI.h

// HumanBMI class

#include <iostream>
using namespace std;

class HumanBMI {
  
   private:
      
       // Required member variables
      
       string name;
       int height;
       int weight;
      
   public:
      
       // All required member functions
      
       void setName(string);
       void setHeight(int);
       void setWeight(int);
      
       float getBMI();
       void print();
       bool equals(const HumanBMI&);

       // Overloaded & copy constructor

       HumanBMI(string, int, int);
       HumanBMI(const HumanBMI&);
};

HumanBMI.cpp

#include "HumanBMI.h"

// Overloaded constructor
HumanBMI::HumanBMI(string n, int h, int w):name{"None"}, height{1}, weight{1} {

   setName(n);
   setHeight(h);
   setWeight(w);
}

// Copy constructor
HumanBMI::HumanBMI(const HumanBMI& other) {
  
   this->name = other.name;
   this->height = other.height;
   this->weight = other.weight;
}

// Other methods
void HumanBMI::setName(string n) {
   name = n;
}
void HumanBMI::setHeight(int h) {
  
   if(h > 0) // Check height must be positive
       height = h;
   else
       height = 1;
}
void HumanBMI::setWeight(int w) {
  
   if(w > 0)
       weight = w;
   else
       weight = 1;
}

float HumanBMI::getBMI() {
  
   float BMI = 703 * ((float)weight / (height * height));
   return BMI;
}

void HumanBMI::print() {
  
   // Printing each member info
  
   cout << "Name: " << name << endl
       << "Height: " << height << endl
       << "Weight: " << weight << endl;
      
   float BMI = getBMI();
  
   // Calculating condition for BMI and printing it
  
   if(BMI < 18.5)
       cout << "Underweight";
   else
       cout << "Not underweight";

   cout << endl;
}

bool HumanBMI::equals(const HumanBMI& other) {
  
   // Comparing all data members
  
   return ((name == other.name) && (height == other.height) && (weight == other.weight));
}

Main.cpp

#include <iostream>
#include "HumanBMI.h"

using namespace std;

int main() {
  
   // Creating HumanBMI object and testing all methods
  
   HumanBMI h1("John", 60, 100);
   HumanBMI h2(h1);
   h2.setName("Kate");
   h2.setHeight(50);
   h2.setWeight(120);
  
   h1.print();
   cout << endl;
   h2.print();
  
   // Comparing two HumanBMI objects
  
   cout << endl;
   if(h1.equals(h2))
       cout << "h1 and h2 objects are equal" << endl;
   else
       cout << "h1 and h2 objects are not equal" << endl;

   return 0;
}

Sample Output:-

----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine class. Definition of classes from the implementation of the classes should be split. The program is made of five files: Lion.h, Lion.cpp, Pine.h, Pine.cpp, and TestLionPine.cpp. The components of Lion class are defined in the Lion.h file; however, all constructors and methods should not have any implementation code in this header file. All implementation code, i.e. constructor body and method body, should be written...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song titles by classifying them according to genre (e.g., Pop, Rock, etc.). The class uses a HashMap to map a genre with a set of songs that belong to such a genre. The set of songs will be represented using a HashSet. Your driver output should sufficiently prove that your code properly implements the code below. public class SongsDatabase { private Map<String, Set<String>> genreMap =...
Design and implement the class Day that implements the day of the week in a program....
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day: Set the day. Print the day. Return the day. Return the next day. Return the previous day. Calculate and return the day by adding certain days to the current day. For example, if the current day...
You are required to write 4 significant JUnit tests for the Account class outlined below taking...
You are required to write 4 significant JUnit tests for the Account class outlined below taking into account the specifications below. Your code must include 3 negative test cases. Specifications for the Account class: The constructor takes as arguments the name, ID and the initial balance. The withdrawn method allows any specified amount between $20 and $1000 to be withdrawn but the withdrawn amount cannot exceed the current balance. The withdraw method is expected to throw the AmtTooLow exception if...
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
Write a python program that implements a Brute Force attack on Shift Cipher. In this program...
Write a python program that implements a Brute Force attack on Shift Cipher. In this program there is only one input - ciphertext - is a sequence of UPPER CASE letters. To make it easy, the program will be interactive and will output all possible plaintexts and ask user which plaintext makes sense. As soon as user will decide YES, the program will stop searching and print the desired plaintext and the found SHIFT KEY.
Write a Java program that implements a queue in a hospital. I want your program to...
Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S, to store...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include member variables for name (string), price (double), shares (double). Write a default constructor. Write a constructor that takes values for all member variables as parameters. Write a copy constructor. Implement Get/Set methods for all member variables. Implement the CalculateValue function. This function should multiply the prices by the shares and return that value. Use the following function header: double CalculateValue(). Add a member overload...
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.
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT