Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 23rd, 2005, 03:35 PM
Registered User
 
Join Date: Mar 2005
Location: , MD, USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 5

I'm having trouble with the exercises at the end of Chap 5. I have the solution code, but prefer to try myself before resorting to copying someone else's code and trying to understand it. Most of it does make sense after seeing the solution. I guess my main question is should I stay on this chapter until I really get it? Or is it safe for me to go on and things will become clearer as I go?
I appreciate any comments or suggestions.

Thanks

Larry
  #2 (permalink)  
Old March 28th, 2005, 01:36 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by kookoohead
 I'm having trouble with the exercises at the end of Chap 5. I have the solution code, but prefer to try myself before resorting to copying someone else's code and trying to understand it. Most of it does make sense after seeing the solution. I guess my main question is should I stay on this chapter until I really get it? Or is it safe for me to go on and things will become clearer as I go?
I appreciate any comments or suggestions.

Thanks

Larry
Hi,

I don't know what edition you have, but in the JDK 1.3 edition, chapter 5 is on Defining Classes. Since everything in Java is a class, that could be the most important chapter in the book.

  #3 (permalink)  
Old March 31st, 2005, 09:44 AM
Registered User
 
Join Date: Mar 2005
Location: , MD, USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Koo
  #4 (permalink)  
Old April 3rd, 2005, 12:48 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 millimeters), 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).
  #5 (permalink)  
Old April 4th, 2005, 09:23 AM
Registered User
 
Join Date: Mar 2005
Location: , MD, USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you 7,

I really appreciate the time and effort you put in to your response. It really helped alot. The math part was easy and I figured that out the first time I did it. The part about truncating with int's was what I was missing most. It is the rest of the requirements, "add and subtract objects", etc... that had me most confused. What objects? Ones I create myself in the test class?

So is the other book you started easier to understand than Ivor's?

Thanks again,



Koo
  #6 (permalink)  
Old April 5th, 2005, 01:47 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:It is the rest of the requirements, "add and subtract objects", etc... that had me most confused. What objects? Ones I create myself in the test class?
Yes. With all the java programs you've learned so far, you have to create a test class with main() in it. The class with main() in it is the starting point for all Java programs. For this problem, you need to create some objects from the mcmLength class inside main(). You should create them with various arguments to test out the different constructors, and then you need to be able to add two objects, subtract two objects, multiply an object by an integer, etc.

Here are some pointers:

Suppose you create two objects in your test class:

mcmLength A = new mcmLength(2, 80, 8);
mcmLength B = new mcmLength(6, 40, 7);

Now, you want to create a class method to add those objects together. To call any class method using an object, you do this:

A.add(); //A calls the class method add()

You need to consider how you are going to add B to A inside the add() method. The only way you can do that is by passing the B object to the add() method. That way, the add() method will have a reference to B. With that reference, the add() method can access the members of B and add them to the members of A. This is the way you will call the add() method:

A.add(B);

which means your function definition will start out like this:
Code:
add(mcmLength aLength) //must be a parameter that matches the type of B
{

}
Also, inside the add() method, it seems natural to create and return a third object to hold the results of adding the two objects together, so you don't alter either A or B:

mcmLength C = A.add(B)

Your function definition would then look like this:
Code:
mcmLength add(mcmLength aLength)
{



    return newLength;
}
The add() method will have direct access to the members of A since it is a class method that is being called by A(that's the way classes work), so you can refer to those values as m, cm, and mm. To access members of B, you will need to use the syntax B.m, B.cm, and B.mm. So, you might do something like this:
Code:
mcmLength add(mcmLength aLength)
{
    mcmLength newLength = new mcmLength( m + aLength.m, 
                                       cm + aLength.cm,
                                       mm + aLength.mm)

    return newLength;
}
However, if the values of the two objects you are adding are:

mcmLength A = new mcmLength(2, 80, 8);
mcmLength B = new mcmLength(6, 40, 7);

then C will have these values:

m = 6
cm = 120
mm = 15

But, notice you need to distribute 10 of the mm to cm, and 100 of the cm to mm. So, you need to figure out how to do that in your add() method. Hint: use the same integer arithmetic you used for the constructors starting with mm.

A subtract() method will be a little trickier because it will require some borrowing between member variables.

Let me know if you get stuck.

Quote:
quote:So is the other book you started easier to understand than Ivor's?
Yes, but it is about 1/3 the size of Beginning Java, and the problems at the end of the chapters are very simple, so obviously I am not getting an in depth workout with Java.
  #7 (permalink)  
Old April 5th, 2005, 02:54 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

duplicate post
  #8 (permalink)  
Old June 9th, 2005, 09:50 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's the one thing that really spoiled this book for me when I tried learning Java. The examples and exercises are heavily reliant on a high level of mathematical knowledge. Without that, I was lost ! There is also the issue of understanding exactly what the exercise is asking you to do; often it's about as clear as mud. I guess that Ivor knew what he was asking for, but the wording is never very clear.

I ended up looking at the solution quickly to see I was being asked to do, then going off with a clearer picture in my head so I knew what I was trying to achieve.

If only these issues were sorted out, it'd be a great book for everyone.

Grant
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 2 - End of chapter exercises whizzkid1892 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 30th, 2008 12:02 PM
chapter 7 - chapter 11 pelopito BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 3 January 6th, 2008 11:40 AM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM



All times are GMT -4. The time now is 11:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.