Archive for December, 2007

604 Graphics (Web hosting ratings) and Java2D Chapter 11 +x (0,

Monday, December 17th, 2007

604 Graphics and Java2D Chapter 11 +x (0, 0) X axis Y axis (x, y) +y Fig. 11.2Java coordinate system. Units are measured in pixels. 11. Portability Tip 11.1 Different display monitors have different resolutions (i.e., the density of pixels varies). This can cause graphics to appear to be different sizes on different monitors. 11.2 Graphics Contexts and Graphics Objects A Java graphics context enables drawing on the screen. A Graphics object manages a graphics context by controlling how information is drawn. Graphics objects contain methods for drawing, font manipulation, color manipulation and the like. Every applet we have seen in the text that performs drawing on the screen has used the Graphicsobject g(the argument to the applet s paintmethod) to manage the applet s graphics context. In this chapter, we demonstrate drawing in applications. However, every technique shown here can be used in applets. The Graphics class is an abstract class (i.e., Graphics objects cannot be instantiated). This contributes to Java s portability. Because drawing is performed differently on each platform that supports Java, there cannot be one class that implements drawing capabilities on all systems. For example, the graphics capabilities that enable a PC running Microsoft Windows to draw a rectangle are different from the graphics capabilities that enable a UNIX workstation to draw a rectangle and those are both different from the graphics capabilities that enable a Macintosh to draw a rectangle. When Java is implemented on each platform, a derived class of Graphicsis created that actually implements all the drawing capabilities. This implementation is hidden from us by the Graphics class, which supplies the interface that enables us to write programs that use graphics in a platform-independent manner. Class Component is the superclass for many of the classes in the java.awt package (we discuss class Component in Chapter 12). Component method paint takes a Graphicsobject as an argument. This object is passed to the paintmethod by the system when a paint operation is required for a Component. The header for the paintmethod is public void paint( Graphics g ) The Graphicsobject greceives a reference to an object of the system s derived Graphics class. The preceding method header should look familiar to you it is the same one Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Make web site - Chapter 11 Graphics and Java2D 603 Key java.lang.Object

Saturday, December 15th, 2007

Chapter 11 Graphics and Java2D 603 Key java.lang.Object java.awt.Color class java.awt.Component interface java.awt.Font java.awt.FontMetrics java.awt.Graphics java.awt.Polygon Classes and interfaces from the Java2D API java.awt.Graphics2D java.awt.Paint java.awt.GradientPaint java.awt.TexturePaint java.awt.BasicStroke java.awt.geom.GeneralPath java.awt.Shape java.awt.Stroke java.awt.geom.Line2D java.awt.geom.RectangularShape java.awt.geom.Arc2D java.awt.geom.Ellipse2D java.awt.geom.Rectangle2D java.awt.geom.RoundRectangle2D Fig. 11.1Some classes and interfaces used in this chapter from Java s original 11. graphics capabilities and from the Java2D API. Software Engineering Observation 11.1 The upper-left coordinate (0, 0) of a window is actually behind the title bar of the window. For this reason, drawing coordinates should be adjusted to draw inside the borders of the window. Class Container(a superclass of all windows in Java) has method getInsets, which returns an Insetsobject (package java.awt) for this purpose. An Insetsobject has four publicmembers top, bottom, leftand right that represent the number of pixels from each edge of the window to the drawing area for the window. Text and shapes are displayed on the screen by specifying coordinates. Coordinate units are measured in pixels. A pixel is a display monitor s smallest unit of resolution. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

602 Graphics and Java2D Chapter 11 Outline 11.1 (Yahoo free web hosting)

Friday, December 14th, 2007

602 Graphics and Java2D Chapter 11 Outline 11.1 Introduction 11.2 Graphics Contexts and Graphics Objects 11.3 Color Control 11.4 Font Control 11.5 Drawing Lines, Rectangles and Ovals 11.6 Drawing Arcs 11.7 Drawing Polygons and Polylines 11.8 The Java2D API 11.9 Java2D Shapes 11.10 (Optional Case Study) Thinking About Objects: Designing Interfaces with the UML Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises 11.1 Introduction In this chapter, we overview several of Java s capabilities for drawing two-dimensional shapes, controlling colors and controlling fonts. One of Java s initial appeals was its support for graphics that enabled Java programmers to visually enhance their applets and applications. Java now contains many more sophisticated drawing capabilities as part of the Java2D API. This chapter begins with an introduction to many of the original drawing capabilities of Java. Next, we present several of the new and more powerful Java2D capabilities, such as controlling the style of lines used to draw shapes and controlling how shapes are filled with color and patterns. Figure 11.1 shows a portion of the Java class hierarchy that includes several of the basic graphics classes and Java2D API classes and interfaces covered in this chapter. Class Color contains methods and constants for manipulating colors. Class Font contains methods and constants for manipulating fonts. Class FontMetrics contains methods for obtaining font information. Class Polygoncontains methods for creating polygons. Class Graphics contains methods for drawing strings, lines, rectangles and other shapes. The bottom half of the figure lists several classes and interfaces from the Java2D API. Class BasicStroke helps specify the drawing characteristics of lines. Classes Gradient- Paint and TexturePaint help specify the characteristics for filling shapes with colors or patterns. Classes GeneralPath, Arc2D, Ellipse2D, Line2D, Rectangle2D and RoundRectangle2D define a variety of Java2D shapes. To begin drawing in Java, we must first understand Java s coordinate system (Figure 11.2), which is a scheme for identifying every possible point on the screen. By default, the upper-left corner of a GUI component (such as an applet or a window) has the coordinates (0, 0). A coordinate pair is composed of an x-coordinate (the horizontal coordinate) and a y-coordinate (the vertical coordinate). The x-coordinate is the horizontal distance moving right from the upper-left corner. The y-coordinate is the vertical distance moving down from the upper-left corner. The x-axis describes every horizontal coordinate, and the y-axis describes every vertical coordinate. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

11 Graphics (Business web site) and Java2D Objectives To understand

Friday, December 14th, 2007

11 Graphics and Java2D Objectives To understand graphics contexts and graphics objects. To understand and be able to manipulate colors. To understand and be able to manipulate fonts. To use Graphics methods to draw lines, rectangles, rectangles with rounded corners, three-dimensional rectangles, ovals, arcs and polygons. To use methods of class Graphics2D from the Java2D API to draw lines, rectangles, rectangles with rounded corners, ellipses, arcs and general paths. To be able to specify Paintand Stroke characteristics of shapes displayed with Graphics2D. One picture is worth ten thousand words. Chinese proverb Treat nature in terms of the cylinder, the sphere, the cone, all in perspective. Paul Cezanne Nothing ever becomes real till it is experienced even a proverb is no proverb to you till your life has illustrated it. John Keats A picture shows me at a glance what it takes dozens of pages of a book to expound. Ivan Sergeyevich Turgenev Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web site design - 600 Strings and Characters Chapter 10 Design and

Thursday, December 13th, 2007

600 Strings and Characters Chapter 10 Design and implement a spell-checker application in Java. Your program should maintain an array wordListof strings. Enable the user to enter these strings. [Note: In Chapter 17, we introduce file processing. Once you have this capability, you can obtain the words for the spell checker from a computerized dictionary stored in a file.] Your program should ask a user to enter a word. The program should then look up that word in the wordList array. If the word is present in the array, your program should print Word is spelledcorrectly. If the word is not present in the array, your program should print word is not spelled correctly. Then your program should try to locate other words in wordList that might be the word the user intended to type. For example, you can try all possible single transpositions of adjacent letters to discover that the word default is a direct match to a word in wordList. Of course, this implies that your program will check all other single transpositions, such as edfault, dfeault, deafult, defalut, and defautl. When you find a new word that matches one in wordList, print that word in a message, such as Didyoumean”default?”. Implement other tests, such as replacing each double letter with a single letter and any other tests you can develop to improve the value of your spell-checker. 10.30 (Project: A Crossword Puzzle Generator) Most people have worked a crossword puzzle, but few have ever attempted to generate one. Generating a crossword puzzle is suggested here as a string- manipulation project requiring substantial sophistication and effort. There are many issues the programmer must resolve to get even the simplest crossword puzzle- generator program working. For example, how does one represent the grid of a crossword puzzle inside the computer? Should one use a series of strings, or should double-subscripted arrays be used? The programmer needs a source of words (i.e., a computerized dictionary) that can be directly referenced by the program. In what form should these words be stored to facilitate the complex manipulations required by the program? The really ambitious reader will want to generate the clues portion of the puzzle, in which the brief hints for each across word and each down word are printed for the puzzle worker. Merely printing a version of the blank puzzle itself is not a simple problem. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 10 Strings and Characters 599 Character Code (Unlimited web hosting)

Wednesday, December 12th, 2007

Chapter 10 Strings and Characters 599 Character Code Character Code A .T - B -… U .. C -.-. V … D -.. W .- E . X -.. F ..-. Y -.- G –. Z –.. H …. I .. Digits J .–1 .— K -.2 ..– L .-.. 3 …- M 4 …. N -. 5 ….. O 6 -…. P 7 –… Q –.8 —.. R .-. 9 —-. S … 0 Fig. 10.31The letters of the alphabet as expressed in international Morse code . 10.31 Your program should recognize invalid conversions. For example, the question “How many feet in 5 kilograms?” is not a meaningful question because “feet”is a unit of length while “kilograms”is a unit of mass. SPECIAL SECTION: CHALLENGING STRING MANIPULATION PROJECTS 10.29 (Project: A Spelling Checker) Many popular word processing software packages have built- in spell checkers. In this project, you are asked to develop your own spell-checker utility. We make suggestions to help get you started. You should then consider adding more capabilities. Use a computerized dictionary (if you have access to one) as a source of words. Why do we type so many words with incorrect spellings? In some cases, it is because we simply do not know the correct spelling, so we make a best guess. In some cases, it is because we transpose two letters (e.g., defualt instead of default ). Sometimes we double-type a letter accidentally (e.g., hanndy instead of handy ). Sometimes we type a nearby key instead of the one we intended (e.g., biryhday instead of birthday ), and so on. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

598 Strings and Characters Chapter 10 (Web design portfolio) 99.87 12345678

Wednesday, December 12th, 2007

598 Strings and Characters Chapter 10 99.87 12345678 contains three blank spaces. If a check is printed with blank spaces, it is easier for someone to alter the amount of the check. To prevent a check from being altered, many check-writing systems insert leading asterisks to protect the amount as follows: ***99.87 12345678 Write an application that inputs a dollar amount to be printed on a check, then prints the amount in check-protected format with leading asterisks if necessary. Assume that nine spaces are available for printing the amount. 10.26 (Writing the Word Equivalent of a Check Amount) Continuing the discussion of the previous exercise, we reiterate the importance of designing check-writing systems to prevent alteration of check amounts. One common security method requires that the check amount be written both in numbers and spelled out in words as well. Even if someone is able to alter the numerical amount of the check, it is extremely difficult to change the amount in words. a) Many computerized check-writing systems do not print the amount of the check in words. Perhaps the main reason for this omission is the fact that most high-level languages used in commercial applications do not contain adequate string-manipulation features. Another reason is that the logic for writing word equivalents of check amounts is somewhat involved. b) Write an application that inputs a numeric check amount and writes the word equivalent of the amount. For example, the amount 112.43 should be written as ONE HUNDRED TWELVE and 43/100 10.27 (Morse Code) Perhaps the most famous of all coding schemes is the Morse code, developed by Samuel Morse in 1832 for use with the telegraph system. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Other representations of dots and dashes are used with light-oriented systems and signal-flag systems. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented system, a space is indicated by a short period of time during which no sound is transmitted. The international version of the Morse code appears in Fig. 10.31. Write an application that reads an English language phrase and encodes the phrase into Morse code. Also write a program that reads a phrase in Morse code and converts the phrase into the English language equivalent. Use one blank between each Morse-coded letter and three blanks between each Morse-coded word. 10.28 (A Metric Conversion Program) Write an application that will assist the user with metric conversions. Your program should allow the user to specify the names of the units as strings (i.e., centimeters, liters, grams, etc. for the metric system and inches, quarts, pounds, etc. for the English system) and should respond to simple questions such as “How many inches are in 2 meters?” “How many liters are in 10 quarts?” Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 10 Strings and Characters 597 Word length (Web design)

Tuesday, December 11th, 2007

Chapter 10 Strings and Characters 597 Word length Occurrences 1 0 2 2 3 1 4 2 (including ’tis) 5 0 6 2 7 1 Fig. 10.30 Counts for the string “Whether’tisnoblerinthemindto suffer”. c) Write an application that reads several lines of text and prints a table indicating the number of occurrences of each different word in the text. The first version of your program should include the words in the table in the same order in which they appear in the text. For example, the lines To be, or not to be: that is the question: Whether ’tis nobler in the mind to suffer contain the words to three times, the word be two times, the word or once, etc. A more interesting (and useful) printout should then be attempted in which the words are sorted alphabetically. 10.24 (Printing Dates in Various Formats) Dates are printed in several common formats. Two of the more common formats are 04/25/1955 and April 25, 1955 Write an application that reads a date in the first format and prints that date in the second format. 10.25 (Check Protection) Computers are frequently employed in check-writing systems such as payroll and accounts payable applications. Many strange stories circulate regarding weekly paychecks being printed (by mistake) for amounts in excess of $1 million. Incorrect amounts are printed by computerized check-writing systems because of human error and/or machine failure. Systems designers build controls into their systems to prevent such erroneous checks from being issued. Another serious problem is the intentional alteration of a check amount by someone who intends to cash a check fraudulently. To prevent a dollar amount from being altered, most computerized check-writing systems employ a technique called check protection. Checks designed for imprinting by computer contain a fixed number of spaces in which the computer may print an amount. Suppose a paycheck contains eight blank spaces in which the computer is supposed to print the amount of a weekly paycheck. If the amount is large, then all eight of those spaces will be filled, for example: 1,230.60 (check amount) 12345678 (position numbers) On the other hand, if the amount is less than $1000, then several of the spaces would ordinarily be left blank. For example, Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

