You are on page 1of 3

Lab 1 – CLASS 1

1. Date

Design a class called Date. The class should store a date in three integers: month, day, and year. There
should be member functions to print the date in the following forms:

12/25/2019
December 25, 2019
25 December 2019

Demonstrate the class by writing a complete program implementing it.

Input Validation: Do not accept values for the day greater than 31 or less than 1. Do not accept values for
the month greater than 12 or less than 1.

2. Population

In a population, the birth rate and death rate are calculated as follows:

Birth Rate = Number of Births ÷ Population

Death Rate = Number of Deaths ÷ Population

For example, in a population of 100,000 that has 8,000 births and 6,000 deaths per year, the birth rate
and death rate are:

Birth Rate = 8,000 ÷ 100,000 = 0.08

Death Rate = 6,000 ÷ 100,000 = 0.06

Design a Population class that stores a population, number of births, and number of deaths for a period
of time. Member functions should return the birth rate and death rate. Implement the class in a program.

Input Validation: Do not accept population figures less than 1, or birth or death numbers less than 0.

3. Mortgage Payment

Design a class that will determine the monthly payment on a home mortgage. The monthly payment with
interest compounded monthly can be calculated as follows:
𝑟𝑎𝑡𝑒
𝑙𝑜𝑎𝑛 × × 𝑡𝑒𝑟𝑚
𝑝𝑎𝑦𝑚𝑒𝑛𝑡 = 12
𝑡𝑒𝑟𝑚 − 1
where

𝑟𝑎𝑡𝑒 45×6789:
𝑡𝑒𝑟𝑚 = 11 + 3
12
Payment = the monthly payment
Loan = the dollar amount of the loan
Rate = the annual interest rate
Years = the number of years of the loan

The class should have member functions for setting the loan amount, interest rate, and number of years
of the loan. It should also have member functions for returning the monthly payment amount and the
total amount paid to the bank at the end of the loan period. Implement the class in a complete program.
Input Validation: Do not accept negative numbers for any of the loan values.

4. Fishing Game Simulation

For this problem, you will write a program that simulates a fishing game. In this game, a six-sided die is
rolled to determine what the user has caught. Each possible item is worth a certain number of fishing
points. The points will not be displayed until the user has finished fishing, and then a message is displayed
congratulating the user depending on the number of fishing points gained. Here are some suggestions for
the game’s design:

- Each round of the game is performed as an iteration of a loop that repeats as long as the player
wants to fish for more items.
- At the beginning of each round, the program will ask the user whether he or she wants to continue
fishing.
- The program simulates the rolling of a six-sided die (use the Die class that was demonstrated
below)
- Each item that can be caught is represented by a number generated from the die. For example, 1
for “a huge fish,” 2 for “an old shoe,” 3 for “a little fish,” and so on.
- Each item the user catches is worth a different amount of points.
- The loop keeps a running total of the user’s fishing points.
- After the loop has finished, the total number of fishing points is displayed, along with a message
that varies depending on the number of points earned.

Contents of Die.h
// Specification file for the Die class
#ifndef DIE_H
#define DIE_H

class Die
{
private:
int sides; // Number of sides
int value; // The die's value

public:
Die(int = 6); // Constructor
void roll(); // Rolls the die
int getSides(); // Returns the number of sides
int getValue(); // Returns the die's value
};
#endif

Contents of Die.cpp
// Implememtation file for the Die class
#include <cstdlib> // For rand and srand
#include <ctime> // For the time function
#include "Die.h"
using namespace std;

//*******************************************************
// The constructor accepts an argument for the number *
// of sides for the die, and performs a roll. *
//*******************************************************
Die::Die(int numSides)
{
// Get the system time.
unsigned seed = time(0);

// Seed the random number generator.


srand(seed);

// Set the number of sides.


sides = numSides;

// Perform an initial roll.


roll();
}

//*******************************************************
// The roll member function simulates the rolling of *
// the die. *
//*******************************************************
void Die::roll()
{
// Constant for the minimum die value
const int MIN_VALUE = 1; // Minimum die value

// Get a random value for the die.


value = (rand() % (sides - MIN_VALUE + 1)) + MIN_VALUE;
}

//*******************************************************
// The getSides member function returns the number of *
// for this die. *
//*******************************************************
int Die::getSides()
{
return sides;
}

//********************************************************
// The getValue member function returns the die's value.*
//********************************************************
int Die::getValue()
{
return value;
}

You might also like