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.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.
- (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.
- (1pt)
Give a brief description of what mystery returns.
- (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 ) {
}
|
- (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 or 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.
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs