home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.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.
- (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. } |
-
Which line(s) declare a named constant?
-
Which line(s) declare a field?
-
Which line(s) declare a constructor?
-
Declare a variable to hold an object of this class.
(Don't do anything more):
-
Write an expression which calls the constructor,
storing the result in the variable you just declared above.
-
Write an expression which
concatenates
"Your order ID: " with the result
of calling placeOrder.
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" );
|
-
How many Glarkzle objects are created?
-
How many fields named “MAX_ORDER_SIZE” exist?
-
How many fields named “numFloobs” exist?
- (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?
- (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.)
- (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.)
- (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.)
- (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!)
- (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
- (1pt)
“static” means
“belongs to the ,
not to ”.
- (1pt)
The purpose of a constructor is to make sure that
- (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).
- (1pt) T / F : When you start running your program,
one object of each class is automatically created.
- (1pt)
A method should either have a side-effect (change a field),
or , but not both.
- (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
- (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.
- (2pts) Would you use a field, or a local variable, for each of the following?
Imagine we were writing a class Dog.
-
boolean hasFleas; // Whether or not this dog has fleas.
-
// Inside a method, name a partial result:
int ageToReturn = this.getAge() * DOG_YEARS_PER_YEAR;
-
(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).
- Sample
The signature for: A method to return an explorer's current hunger-level:
double getHungerLevel( /* Explorer this */ )
- The signature for: a function that returns an student's gpa.
- The signature for: a function that creates a new student, given a name
(0 credits-completed, 0 credit-hours, major "undecided")
-
The signature for: a function that returns whether a student is on the honor roll
-
The signature for: a function that returns the school's valedictorian
-
The signature for: a function that returns whether a student is the valedictorian
-
The signature for: a function that returns whether one song is longer than another
-
The signature for: a function that returns how much disk space is required for a given #mins/secs?
-
The signature for: a function that returns how much disk space is required for a given song
-
The signature for: a function that returns the longest song ever created?
-
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?
- Given a number x, set soFar to 1,
and then set soFar to (soFar - x/soFar)/2,
and keep repeating this until x-soFar*soFar is less than 0.0001.
Return soFar.
- Given a number x, return its square root.
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.
↩
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs