9.6 Review Exercise 9

  1.       What would be printed by the following fragment?
    
             String s = "one";
             String t = new String(s);
             String u = s;
             u = "two";
             t = "three";
             System.out.println(s + " " + t + " " + u);
          
          
  2.       Given the declaration
             String s = "Alyssa Titus";
          find the value of each expression.
          (a) s.charAt(3)                   (b) s.indexOf('a')
          (c) s.substring(7)                (d) s.length()
          (e) s.substring(2,3)              (f) s.charAt(2)
          (g) s.indexOf('o')                (h) s.indexOf('s',5)
          (i) s.toLowerCase()               (j) s.substring(1,7)
          (k) s.equals("Alyssa")            (l) s.compareTo("a") < 0
       
        
  3.      For each expression, state whether it is true or false.
    
          (a) '3' > '2'
          (b) 'm' < 'M'
          (c) '7' < 's'
          (d) 'z' < ' '
          (e) "foo".compareTo("fool") < 0
          (f) "foo".compareTo("Foo") > 0
          
    	
  4.       A program is expecting command line Arguments in the form
             <given <name> <family name> <age> <sex>
          where <sex> is a single charecter. For example,
            Luka Matutinovic 19 m
          would be a valid sequence of command line arguments.
    
          Assuming that the main method of the program has the form
            public static void main (String[] args)
          write a fragment that would use these arguments to assign appropriate values to the String variable full name,
          the int variable age, and the char variable sex. For the example, the fragment should set
            full name = "Luka Matutinovic"
            age = 19
            sex = 'm'
          Assume that all arguments are valid.
          
    	
  5.       Suppose that we want a class Dog in which each dog has a name, a breed, and an age (in years). The firlds of the class 
          are
    
            private String name;
            private String breed;
            private int age;
    
          (a) Wrirt an equals method for the Dog class. Two Dog objects should be considered equal if the breeds are the same and 
              the ages differ by one year or less.
    
          (b) Write a toString method for the class. As an example, for an object representing a three year old Samoyed named
              "Toby", the method should return "Toby - Breed: Samoyed, Age: 3"
              
    
  6.       Write a program that reads a string and then prints a diamond shaprd array based on the characters of the string. 
          As an example, if the string has the value "SAMPLE", then the program should print the pattern
    
                                                S
                                               SAS
                                              SAMAS
                                             SAMPMAS
                                            SAMPLPMAS
                                           SAMPLELPMAS
                                            SAMPLPMAS
                                             SAMPMAS
                                              SAMAS
                                               SAS
                                                S
    
          The program should work for a string up to ten xharecters long. If a user supplies a string that is longer than ten
          characters, the program should use only the first ten characters in forming the pattern.
          
    
  7.       A palindrome is a string that reads the same both forward and backward. Examples of palindromes are "radar", "31413",
          and "god a dog".
    
          (a) Write a boolean-valued method isPalindrome that has a single String parameter. The method should return true if and 
              only if its parameter is a palindrome.
       
          (b) Modify your method so that it ignores the cases of letters, punctuation marks, and blanks in making a decision about
              whether or not a string is a palindrome. For example, the string:
                 "A man, a plan, a canal: Panama!"
              (a reference to Ferdinanad de Lessups, the chief engineer of the first attempt to build the Panama canal) should be
              taken to be a palindrome.
              
    
  8.       In the Review Exercises of Chapter 4, Question 13 asked for a progran that implemented a test for divisibility by 11.
          In answering that question, you were to assume that the input could be represented by a long value. If the input is read
          as a String, this restriction is no longer necessary. Write a program that repeatedly prompts to user for arbitrarily long
          integers and uses Dodgson's algorithm to determine whether or not each one is divisible by 11.
       
    
  9. Projects

  10.       Write a program that reads names in standard form and prints them in the form
    
                  <last name>, <any given initials>
    
          The program should prompt the user for names, halting when the user provides the name ZZZ. As an example, the input of
          Santa Claus
          Michael J. Fox
          Madonna
          William Henry Richard Charles Windsor
          ZZZ
    
          should produce output of
        
           
           Claus, S.
           Fox, M. J.
           Madonna
           Windsor, W. H. R. C.
  11.       Write a program that reads a string containing a Roman numeral representing a value in the range 1 to 3999. The program 
          should print the Roman numeral and its value in our notation (a Hindu Arabic numeral).
      
               Roman numerals use the symbols I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and M = 1000.
            
               Roman numerals use an additive rather than a positional notation. Numerals are formed by writing symbols from the
               preceding list, from left to right, to represent a sum, each time using the symbol for the largest possible value 
               from the list of symbols. This rule is subject to the limitation that no symbol may be written ,ore than three 
               times in a row. To avoid this, the system uses IV = 4, IX = 9, XL = 40, XC = 90, CD = 400, and CM = 900.
    
          As examples, VIII = 8, XXIV = 24, LXXXIX = 89, MMVIII = 2008, MDCCCLXXIX = 1879
    
  12.       Write a program that will perform arithmetic operations on pairs of integers written in various number bases. Input to 
          the program should consist of aan arbitrary number of expressions, each of which is written on one line. Each expression
          should consist of the following five items seperated by one or more blanks:
    
               an input base: 2 to 10
            
               a first integer, written in the input base
            
               an operator: +, -, *, /, %
     
               a second integer, written in the input base
    
               an output base: 2 to 10
    
          The two integers written in the input base should be converted to a base ten representation, the indicated arithmetic 
          operation should be performed, the result should be converted to the output base, and a summary should be prrinted 
          in a tasteful manner. As an example, input of
             73 (base 8) + 216 (base 8)
             = 59 + 142
             = 201
             = 1301 (base 5)
    
          Your program should be modular, using methods appropriately. Bad data should be rejected by your program without 
          causing it great grief.
    
    
  13.       One of the tasks performed by a text editor or word processor is the breaking up of text into lines of a specified 
          length. For this assignment you are to write a program that carries out this task. Specifically, your program should 
          perform the following tasks:
    
              Prompt the user, repeatedly if necessary, for a line width: an integer that is at least 20 and at the most 60.
         
              Read lines of text and rearrange the text if necessary so that the words fit into lines of the specified width. 
              The input will be terminated by a single line containing only ZZZ. You should not make any assumptions about the
              maximum length of a line of input or the maximum number of words in a line of input. You may assume that there will
              be no more than 100 lines of output. If a single word is longer than a full output line, display it on a line by
              itself with blank lines preceding and following the line containing the word. As illustrated in the following 
              example, there may be more than one blank between words. It is also possible that the input may contain blanks
              before punctuation marks. These should be eliminated. For example, the text
                  Bubbles , baubles   , and bangles are beautiful  .
              should be written as:
                  Bubbles, baubles, and bangles are beautiful.
              You may assume that the input contains only words and the following six punctuation marks: period (.), comma (,), 
              colon (:), semi-colon (;), exclamation mark (!), and question mark (?). If a punctuation mark occurs after a word 
              whose length is equal to or greater than the line length, that punctuation mark should be printed immediately 
              following the word. In such cases, the word should be preceded and followed by blank lines.
    
              Print the re-formatted text. This output should not include the ZZZ that terminated the input.
    
          The StringTokenizer class, described in section 9.4 of the text, contains methods that you may find helpful in writing
          your program. It is not necessary to use these methods; the project can be done by using only methods from the string
          class.
       
          Your program should behave as shown in the following example.
    
      
           Provide a line width (20-60): 75
           Provide a line width (20-60): 35
           Provide input (zz to stop):
           Reasonable people adapt    to the world    ; 
               the unreasonable     ones                   
              persist in trying to adapt the world    to themselves.
            Therefore all progress  depends   on
           unreasonable   people.
           aVeryVeryLongWordTahtMayNotFitOnOneLine.
           The last line
           ZZZ
     
           Reasonable people adapt to the
           world; the unreasonable ones                   
           persist in trying to adapt the 
           world to themselves. Therefore all 
           progress depends on unreasonable
           people.
      
           aVeryVeryLongWordTahtMayNotFitOnOneLine.
    
           The last line
  14.       Extend the program of the previous question so that it produces text that is right-justified. In this form, extra 
          blanks are inserted if necessary between words so that each line of the resulting text has exactly the width that
          was specified. Blanks should be distributed in a line so that inter-word spaces are as close to equal as possible.
          If some inter-word spaces are larger than others, the larger ones should be on the left. For example, if ten 
          blanks must be distributed over four inter-word spaces, they should be distributed left to right in the order 
          3-3-2-2. You should not try to justify the text of a line if there is only one word on a line or if the line is 
          the last line of the text. For the example of the previous question, your program should produce output of the 
          following form:
    
           
           Reasonable   people  adapt  to  the
           world;    the   unreasonable   ones                   
           persist  in  trying  to  adapt  the 
           world  to themselves. Therefore all 
           progress  depends  on  unreasonable
           people.
      
           aVeryVeryLongWordTahtMayNotFitOnOneLine.
    
           The last line