Exercises

Included below are short-answer and programming exercises. Answers are provided for those exercises whose exercise number is a hyperlink. Because college faculty use these exercises in their exams, we have provided answers to roughly half of the exercises included here.


3.4 Fill in the blanks in each of the following:

  1. Data type __________ declares a single-precision floating-point variable.
  2. If class Double provides method parseDouble to convert a String to a double and class Integer provides method parseInt to convert a String to an int, then class Float probably provides method _____________ to convert a String to a float.
  3. Data type ____________ declares a double-precision floating-point variable.
  4. The _____________ or a browser can be used to execute a Java applet.
  5. To load an applet into a browser you must first define an _______________ file.
  6. The _______________ and ___________________ tags specify that an applet should be loaded and executed.

3.5 State whether each of the following is true or false. If false, explain why.

  1. All browsers support Java 2.
  2. When using an import of the form javax.swing.*, all classes in the package are imported.
  3. You do not need import statements if the full package name and class name are specified each time you refer to a class in a program.

3.6 Write an applet that asks the user to enter two floating-point numbers, obtains the two numbers from the user and draws the sum, product, difference and quotient of the two numbers. Use the techniques shown in Fig. 3.12.

3.7 Write an applet that asks the user to enter two floating-point numbers, obtains the numbers from the user and displays the larger number followed by the words "is larger" as a string on the applet. If the numbers are equal, print the message "These numbers are equal." Use the techniques shown in Fig. 3.12.

3.8 Write an applet that inputs three floating-point numbers from the user and displays the sum, average, product, smallest and largest of these numbers as strings on the applet. Use the techniques shown in Fig. 3.12.

3.9 Write an applet that inputs from the user the radius of a circle as a floating-point number and draws the circle’s diameter, circumference and area. Use the constant value 3.14159 for p. Use the techniques shown in Fig. 3.12. [Note: You may also use the predefined constant Math.PI for the value of p. This constant is more precise than the value 3.14159. Class Math is defined in the java.lang package, so you do not need to import it.] Use the following formulas (r is the radius): diameter = 2r, circumference = 2pr, area = pr2.

3.10 Write an applet that draws a box, an oval, an arrow and a diamond using asterisks (*) as follows:
*********          ***              *              *
*       *        *     *           ***            * *
*       *       *       *         *****          *   *
*       *       *       *           *           *     *
*       *       *       *           *          *       *
*       *       *       *           *           *     *
*       *       *       *           *            *   *
*       *        *     *            *             * *
*********          ***              *              *

3.11 Write an applet that reads five integers and determines and prints the largest and smallest integers in the group. Use only the programming techniques you learned in this chapter and Chapter 2. Draw the results on the applet.

3.12 Write an applet that reads in two floating-point numbers and determines and prints if the first is a multiple of the second. (Hint: Use the modulus operator.) Use only the programming techniques you learned in this chapter and Chapter 2. Draw the results on the applet.

3.13 Write an applet that draws a checkerboard pattern as follows:

 
* * * * * * * *
 * * * * * * * *
* * * * * * * *
 * * * * * * * *
* * * * * * * *
 * * * * * * * *
* * * * * * * *
 * * * * * * * *

3.14 Write an applet that draws a variety of rectangles of different sizes and locations.

3.15 Write an applet that allows the user to input the four arguments required by method drawRect, then draws a rectangle using the four input values.

3.16 The Graphics class contains a drawOval method that takes the exact same four arguments as the drawRect method. However, the arguments for the drawOval method specify the "bounding box" for the oval. The sides of the bounding box are the boundaries of the oval. Write a Java applet that draws an oval and a rectangle with the same four arguments. You will see that the oval touches the rectangle at the center of each side.

3.17 Modify the solution to Exercise 3.16 to output a variety of ovals of different shapes and sizes.

3.18 Write an applet that allows the user to input the four arguments required by method drawOval, then draws an oval using the four input values.

3.19 What does the following code print?

g.drawString( "*", 25, 25 );
g.drawString( "***", 25, 55 );
g.drawString( "*****", 25, 85 );
g.drawString( "****", 25, 70 );
g.drawString( "**", 25, 40 );

3.20 Using only the programming techniques you learned in Chapters 2 and 3, write an applet that calculates the squares and cubes of the numbers from 0 to 10 and draws the resulting values in table format as follows:

number square cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000

 [Note: This program does not require any input from the user.]

3.21 Write an applet that reads a first name and a last name from the user as two separate inputs and concatenates the first name and last name separated by a space. Draw a string with the concatenated name.

3.22 Write an applet that inputs five floating-point numbers and determines and prints the number of negative numbers input, number of positive numbers input and the number of zeros input. Use only the programming techniques you learned in this chapter and Chapter 2. Draw the results on the applet.

 


Selected Answers

