RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

hw08
short answers: object-orientation; loops

due Nov.11 (Thu) at the start of class.
If you turn in this homework in class on Wednesday, I will return it to you on Thursday morning.

All code should be written in object-oriented style: that is, use non-static methods when appropriate.

  1. (10pts)
    
    01.      class Glarkzle {
    02.
    03.        int numFloobs;
    04.        String name;
    05.
    06.        static final int MAX_ORDER_SIZE = 25;
    07.
    08.        Glarkzle( int _numFloobs, String _name ) {
    09.          this.numFloobs = _numFloobs;
    10.          this.name = _name;
    11.        }
    12.
    13.        String placeOrder( int numOrdered ) {
    14.          /* ... body not shown ... */
    15.        }
    16.
    17.        static int mystery( String s1, String s2 ) {
    18.          int i = 0;
    19.          int soFar = 0;
    20.          while (i<s1.length() && i<s2.length()) {
    21.            if (s1.charAt(i) == s2.charAt(i)) {
    22.              soFar += 1;
    23.            }
    24.            i += 1;
    25.          }
    26.          return soFar;
    27.        }
    28.      }
    1. Which line(s) declare a named constant?                 
    2. Which line(s) declare a field?                 
    3. Which line(s) declare a constructor?                 
    4. Declare a variable to hold an object of this class. (Don't do anything more):                 
    5. Write an expression which calls the constructor, storing the result in the variable you just declared above.                 
    6. Write an expression which concatenates "Your order ID: " with the result of calling placeOrder.                 

    7. The following three questions refer to the following lines of code:
      Glarkzle g1 = new Glarkzle( 34, "Ali Baba" );
      Glarkzle g2 = g1;
      Glarkzle g3 = new Glarkzle( 34, "Al Capone" );
                  
    8. How many Glarkzle objects are created?                 
    9. How many fields named “MAX_ORDER_SIZE” exist?                 
    10. How many fields named “numFloobs” exist?                 
  2. (2pts) A non-static method always has one implicit parameter named “                ”. What is the type of that parameter for non-static methods in class Glarkzle?                 
  3. (3pts) Write the method getName, a one-line getter method which just returns the object's name field.
    (You don't need test-cases or documentation -- just the method.)
    
    
    
    
    
    
    
    
                
  4. (3pts) Write setNumFloobs, a one-line setter method which just updates the object's numFloobs field.
    (You don't need test-cases or documentation -- just the method.)
    
    
    
    
    
    
    
    
                
  5. (4pts) Write the method equals which has a Glarkzle determine whether it has the same fields as another.
    (You don't need test-cases or documentation -- just the method. Do include all the necessary keyword(s) in front of the signature.)
    
    
    
    
    
    
    
    
    
                
  6. (4pts) Suppose we call Glarkzle.mystery("spectacular", "ape-man") from problem #1 above. Complete the table below, with the values of each variable every time the while-loop's condition is evaluated.
    isoFar
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
  7. (1pt) Give a brief description of what mystery returns.                                                                                                     
  8. (3pts) Re-write the method-body of mystery to use a for-loop instead of a while loop.
    static int mystery( String s1, String s2 ) {
    
    
    
    
    
    
    
    
    
    }
          
  9. (1pt) “static” means “belongs to the                     , not to                     ”.
  10. (1pt) The purpose of a constructor is to make sure that                                         
  11. (1pt) When Java wants to convert a Glarkzle to a String, it calls the method                  (which exists for all Objects even if you don't override it with a specialized version for your class, and must be declared public).
  12. (1pt) T or F: When you start running your program, one object of each class is automatically created.
  13. (1pt) A method should either have a side-effect (change a field), or                                         , but not both.
  14. (4pts) Write a method which prints out a table of the first n numbers and their cubes. For example, if given 5, the printout might be:
    1    1
    2    8
    3    27
    4    64
    5    125
    
  15. (1pt) When writing a loop, start by deciding how to initialize your                  variable and how you want to update it each time through the loop. Then decide how to initialize your accumulator (or “    -      ”) variable, and how you want to update it each time through the loop.

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Nov.18 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme