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 =...
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
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
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.
Write a program in Java and run it in BlueJ according to the following specifications: The...
Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line) and determines their type (excellent or ok). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "all" - prints all student records (first name, last name, grade, type). "excellent" - prints students with grade > 89. "ok"...
In this assignment, you will write a class that implements a contact book entry. For example,...
In this assignment, you will write a class that implements a contact book entry. For example, my iPhone (and pretty much any smartphone) has a contacts list app that allows you to store information about your friends, colleagues, and businesses. In later assignments, you will have to implement this type of app (as a command line program, of course.) For now, you will just have to implement the building blocks for this app, namely, the Contact class. Your Contact class...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT