Some notes on the Problems from Chapter 1

 

Problem 2 on page 44

 

   You are to write an actual Java program and get it to work. For those of you who are still shaky on general programming concepts, this is the problem you should do!

 

   The problem is to read in a bunch of character pairs, each representing a card from a standard deck of cards. For example “2C” means the two of clubs, “TD” means the ten of diamonds. You do not have to use any fancy GUI techniques to read these in. I will be happy if you can do it with just a string initialized at the start of your program. For example:

 

    String bridgeHand =”2C QD TC AD 6C 3D TD 3H 5H 7H AS JH KH”;

 

But you will have to have a way of separating the input into character pairs. To do this you should use the StringTokenizer class in Java. To learn how to do this, see page 288 in your old Java textbook.

 

Once you have a character pair, I would separate them and place them into arrays, for example,

 

    char [] clubArray = new char[];

 

When you are done reading the data, the clubArray should have a 2, 10 and 6 in it, etc.

 

Then sort the arrays from highest to lowest. If you use integers then set a J =11, Q=12, etc.

 

Now you are in a good position to evaluate the array based on the rules in your textbook and also to print the cards as is shown in your textbook.

 

 

Problem 3 on page 45

 

   Here you do not have to write a Java program that works but you do have to write a Java class with several methods. Your job is to be able to handle extremely large numbers. Suppose that someone wanted to store a number that had 1000 digits in it, e.g. 6134631272137………. There is no integer data type in Java that is big enough to hold this kind of number.

 

   You are to create a class called BigInt that can handle these kinds of numbers. Each big integer will be represented by a string of characters. For example:

          “606098698698762185164402928351259081”

 

The one big problem in this whole exercise is that the string can contain leading zeroes. For example:

          “000000000000000000000008764876400000000”

 

Your class has to have 4 methods:

a.      public BigInt(String val) to initialize a data element

b.     public void display() to display a string (without leading zeroes)

c.     public BigInt add(BigInt val) which adds the BigInt value in the parameter to the current object.

d.     Public BigInt multiply(BigInt val) which multiplies the BigInt value in the parameter to the current object.

 

Notes on adding numbers:

1.     You have to start with the smallest digit – the ones at the ends of the two strings.

2.     Convert them to integers.

3.     Add them.

4.     If the result is ten or more – remember that you have to carry the one

5.     Put the result in the “end” position of a third string.

6.     Continue until all digits have been added.

This is not as easy as it may seem.

 

Notes on multiplying numbers:

1.     Remember the way you did long multiplication

2.     You will have to multiply each digit in one string with all of the digits in the other string

3.     Remember again to “carry” things to the next digit

4.     You can use the “add” method that you wrote above to help you.

5.     Let’s look at an example:

 

123 * 456

 

                 123

                 456

           ---------

                 738

                              6150

                            49200

                         ----------

                            56088

 

Multiply the 6 by the 123 and store the answer in a temporary BinInt variable.

Multiply the 5 by the 123, then multiply it by 10 and then add it to the temporary variable.

Multiply the 4 by the 123, then multiply it by 100 and then add it to the temporary variable.

Return the temporary variable as the answer.