Vous êtes sur la page 1sur 2

Structure, Class and Objects Extra Questions

1. Create a class called Invoice that a hardware store might use to represent an invoice for
an item sold at the store. An Invoice should include four data membersa part number
(type string), a part description (type string), a quantity of the item being purchased (type
int) and a price per item (type int). Your class should have a constructor that initializes
the four data members. Provide a set and a get function for each data member. In
addition, provide a member function named getInvoiceAmount that calculates the invoice
amount (i.e., multiplies the quantity by the price per item), then returns the amount as an
int value. If the quantity is not positive, it should be set to 0.If the price per item is not
positive, it should be set to 0.Write a test program that demonstrates class Invoices
capabilities.

2. (Target-Heart-Rate Calculator) While exercising, you can use a heart-rate monitor to see
that your heart rate stays within a safe range suggested by your trainers and doctors.
According to the American Heart Association (AHA) the formula for calculating your
maximum heart rate in beats per minute is 220 minus your age in years. Your target heart
rate is a range that is 5085%ofyourmaximum heart rate. [Note: These formulas are
estimates provided by the AHA. Maximum and target heart rates may vary based on the
health, fitness and gender of the individual. Always consult a physician or qualified
health care professional before beginning or modifying an exercise program.]Create a
class called HeartRates. The class attributes should include the persons first name, last
name and date of birth (consisting of separate attributes for the month, day and year of
birth). Your class should have methods to get and display these attributes. The class also
should include a function getAge that calculates and returns the persons age(in years),a
function getMaxiumumHeartRate that calculates and returns the persons maximum heart
rate and a function getTargetHeartRate that calculates and returns the persons target
heart rate. Since you do not yet know how to obtain the current date from the computer,
function getAge should prompt the user to enter the current month, day and year before
calculating the persons age. Write an application that prompts for the persons
information, instantiates an object of class HeartRates and prints the information from
that objectincluding the persons first name, last name and date of birththen
calculates and prints the persons age in(years),maximum heart rate and target-heart-rate
range
3. Create a class that imitates part of the functionality of the basic data type int. Call the
class Int (note different capitalization). The only data in this class is an int variable.
Include member functions to initialize an Int to 0, to initialize it to an int value, to display
it (it looks just like an int), and to add two Int values. Write a program that exercises this
class by creating one uninitialized and two initialized Int values, adding the two
initialized values and placing the response in the uninitialized value, and then displaying
this result.
4. Write a complete definition for a class called Rational that implements basic operations
on rational numbers, which are represented internally as a pair of long integers. Your
class should implement the public methods shown in Figure 9-7. All data members
should be private to the class.
Methods to be implemented for the Rational class
set(n, d) This method creates a rational number with n and d as its numerator and
denominator, respectively. Your implementation should always reduce
the internal fraction to lowest terms and show the sign of the value in the
numerator. The denominator of the internal value should always be
positive
r1.add(r2) These methods compute a new rational number which is the result of
r1.sub(r2) applying the indicated arithmetic operation to the rational numbers r1
r1.mul(r2) and r2. The results of these operations are given by the following
r1.div(r2) formulae:

r1.equals(r2) This method returns true if r1 and r2 are equal.


r1.compareTo(r2) This method returns an integer whose sign reflects the relationship
between r1 and r2 in the manner of comparison functions.

5. Create an abstract data type that represents a video tape in a video rental store. Try to
consider all the data and operations that may be necessary for the Video type to work
well within the video rental management system. Include a print ( ) member function that
displays information about the Video.

Vous aimerez peut-être aussi