Chapter 4 Exercises


Site hosted by Angelfire.com: Build your free website today!
4.1
	1. Translate the following statements to equivalent 
	 statements that use extended assignment operators: 
		a. x = x*2;  x *= 2;
		b. y = y%2;  y %= 2;
	2. Translate the following statements to equivalent 
	 statements that do not use extended assignment operators: 
		a. x += 5;  x=x+5;
		b. x *= x;  x= x*x;


4.2
	1. Assume that x has the value 3.6 and y has the value 4.  
	 State the value of the variable z after the following statements: 
		a. z = Math.sqrt(y);  2.0
		b. z = Math.round(x);  4
		c. z = Math.pow(y, 3);  46.656
		d. z = Math.round(Math.sqrt(x));  2
	2. Write code segments to print out the following values in a terminal window: 
		a. A random integer between 1 and 20, inclusive.  generator.nextInt(20)+1;
		b. A random double between 1 and 10, inclusive.  
		 generator.nextDouble()+1+generator.nextInt(9);

4.5
	1. What type of expression must the condition of an if statement contain? Boolean.  
	2.  Describe the role of the curly braces ({}) in an if statement.  They allow multiple statements
	 for a given if statement.  
	3.  What is the difference between an if statement and an if-else statement?  if-else must follow
	 an if statement and requires an else after it.  
	4. Assume that x is 5 and y is 10.  Write the values of the following expressions: 
		a. x<=10		True
	 	b. x-2!=0		True
		c. x>y		False
	5. Given the following mini-specifications, write expressoins involving relational operations: 
		a. Determine if an input value x is greater than 0.  (x>0)
		b. Determine if a given number of secods equals a minute.  (x==60)
		c. If a, b, and c are the lengths of the sides of a triangle and c is the largest side, 
		 determine if the triangle is a right triangle.  (a*a+b*b==c*c)
	6. Write the outputs of the following code segments: 
		a. 
			int x=20, y=15, z;
			if (xb)
				System.out.println(a);
			else if
				System.out.println(b);
			else;
		b. Prompt the user for two whole numbers and input them.  Then print
		 the numbers in numerical order.  
			int a, b;
			a=reader.readInt("Enter a whole number: ");
			b=reader.readInt("Enter another whole number: ");
			if (b>a)
				System.out.println(b+" "+a);
			else
				System.out.println(a+" "+b);

4.6
	1. When does a while loop terminate execution?  When the condition
	 evaluates to False.  
	2. List the four components of a while loop.  Loop test, opening bracket, statements, 
	 closing bracket.  
	3. What happens if the condition of a while loop is false from the outset?  The statements are
	 never read.  
	4. Describe in English what the following code segments do.  
		a. This is an endless loop where this is printed forever: 1 2 1 2 1 2 . . .
		b. This prompts the user for a positive number and multiplies them until -999 is entered.
	5. Write code segments to perform the following tasks:
		a.
		for (int x=1;x<=10;x++)
		{
			System.out.println(Math.pow(x,2));
			System.out.println(Math.pow(x,3));
		}
		b.
		for (int x=1;x<=10;x++)
		{
			System.out.println(generator.next(10)+1);
		}
		c.
		do
		{
		age=reader.readLine("Enter an age (100 to end): ");
		}while(age!=100);

4.7
	1. Describe in English what the following code segments do:
		a. It prints out 2 to the expo power preceeded by that same number until expo is greater
	  	 than limit.  
		b. two is multiplied by itself the number of times equal to the value of expo-1.  
	2. 
		a. 
		for (int x=1;x<=10;x++)
		{
			System.out.println(Math.pow(x,2));
			System.out.println(Math.pow(x,3));
		}
		b. 
		String word="";
		for (int x=10;x>=1;x--)
		{
			word+=x;
		}
	3. 
		a. 
		Pen StandardPen=new StandardPen();
		int i=1;
		while(i<=100)
		{
			pen.turn(3.6);
			pen.move(1.6);
			i++;
		}
		
		b.
		int base=2, count=expo;
		while(count>1)
		{
			count--;
			base=base*base;
		}


4.8
	1. 
		a. This prints out the even number between 1 and limit.  
		b. This generates a random number from 0 to 9, and loops until the user enters 
		  that number
	2. Write code segments that use loops to perform the following tasks: 
		a. 
		for (int x=1;x<20;x+=2)
			System.out.println(Math.pow(x,2)+" "+Math.pow(x,3));
		
		b. 
		String word="";
		for (int x=18;x>=0;x-=2)
			word+=x;
	
4.9
	1. Describe the logic errors in the following loops:
		a. This will not include limit because < should be <=.
		b. This will only print 5 odd numbers.  (number !=10)  should be  (number!=20).

Home