596 Strings and Characters Chapter (Cheapest web hosting) 10 10.15 Write

Monday, December 10th, 2007

596 Strings and Characters Chapter 10 10.15 Write an application that inputs text and outputs the text in uppercase and lowercase letters. 10.16 Write an application that inputs several lines of text and a search character and uses method Stringmethod indexOf to determine the number of occurrences of the character in the text. 10.17 Write an application based on the program in Exercise 10.16 that inputs several lines of text and uses Stringmethod indexOf to determine the total number of occurrences of each letter of the alphabet in the text. Uppercase and lowercase letters should be counted together. Store the totals for each letter in an array and print the values in tabular format after the totals have been determined. 10.18 Write an application that reads a series of strings and outputs only those strings beginning with the letter b. The results should be output to a text area. 10.19 Write an application that reads a series of strings and prints only those strings ending with the letters ED. The results should be output to a text area. 10.20 Write an application that inputs an integer code for a character and displays the corresponding character. Modify this program so that it generates all possible three-digit codes in the range form 000 to 255 and attempts to print the corresponding characters. Display the results in a text area. 10.21 Write your own versions of the String methods for searching strings. 10.22 Write a program that reads a five-letter word from the user and produces all possible three- letter words that can be derived from the letters of the five-letter word. For example, the three-letter words produced from the word bathe include the commonly used words ate, bat, bet, tab, hat, the and tea. SPECIAL SECTION: ADVANCED STRING MANIPULATION EXERCISES The preceding exercises are keyed to the text and designed to test the reader s understanding of fundamental string-manipulation concepts. This section includes a collection of intermediate and advanced string-manipulation exercises. The reader should find these problems challenging, yet entertaining. The problems vary considerably in difficulty. Some require an hour or two of program writing and implementation. Others are useful for lab assignments that might require two or three weeks of study and implementation. Some are challenging term projects. 10.23 (Text Analysis) The availability of computers with string-manipulation capabilities has resulted in some rather interesting approaches to analyzing the writings of great authors. Much attention has been focused on whether William Shakespeare ever lived. Some scholars believe there is substantial evidence indicating that Christopher Marlowe or other authors actually penned the masterpieces attributed to Shakespeare. Researchers have used computers to find similarities in the writings of these two authors. This exercise examines three methods for analyzing texts with a computer. a) Write an application that reads several lines of text from the keyboard and prints a table indicating the number of occurrences of each letter of the alphabet in the text. For example, the phrase To be, or not to be: that is the question: contains one a, two b s, no c s, etc. b) Write an application that reads several lines of text and prints a table indicating the number of one-letter words, two-letter words, three-letter words, etc. appearing in the text. For example, Fig. 10.30 shows the counts for the phrase Whether ’tis nobler in the mind to suffer Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 10 Strings and Characters 595 (Web proxy server) 10.7 Write

