Chapter 3 Exercise Solutions - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Chapter 3 Exercise Solutions

Description:

Quest for tze Holy Grail quest for ... System.out.println (Math.abs(total)); Exercise 3.4 ... frame.pack(); frame.setVisible(true); // LabelPanel.java continued ... – PowerPoint PPT presentation

Number of Views:359
Avg rating:3.0/5.0
Slides: 13
Provided by: rafael9
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3 Exercise Solutions


1
Chapter 3 Exercise Solutions
  • Exercise 3.1 What output is produced by
    the following code fragment?
  • String m1, m2, m3
  • m1 "Quest for the Holy Grail"
  • m2 m1.toLowerCase()
  • m3 m1 " " m2
  • System.out.println (m3.replace('h', 'z'))
  • The output produced is
  • Quest for tze Holy Grail quest for tze zoly
    grail
  • The original string is concatenated with a
    lowercase version of itself, then all lowercase
    h characters are replaced with z.

2
Chapter 3 Exercise Solutions
  • Exercise 3.2Write an assignment statement that
    computes the square root of the sum of num1 and
    num2 and assigns the result to num3.
  • num3 Math.sqrt(num1 num2)
  • Exercise 3.3Write a single statement that
    computes and prints the absolute value of total.
  • System.out.println (Math.abs(total))
  • Exercise 3.4What is the effect of the following
    import statement?
  • import java.awt.
  • This statement allows all the classes in the
    java.awt package to be referenced in a program
    without the explicit package name.

3
Chapter 3 Exercise Solutions
  • Exercise 3.5Assuming that a Random object has
    been created called generator, what is the range
    of the result of each of the following
    expressions?
  • generator.nextInt(20)
  • 0 to 19, inclusive
  • generator.nextInt(8) 1
  • 1 to 8, incluslive
  • generator.nextInt(45) 10
  • 10 to 54, inclusive
  • generator.nextInt(100) 50
  • -50 to 49, inclusive

4
Chapter 3 Exercise Solutions
  • Exercise 3.6Write code to declare and
    instantiate an object of the Random class (call
    the object reference variable rand). Then write a
    list of expressions using the nextInt method that
    generate random numbers in the following
    specified ranges, including the endpoints. Use
    the version of the nextInt method that accepts a
    single integer parameter.
  • Random rand new Random()
  • 0 to 10
  • rand.nextInt(11)
  • 0 to 500
  • rand.nextInt(501)
  • 1 to 10
  • rand.nextInt(10) 1

1 to 500 rand.nextInt(500) 1 25 to
50 rand.nextInt(26) 25 10 to
15 rand.nextInt(26) 10
5
Chapter 3 Exercise Solutions
  • Exercise 3.7Write code statements to create a
    DecimalFormat object that will round a formatted
    value to 4 decimal places. Then write a statement
    that uses that object to print the value of
    result, properly formatted.
  • DecimalFormat fmt new DecimalFormat
    (0.)
  • System.out.println (fmt.format(result))

6
Chapter 3 Programming Project Solutions
  • 3.3 Write an application that reads the lengths
    a,b,c of the sides of a triangle from the user.
    Compute the area of the triangle using Herons
    formula where s is one-half the perimeter of
    the triangle, i.e., s ½ (abc). Print the
    area to three decimal places.

7
Chapter 3 Project Solutions Area of a Triangle
  • import java.util.Scanner
  • import java.text.DecimalFormat
  • public class TriangleArea
  • //----------------------------------------------
    -------------------------------// Computes the
    area of a triangle given the length of each//
    side. Uses Heron's formula.//--------------------
    --------------------------------------------------
    -------
  • public static void main (String args)
  • double side1, side2, side3, temp, temp2,
    area
  • Scanner sc new Scanner(System.in)
  • System.out.print ("Enter the length of side
    1 ")
  • side1 Double.parseDouble(sc.nextLine())
  • System.out.print ("Enter the length of side
    2 ")
  • side2 sc.nextDouble()
  • System.out.print ("Enter the length of side
    3 ")
  • side3 Double.parseDouble(sc.nextLine())
  • // Continued on next slide

8
Chapter 3 Project Solutions Area of a Triangle
  • temp (side1 side2 side3) / 2 //
    compute s
  • temp2 temp (temp-side1) (temp-side2)
    (temp-side3)
  • area Math.sqrt (temp2)
  • DecimalFormat fmt new DecimalFormat
    ("0.")
  • System.out.println ("Area "
    fmt.format(area))

// TriangleArea.java continued
9
Chapter 3 Programming Project Solutions
  • 3.5 Modify the LabelDemo program so that it
    displays a fourth label, with the text of the
    label centered above the image.
  • import java.awt.
  • import javax.swing.
  • public class LabelDemo
  • //---------------------------------------------
    --------------------
  • // Creates and displays the primary
    application frame.
  • //---------------------------------------------
    --------------------
  • public static void main (String args)
  • JFrame frame new JFrame ("Label Demo")
  • frame.setDefaultCloseOperation
    (JFrame.EXIT_ON_CLOSE)
  • ImageIcon icon new ImageIcon
    ("devil.gif")
  • JLabel label1, label2, label3, label4

10
Chapter 3 Project Solutions Add a Devil
  • //---------------------------------------------
    -----------------------------
  • // Display four labels with different
    text/icon orientations.
  • //---------------------------------------------
    -----------------------------
  • label1 new JLabel ("Devil Left", icon,
    SwingConstants.CENTER)
  • label2 new JLabel ("Devil Right", icon,
    SwingConstants.CENTER)
  • label2.setHorizontalTextPosition
    (SwingConstants.LEFT)
  • label2.setVerticalTextPosition
    (SwingConstants.CENTER)
  • label3 new JLabel ("Devil Above", icon,
    SwingConstants.CENTER)
  • label3.setHorizontalTextPosition
    (SwingConstants.CENTER)
  • label3.setVerticalTextPosition
    (SwingConstants.BOTTOM)
  • label4 new JLabel ("Devil Below", icon,
    SwingConstants.LEFT)
  • label4.setHorizontalTextPosition
    (SwingConstants.CENTER)
  • label4.setVerticalTextPosition
    (SwingConstants.TOP)

// LabelPanel.java continued
11
Chapter 3 Project Solutions Add a Devil
  • JPanel panel new JPanel()
  • panel.setBackground (Color.cyan)
  • panel.setPreferredSize (new Dimension (200,
    250))
  • panel.add (label1)
  • panel.add (label2)
  • panel.add (label3)
  • panel.add (label4)
  • frame.getContentPane().add(panel)
  • frame.pack()
  • frame.setVisible(true)

// LabelPanel.java continued
12
Output of Modified LabelDemo Application
Write a Comment
User Comments (0)
About PowerShow.com