Showing posts with label Chpt4. Show all posts
Showing posts with label Chpt4. Show all posts

Saturday, February 13, 2016

Exercise 4.5

Fermat's Last Theorem says that there are no integers a, b, and c such that

an+bcn

except in the case when n = 2.


Write a method named checkFermat that takes four integers as parameters—a, b, c and n—and that checks to see if Fermat’s theorem holds. If n is greater than 2 and it turns out to be true that an+bn = cn, the program should print than 2 and it turns out to be true that an+bn = cn, the program should print 

“Holy smokes, Fermat was wrong!” Otherwise the program should print “No, that doesn’t work.”


You should assume that there is a method named raiseToPow that takes two integers as arguments and that raises the first argument to the power of the second. For example:
     int x = raiseToPow(2,3);
would assign the value 8 to x, because 23 = 8.

Exercise 4.4

QUESTION

What is the output of the following program?

public class Narf {

     public static void zoop(String fred, int bob) {
          System.out.println(fred);
          if (bob == 5 ) {
               ping ("not ");
          } else {
               System.out.println("!");
          }
     }
   
     public static void main(String[] args) {
          int bizz = 5;
          int buzz = 2;
          zoop("just for", bizz);
          clink(2*buzz);
     }

     public static void clink(int fork) {
          System.out.print(It's ");
          zoop("breakfast ", fork) ;
     }

     public static void ping(String strangStrung) {
          System.out.println("any " + strangStrung + "more ");
     }
}

SOLUTION

just for
any not more
It's breakfast
!

Exercise 4.3

QUESTION

The first verse of the song “99 Bottles of Beer” is:

99 bottles of beer on the wall, 99 bottles of beer, ya’ take one down, ya’ pass it around, 98 bottles of beer on the wall.

Subsequent verses are identical except that the number of bottles gets smaller by one in each verse, until the last verse:

No bottles of beer on the wall, no bottles of beer, ya’ can’t take one down, ya’ can’t pass it around, ’cause there are no more bottles of beer on the wall!

And then the song(finally) ends.

Write a program that prints the entire lyrics of “99 Bottles of Beer.” Your program should include a recursive method that does the hard part, but you might want to write additional methods to separate the major functions of the program.

As you develop your code, test it with a small number of verses, like “3 Bottles of Beer.”
The purpose of this exercise is to take a problem and break it into smaller problems, and to solve the smaller problems by writing simple methods.

SOLUTION

public class ThinkJava4_Ex3 {
public static void BeerSong (int beer){
if (beer>0){
System.out.print(beer+" bottles of beer on the wall, ");
System.out.print(beer+" bottles of beer, ya' take one down, ya' pass it around, ");
System.out.println(beer-1+" bottles of beer on the wall.");
BeerSong(beer-1);
} else {
System.out.println("No bottles of beer on the wall, no bottles of beer, ya' can't take one down, ya' can't pass it around, 'cause there are no more bottles of beer on the wall!");
}
}
public static void main (String[]args){
BeerSong (99);
}
}

Exercise 4.2

This exercise reviews the flow of execution through a program with multiple methods. Read the following code and answer the questions below.

public class Buzz {

public static void baffle(String blimp) {
System.out.println(blimp);
zippo("ping", -5);
}

public static void zippo(String quince, int flag) {
if (flag < 0) {
System.out.println(quince + " zoop");
}
else {
System.out.println("ik");
baffle(quince);
System.out.println(
"boo-wa-ha-ha");
}
}

public static void main(String[] args) {
zippo("rattle", 13);
}
}

  1. Write the number 1 next to the first statement of this program that will be executed. Be careful to distinguish things that are statements from things that are not.
  2. Write the number 2 next to the second statement, and so on until the end of the program. If a statement is executed more than once, it might end up with more than one number next to it.
  3. What is the value of the parameter blimp when baffle gets invoked?
  4.  What is the output of this program?

SOLUTION

1.
1 System.out.println("ik");

2.
2 System.out.println(blimp);
3 System.out.println(quince + " zoop");
4 System.out.println("boo-wa-ha-ha");

3.Value of parameter blimp when baffle gets invoked is rattle.

4. 
ik
rattle
ping zoop
boo-wa-ha-ha