Hi,
Quote:
quote:Originally posted by kookoohead
It's not that I don't get the material covered in the chapter, it's the exercises at the end. I have the 1.4 edition. In particular it's the exercise dealing with mcmLength Class.
|
That's exercise 2 in my edition. First of all, Ivor Horton's exercises are never easy. I learned C++ from "Ivor Horton's Beginning C++" and the exercises in that book were extremely challenging, but you will learn a lot if you can complete them. Computer programs are usually math intensive, and Ivor's problems are usually pretty difficult. I think you have to be an A student in math to be able to complete his exercises, so take that into account, although I don't think this one is too difficult once you get the hang of it. This problem focuses on the difference between integer arithmetic and floating point arithmetic(i.e numbers with decimal points).
To begin, the problem says you need three int member variables to store the meters, centimeters, and millimeters of a measurement. So, that means you can initially define your class something like this:
Code:
class mcmLength
{
int m;
int cm;
int mm;
...
...
}
Then, lets take a look at the constructors the problem requires:
Constructors:
1)
Quote:
quote:One that accepts 3 arguments, meters, centimeters, and millimeters.
|
It doesn't say what type the arguments should be, so just make them int's so they match the 3 int member variables to simplify things. That should be an easy constructor to come up with.
2)
Quote:
quote:A constructor that accepts an integer argument representing the total number of millimeters.
|
Somehow, you have to figure out how to distribute the millimeters to the three member variables. Right away, these relationships come to my mind:
1m = 1000mm
1cm = 10mm
as well as using integer arithmetic to separate the total millimeters into meters and centimeters. Here are some suggestions:
a) Divide the constructor's integer argument, let's name it mms(for
milli
meter
s), by 1000. That will give you a whole number of meters(it could well be 0), and you can assign the result to the member variable m. To understand why that will work, take a look at this code:
int x = 15;
int y = x/4;
You might think x/4 is 3.75. However, since both x and 4 are ints, the result you get from the division will be an int, and that int is produced by truncating the .75. Since the int result is the same as the type of y, the result can be assigned to y. The same thing will happen when you divide the int mms by 1000: it will produce either 0 or a whole number of meters. Here are some examples:
1100 millimeters/1000 = 1.1 which will get truncated to 1
1900 millimeters/1000 = 1.9 which will get truncated to 1
900 millimeters/1000 = 0.9 which will get truncated to 0
b) Then, subtract m * 1000 from mms. m is the number of meters in the given number of millimeters, and there are 1000 mm per meter. So, you've used up m * 1000 of the given number of millimeters.
c) Next, divide mms by 10 and assign the result to cm--once again that will give you a whole number. There are 10 mm per cm, so if you divide the number of millimeters that are left by 10, you will get the whole number of centimeters in the remaining millimeters.
etc..
3)
Quote:
quote:A constructor that accepts a double argument representing the total number of centimeters.
|
That's essentially the same as 2), however it presents a pesky problem. If you try to do this:
double cms = 221.6234;
m = cms/100;
you will get a "loss of precision" error and your program won't compile. In this case, cms is a double and when you divide that by the int 100, you get a double result. However, you can't assign a double to the member variable m because it is an int type. To make that assignment, the compiler would have to truncate the decimal part, and that would cause a "loss of precision".
The way to get around that problem is to
cast the result of the division to an int. Casting a number to a different type just means you are going to change it to another type. When you cast a double to an int, you truncate the decimal part of the number, and the result is the same as the integer arithmetic above. You are essentially telling the compiler, "yeah, I know I am truncating the decimal part off the number, and that is the result I want, so don't give me an error." Here is an example:
double cms = 221.6234;
m = (int)(cms/100); //the member variable m is equal to 2
You would then subtract m * 100 from cms to give you the leftover cms. After that, you can handle the cm and mm member variables the same way.
4)A constructor that accepts no arguments.
In this one, you will just set the three member variables to 0.
See if that helps you get started. You really need to post specific problems you are having, i.e. some code and what is stumping you.
By the way, I abandoned Java when I got to chapter 8 of Ivor's book: Streams, Files, and Stream Output. :( I thought the Java classes for input and output were so complex that Java was too difficult for me. That was a couple of years ago. Currently, I am finishing up a much more cursory book on Java called "Java 2: A Beginner's Guide"(Herbert Schildt).