Included below are answers to approximately half the of the exercises in the Cyber Classroom. We are not able to include answers to every exercise because college faculty use these exercises in their classroom exams.


3.4 Fill in the blanks in each of the following:

  1. Data type declares a single-precision floating-point variable.
    ANS:  float.
  2. If class Double provides method parseDouble to convert a String to a double and class Integer provides method parseInt to convert a String to an int, then class Float probably provides method to convert a String to a float.
    ANS: parseFloat.
  3. Data type declares a double-precision floating-point variable.
    ANS: double.
  4. The or a browser can be used to execute a Java applet.
    ANS: appletviewer.
  5. To load an applet into a browser you must first define an file.
    ANS: HTML file.
  6. The and tags specify that an applet should be loaded and executed.
    ANS: <applet>, </applet>.

3.7

// Exercise 3.07 Solution
// Larger.java
// Program determines the larger of two numbers
import javax.swing.*;
import java.awt.Graphics;

public class Larger extends JApplet {
   String result;            // a string containing the output

   public void init()
   {
      String firstNumber,   // first string entered by user
             secondNumber;  // second string entered by user
           
      int number1,          // first number to compare
          number2;          // second number to compare

      // read first number from user as a string
      firstNumber =
         JOptionPane.showInputDialog( "Enter first integer:" );

      // read second number from user as a string
      secondNumber =
         JOptionPane.showInputDialog( "Enter second integer:" );          

      // convert numbers from type String to type int
      number1 = Integer.parseInt( firstNumber );
      number2 = Integer.parseInt( secondNumber );

      if ( number1 > number2 )
         result = number1 + " is larger.";
      else if ( number1 < number2 )
         result = number2 + " is larger.";
      else
         result = "These numbers are equal."; 
   }

   public void paint( Graphics g )
   {
      // draw results
      g.drawRect( 15, 10, 270, 20 );
      g.drawString( result, 25, 25 );
   }   
}

3.9

// Exercise 3.9 Solution
// Circle.java
// Program calculate the area, circumference, and diameter for a circle
import java.awt.Graphics;
import javax.swing.*;

public class Circle extends JApplet {
   String line1,        // strings for output
          line2,
          line3;

   public void init()
   {
      String input;       // string entered by user
      int radius;         // radius of circle

      // read from user as a string
      input =
         JOptionPane.showInputDialog( "Enter radius:" );

      // convert number from type String to type int
      radius = Integer.parseInt( input );
 
      line1 = "Diameter is " + ( 2 * radius );
      line2 = "Area is " + ( Math.PI * radius * radius );
      line3 = "Circumference is " + ( 2 * Math.PI * radius );
   }

   public void paint( Graphics g )
   {
      g.drawString( line1, 25, 30 );
      g.drawString( line2, 25, 45 );
      g.drawString( line3, 25, 60 );
   }
}

3.12

// Exercise 3.12 Solution
// OddEven.java
// Program determines if a number is odd or even
import javax.swing.*;
import java.awt.Graphics;

public class OddEven extends JApplet {
   String result;      // output display string

   public void init()
   {
      String input;       // string entered by user
      int number;         // number

      // read from user as a string
      input =
         JOptionPane.showInputDialog( "Enter integer:" );

      // convert number from type String to type int
      number = Integer.parseInt( input );

      if ( number % 2 == 0 )
         result = "Number is even.";
      else
         result = "Number is odd.";
   }
   
   public void paint( Graphics g )
   {
      g.drawRect( 15, 10, 270, 20 );
      g.drawString( result, 25, 25 );
   }
}

3.13

// Exercise 3.13 Solution
// Checker.java
// Program draws a checkerboard
import javax.swing.*;
import java.awt.Graphics;

public class Checker extends JApplet {
   public void paint( Graphics g )
   {
      g.drawString( "* * * * * * * *", 25, 20 );
      g.drawString( " * * * * * * * *", 25, 30 );
      g.drawString( "* * * * * * * *", 25, 40 );
      g.drawString( " * * * * * * * *", 25, 50 );
      g.drawString( "* * * * * * * *", 25, 60 );
      g.drawString( " * * * * * * * *", 25, 70 );
      g.drawString( "* * * * * * * *", 25, 80 );
      g.drawString( " * * * * * * * *", 25, 90 );
   }
}

3.16

<html>
<applet code="Draw3.class" width=275 height=70>
</applet>
</html>

3.19

// Exercise 3.19 Solution
// Mystery.java
// What does this print?
import javax.swing.*;
import java.awt.Graphics;

public class Mystery extends JApplet {
   public void paint( Graphics g )
   {
      g.drawString( "*", 25, 25 );
      g.drawString( "***", 25, 55 );
      g.drawString( "*****", 25, 85 );
      g.drawString( "****", 25, 70 );
      g.drawString( "**", 25, 40 );
   }
}