RU beehive logo ITEC dept promo banner
ITEC 120
2012fall
dbraffitt
ibarland

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

hw08
short answers: object-orientation; loops

due Nov.02 (Fri) at the start of class.

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. (Not in the middle of the loop — at the moment the condition is evaluated!)
    isoFar
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
  7. (1pt) Give a brief description of what mystery returns.                                                                                                      Imagine reading your answer to a friend who has not taken ITEC 120 — would your answer make any sense to them?1
  8. (1pt) “static” means “belongs to the                     , not to                     ”.
  9. (1pt) The purpose of a constructor is to make sure that                                         
  10. (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).
  11. (1pt) T / F : When you start running your program, one object of each class is automatically created.
  12. (1pt) A method should either have a side-effect (change a field), or                                         , but not both.
  13. (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
    
  14. (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.
  15. (2pts) Would you use a field, or a local variable, for each of the following? Imagine we were writing a class Dog.
    1. boolean hasFleas; // Whether or not this dog has fleas.
    2. // Inside a method, name a partial result:
      int ageToReturn = this.getAge() * DOG_YEARS_PER_YEAR;
  16. (6pts + 4pts e.c.) Let's play “static or not?”. For each method below, write a signature for the method, paying special attention to whether or not to include the adjective “static”. For non-static methods, include a commented-out parameter “this. You can assume each method is inside an appropriate class (Student or Song or TempConverter).

    1. Sample The signature for: A method to return an explorer's current hunger-level:
        double getHungerLevel( /* Explorer this */ )  
    2. The signature for: a function that returns an student's gpa.                 
    3. The signature for: a function that creates a new student, given a name
      (0 credits-completed, 0 credit-hours, major "undecided")                 
    4. The signature for: a function that returns whether a student is on the honor roll                 
    5. The signature for: a function that returns the school's valedictorian                 
    6. The signature for: a function that returns whether a student is the valedictorian                 
    7. The signature for: a function that returns whether one song is longer than another                 
    8. The signature for: a function that returns how much disk space is required for a given #mins/secs?                 
    9. The signature for: a function that returns how much disk space is required for a given song                 
    10. The signature for: a function that returns the longest song ever created?                 
    11. The signature for: a function that converts inches to meters                 

1

Moreover: this question is asking for what the function does, not how it does it. To see this important distinction, compare the following two descriptions — which one is more helpful?

The first describes how Newton's method computes the square root but manages to not tell you what the function is doing at a big level. The second description is the one you would want in the documentation; as a user you don't care how the function gets its answer as long as it does so.

     

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


©2012, Ian Barland, Radford University
Last modified 2012.Nov.04 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme