Programming Projects require more problem-solving than Practice Programs and can usually be solved many different ways. Visit www.myprogramminglab.com to complete many of these Programming Projects online and get instant feedback.
Write a grading program for a class with the following grading policies:
There are two quizzes, each graded on the basis of 10 points.
There is one midterm exam and one final exam, each graded on the basis of 100 points.
The final exam counts for 50 percent of the grade, the midterm counts for 25 percent, and the two quizzes together count for a total of 25 percent. (Do not forget to normalize the quiz scores. They should be converted to a percent before they are averaged in.)
Any grade of 90 or more is an A, any grade of 80 or more (but less than 90) is a B, any grade of 70 or more (but less than 80) is a C, any grade of 60 or more (but less than 70) is a D, and any grade below 60 is an F.
The program will read in the student’s scores and output the student’s record, which consists of two quiz and two exam scores as well as the student’s average numeric score for the entire course and the final letter grade. Define and use a structure for the student record. If this is a class assignment, ask your instructor if input/output should be done with the keyboard and screen or if it should be done with files. If it is to be done with files, ask your instructor for instructions on file names.
Redo Programming Project 1 (or do it for the first time), but this time make the student record type a class type rather than a structure type. The student record class should have member variables for all the input data described in Programing Project 1 and a member variable for the student’s weighted average numeric score for the entire course as well as a member variable for the student’s final letter grade. Make all member variables private. Include member functions for each of the following: member functions to set each of the member variables to values given as an argument(s) to the function, member functions to retrieve the data from each of the member variables, a void
function that calculates the student’s weighted average numeric score for the entire course and sets the corresponding member variable, and a void
function that calculates the student’s final letter grade and sets the corresponding member variable.
Define a class called Month
that is an abstract data type for a month. Your class will have one member variable of type int
to represent a month (1 for January, 2 for February, and so forth). Include all the following member functions: a constructor to set the month using the first three letters in the name of the month as three arguments, a constructor to set the month using an integer as an argument (1 for January, 2 for February, and so forth), a default constructor, an input function that reads the month as an integer, an input function that reads the month as the first three letters in the name of the month, an output function that outputs the month as an integer, an output function that outputs the month as the first three letters in the name of the month, and a member function that returns the next month as a value of type Month
. The input and output functions will each have one formal parameter for the stream. Embed your class definition in a test program.
Redefine the implementation of the class Month
described in Programming Project 3 (or do the definition for the first time, but do the implementation as described here). This time the month is implemented as three member variables of type char
that store the first three letters of the name of the month. Embed your definition in a test program.
(In order to do this project you must have first done either Programming Project 3 or Project 4.) Rewrite the program in Display 10.4, but use the class Month
that you defined in Project 3 or Project 4 as the type for the member variable to record the month. (You may define the class Month
either as described in Project 3 or as described in Project 4.) Redefine the member function output
so that it has one formal parameter of type ostream
for the output stream. Modify the program so that everything that is output to the screen is also output to a file. This means that all output statements will occur twice: once with the argument cout
and once with an output-stream argument. If you are in a class, obtain the file name from your instructor. The input will still come from the keyboard. Only the output will be sent to a file.
My mother always took a little red counter to the grocery store. The counter was used to keep a tally of the amount of money she would have spent so far on that visit to the store, if she bought all the items in her basket. There was a four-digit display, increment buttons for each digit, and a reset button. There was an overflow indicator that came up red if more money was entered than the $99.99
it would register. (This was a long time ago.)
Write and implement the member functions of a class Counter
that simulates and slightly generalizes the behavior of this grocery store counter. The constructor should create a Counter
object that can count up to the constructor’s argument. That is, Counter(9999)
should provide a counter that can count up to 9999
. A newly constructed counter displays a reading of 0
. The member function void
reset();
sets the counter’s number to 0
. The member functions void
incr1();
increments the units digit by 1, void
incr10();
increments the tens digit by 1, and void
incr100();
and void
incr1000( );
increment the next two digits, respectively. Accounting for any carry when you increment should require no further action than adding an appropriate number to the private data member. A member function bool
overflow();
detects overflow. (Overflow is the result of incrementing the counter’s private data member beyond the maximum entered at counter construction.)
Use this class to provide a simulation of my mother’s little red clicker. Even though the display is an integer, in the simulation, the rightmost (lower-order) two digits are always thought of as cents and tens of cents, the next digit is dollars, and the fourth digit is tens of dollars.
Provide keys for cents, dimes, dollars, and tens of dollars. Unfortunately, no choice of keys seems particularly mnemonic. One choice is to use the keys asdfo
: a for cents, followed by a digit 1 to 9; s for dimes, followed by digits 1 to 9; d for dollars, followed by a digit 1 to 9; and f for tens of dollars, again followed by a digit 1 to 9. Each entry (one of asdf
followed by 1 to 9) is followed by pressing the Return key. Any overflow is reported after each operation. Overflow can be requested by pressing the o key.
Write a rational number
class. This problem will be revisited in Chapter 11, where operator overloading will make the problem much easier. For now we will use member functions add
, sub
, mul
, div
, and less
that each carry out the operations +
, -
, *
, /
, and <
. For example, a + b
will be written a.add(b)
, and a < b
will be written a.less(b)
.
Define a class for rational numbers. A rational number is a “ratio-nal” number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2
, 2/3
, 15/32
, 65/4
, 16/5
. You should represent rational numbers by two int
values, numerator
and denominator
.
A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int
values; this is a constructor with two int
parameters. Since every int
is also a rational number, as in 2/1
or 17/1
, you should provide a constructor with a single int
parameter.
Provide member functions input
and output
that take an istream
and ostream
argument, respectively, and fetch or write rational numbers in the form 2/3
or 37/51
to or from the keyboard (and to or from a file).
Provide member functions add
, sub
, mul
, and div
that return a rational value. Provide a function less
that returns a bool
value. These functions should do the operation suggested by the name. Provide a member function neg
that has no parameters and returns the negative of the calling object.
Provide a main
function that thoroughly tests your class implementation. The following formulas will be useful in defining functions.
a/b + c/d = (a * d + b * c) / (b * d)
a/b − c/d = (a * d − b * c) / (b * d)
(a/b) * (c/d) = (a * c) / (b * d)
(a/b) / (c/d) = (a * d) / (c * b)
−(a/b) = (−a/b)
(a/b) < (c/d) means (a * d) < (c * b)
(a/b) == (c/d) means (a * d) == (c * b)
Let any sign be carried by the numerator; keep the denominator positive.
Define a class called Odometer
that will be used to track fuel and mileage for an automotive vehicle. Include private member variables to track the miles driven and the fuel efficiency of the vehicle in miles per gallon. The class should have a constructor that initializes these values to zero. Include a member function to reset the odometer to zero miles, a member function to set the fuel efficiency, a member function that accepts miles driven for a trip and adds it to the odometer’s total, and a member function that returns the number of gallons of gasoline that the vehicle has consumed since the odometer was last reset.
Use your class with a test program that creates several trips with different fuel efficiencies.
Redo Programming Project 7 from Chapter 5 (or do it for the first time), but this time use a class to encapsulate the date. Use private member variables to store the day, month, and year along with an appropriate constructor and member functions to get and set the data. Create a public function that returns the day of the week. All helper functions should be declared private. Embed your class definition in a suitable test program.
The U.S. Postal Service printed a bar code on every envelope that represented a five- (or more) digit zip code using a format called POSTNET (this format was deprecated in favor of a new system, OneCode, in 2009). The bar code consists of long and short bars as shown:
For this program, we will represent the bar code as a string of digits. The digit 1 represents a long bar, and the digit 0 represents a short bar. Therefore, the bar code would be represented in our program as
110100101000101011000010011
The first and last digits of the bar code are always 1. Removing these leaves 25 digits. If these 25 digits are split into groups of 5 digits each, we have
10100 10100 01010 11000 01001
Next, consider each group of 5 digits. There will always be exactly two 1s in each group of digits. Each digit stands for a number. From left to right, the digits encode the values 7, 4, 2, 1, and 0. Multiply the corresponding value with the digit and compute the sum to get the final encoded digit for the zip code. The table below shows the encoding for 10100.
Zip Code Digit = 7 + 0 + 2 + 0 + 0 = 9
Repeat this for each group of 5 digits and concatenate to get the complete zip code. There is one special value. If the sum of a group of 5 digits is 11, then this represents the digit 0 (this is necessary because with two digits per group it is not possible to represent zero). The zip code for the sample bar code decodes to 99504. Although the POSTNET scheme may seem unnecessarily complex, its design allows machines to detect if errors have been made in scanning the zip code.
Write a zip code class that encodes and decodes 5-digit bar codes used by the U.S. Postal Service on envelopes. The class should have two constructors. The first constructor should input the zip code as an integer, and the second constructor should input the zip code as a bar code string consisting of 0s and 1s, as described above. Although you have two ways to input the zip code, internally, the class should store the zip code using only one format (you may choose to store it as a bar code string or as a zip code number). The class should also have at least two public member functions, one to return the zip code as an integer, and the other to return the zip code in bar code format as a string. All helper functions should be declared private. Embed your class definition in a suitable test program. Your program should print an error message if an invalid bar code is passed to the constructor.
Consider a class Movie
that contains information about a movie. The class has the following attributes:
The movie name
The MPAA rating (for example, G, PG, PG-13, R)
The number of people that have rated this movie as a 1 (Terrible)
The number of people that have rated this movie as a 2 (Bad)
The number of people that have rated this movie as a 3 (OK)
The number of people that have rated this movie as a 4 (Good)
The number of people that have rated this movie as a 5 (Great)
Implement the class with accessor and mutator functions for the movie name and MPAA rating. Write a function addRating
that takes an integer as an input parameter. The function should verify that the parameter is a number between 1 and 5, and if so, increment the number of people rating the movie that match the input parameter. For example, if 3 is the input parameter, then the number of people that rated the movie as a 3 should be incremented by 1. Write another function, getAverage
, that returns the average value for all of the movie ratings. Finally, add a constructor that allows the programmer to create the object with a specified name and MPAA rating. The number of people rating the movie should be set to 0 in the constructor.
Test the class by writing a main
function that creates at least two movie objects, adds at least five ratings for each movie, and outputs the movie name, MPAA rating, and average rating for each movie object.