Monday, December 10th, 2007

Chapter 10 Strings and Characters 595 10.7 Write an application that uses Stringmethod compareTo to compare two strings input by the user. Output whether the first string is less than, equal to or greater than the second. 10.8 Write an application that uses String method regionMatches to compare two strings input by the user. The program should input the number of characters to be compared and the starting index of the comparison. The program should state whether the first string is less than, equal to or greater than the second string. Ignore the case of the characters when performing the comparison. 10.9 Write an application that uses random number generation to create sentences. Use four arrays of strings called article, noun, verband preposition. Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, articleand noun. As each word is picked, concatenate it to the previous words in the sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and end with a period. The program should generate 20 sentences and output them to a text area. The arrays should be filled as follows: The article array should contain the articles “the”, “a”, “one”, “some” and “any”; the noun array should contain the nouns “boy”, “girl”, “dog”, “town” and “car”; the verb array should contain the verbs “drove”, “jumped”, “ran”, “walked”and “skipped”; the preposition array should contain the prepositions “to”, “from”, “over”, “under”and “on”. After the preceding program is written, modify the program to produce a short story consisting of several of these sentences. (How about the possibility of a random term paper writer!) 10.10 (Limericks) A limerick is a humorous five-line verse in which the first and second lines rhyme with the fifth, and the third line rhymes with the fourth. Using techniques similar to those developed in Exercise 10.9, write a Java program that produces random limericks. Polishing this program to produce good limericks is a challenging problem, but the result will be worth the effort! 10.11 (Pig Latin) Write an application that encodes English language phrases into pig Latin. Pig Latin is a form of coded language often used for amusement. Many variations exist in the methods used to form pig Latin phrases. For simplicity, use the following algorithm: To form a pig Latin phrase from an English language phrase, tokenize the phrase into words with an object of class StringTokenizer. To translate each English word into a pig Latin word, place the first letter of the English word at the end of the word and add the letters ay. Thus, the word jump becomes umpjay, the word the becomes hetay, and the word computer becomes omputercay. Blanks between words remain as blanks. Assume the following: The English phrase consists of words separated by blanks, there are no punctuation marks and all words have two or more letters. Method printLatinWord should display each word. Each token returned from nextToken is passed to method printLatinWord to print the pig Latin word. Enable the user to input the sentence. Keep a running display of all the converted sentences in a text area. 10.12 Write an application that inputs a telephone number as a string in the form (555) 5555555. The program should use an object of class StringTokenizer to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. The program should convert the area code string to int (remember parseInt!) and convert the phone number string to long. Both the area code and the phone number should be printed. Remember that you will have to change delimiter characters during the tokenization process. 10.13 Write an application that inputs a line of text, tokenizes the line with an object of class StringTokenizer and outputs the tokens in reverse order. 10.14 Use the string comparison methods discussed and the techniques for sorting arrays developed in Chapter 7 to write a program that alphabetizes a list of strings. Allow the user to enter the strings in a text field. Display the results in a text area. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01