1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

a0programming and problem split 1 9425

7 1 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Nội dung

Purchase this book NEW and receive FREE! The Student Resource Disk for Java TE AM FL Y ã Borlandđ JBuilder Personal • Sun Java™ SDK Standard Edition • Sun Java™ SDK Standard Edition Documentation • Program Files Team-Fly® JONES AND BARTLETT COMPUTER SCIENCE World Headquarters Jones and Bartlett Publishers 40 Tall Pine Drive Sudbury, MA 01776 978-443-5000 info@jbpub.com www.jbpub.com Jones and Bartlett Publishers Canada 2406 Nikanna Road Mississauga, ON L5C 2W6 CANADA Jones and Bartlett Publishers International Barb House, Barb Mews London W6 7PA UK Copyright © 2003 by Jones and Bartlett Publishers, Inc Cover image © Peter J Robinson/Photolibrary/PictureQuest Text photo credits follow the index, which constitutes a continuation of the copyright page Unless otherwise acknowledged, all photographs are the property of Jones and Bartlett Publishers Library of Congress Cataloging-in-Publication Data Dale, Nell B Programming and problem solving with Java / Nell Dale, Chip Weems, Mark Headington p cm Includes index ISBN 0-7637-0490-3 Java (Computer program language) I Weems, Chip II Headington, Mark R III Title QA76.73.J38D346 2003 005.13’3 dc21 2002043476 All rights reserved No part of the material protected by this copyright notice may be reproduced or utilized in any form, electronic or mechanical, including photocopying, recording, or any information storage or retrieval system, without written permission from the copyright owner Production Credits Chief Executive Officer: Clayton Jones Chief Operating Officer: Don W Jones, Jr Executive V.P and Publisher: Robert W Holland, Jr V.P., Design and Production: Anne Spencer V.P., Manufacturing and Inventory Control: Therese Bräuer V.P., Sales and Marketing: William Kane Editor-in-Chief, College: J Michael Stranz Production Manager: Amy Rose Senior Marketing Manager: Nathan Schultz Associate Production Editor: Karen C Ferreira Associate Editor: Theresa DiDonato Production Assistant: Jenny McIsaac Cover Design: Kristin Ohlin Composition: Northeast Compositors Illustrations and Technical Art: Smolinski Studios Copyediting: Jill Hobbs Proofreading: Trillium Project Management Text Design: Anne Spencer Printing and Binding: Courier Kendallville Cover Printing: Lehigh Press This book was typeset in QuarkXPress 4.1 on a Macintosh G4 The font families used were Caecilia, Myria, and Letter Gothic The first printing was printed on 45# Utopia GW Matte Printed in the United States of America 07 06 05 04 03 10 This book is dedicated to you, and to all of our other students for whom it was begun and without whom it would never have been completed To quote Mephistopheles, one of the chief devils, and tempter of Faust, …My friend, I shall be pedagogic, And say you ought to start with Logic… …Days will be spent to let you know That what you once did at one blow, Like eating and drinking so easy and free, Can only be done with One, Two, Three Yet the web of thought has no such creases And is more like a weaver’s masterpieces; One step, a thousand threads arise, Hither and thither shoots each shuttle, The threads flow on, unseen and subtle, Each blow effects a thousand ties The philosopher comes with analysis And proves it had to be like this; The first was so, the second so, And hence the third and fourth was so, And were not the first and second here, Then the third and fourth could never appear That is what all the students believe, But they have never learned to weave J W von Goethe, Faust, Walter Kaufman trans., New York, Anchor/Doubleday: 1963 As you study this book, not let the logic of algorithms bind your imagination, but rather make it your tool for weaving masterpieces of thought 418 CASE STUDY static JButton subtract; static JButton clear; // Subtract button // Clear button // Define event listener for numeric buttons static class NumericHandler implements ActionListener { public void actionPerformed(ActionEvent event) // Handle events from the buttons in calcPane { double secondOperand; // Holds input value String whichButton; // Holds the button's name // Get the operand secondOperand = Double.parseDouble(inputField.getText()); whichButton = event.getActionCommand(); // Get the button's name if (whichButton.equals("+")) result = result + secondOperand; else result = result – secondOperand; // When the name is "add" // add the operand // Otherwise // subtract the operand register.setText("" + result); inputField.setText(""); // Display result // Clear input } } static class ClearHandler implements ActionListener { public void actionPerformed(ActionEvent event) // Handle events from the Clear button in calcPane { // Set result back to zero result = 0.0; register.setText("0.0"); // Reset result in register inputField.setText(""); // Clear input } } public static void main(String[] args) { operation = new NumericHandler(); clearOperation = new ClearHandler(); result = 0.0; // Instantiate a NumericHandler // Instantiate a ClearHandler // Initialize result // Instantiate labels and initialize input field resultLabel = new JLabel("Result:"); register = new JLabel("0.0", JLabel.RIGHT); entryLabel = new JLabel("Enter #:"); inputField = new JTextField("", 10); CASE STUDY // Instantiate button objects add = new JButton("+"); subtract = new JButton("-"); clear = new JButton("Clear"); // Register the button listeners add.addActionListener(operation); subtract.addActionListener(operation); clear.addActionListener(clearOperation); // Add interface elements to calcFrame calcFrame = new JFrame(); // Give the frame a value calcFrame.setSize(300, 200); // Specify size of frame // Set close operation to exit calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); calcPane = calcFrame.getContentPane(); // Get the content pane calcPane.setLayout(new GridLayout(4,2)); // Set the layout manager calcPane.add(resultLabel); calcPane.add(register); calcPane.add(entryLabel); calcPane.add(inputField); calcPane.add(add); calcPane.add(subtract); calcPane.add(clear); // Show the frame calcFrame.setVisible(true); } } When we run the application, it displays a window like the one here: 419 ... unseen and subtle, Each blow effects a thousand ties The philosopher comes with analysis And proves it had to be like this; The first was so, the second so, And hence the third and fourth was so, And. .. Jones, Jr Executive V.P and Publisher: Robert W Holland, Jr V.P., Design and Production: Anne Spencer V.P., Manufacturing and Inventory Control: Therese Bräuer V.P., Sales and Marketing: William... new NumericHandler(); clearOperation = new ClearHandler(); result = 0.0; // Instantiate a NumericHandler // Instantiate a ClearHandler // Initialize result // Instantiate labels and initialize

Ngày đăng: 04/12/2022, 15:05