Midterm exam 1 review Sp2006: Short Essay
Questions, comments, concerns, answers, concerns about answers?
(Back to Midterm Exam 1 Review Sp2006)
Please add to these answers–I'd like to know what y'all think!
A. Why would someone want to extend another class, in the way that Picture extends SimplePicture?
In order to add functionality to a class without messing up the original.
In order to make a more specific type of a particular object.
B. What is the difference between public and private?
I had help from our TA on this one–Public can be accessed from all other classes.
private can be accessed only by the class in which it is defined.
C. What is a static method and why would you ever want one?
Static methods can be run without first creating an instance of their class. This can be useful for methods that act more as a library (like songPhrase), where we don't really want or need an object to manipulate, just some data. Also, good for stuff like main(), which can be accessed w/out creating an instance (eg. java turtleDance).
D. What in the world is a void method?
A void method is just a method that returns nothing. We've used them to manipulate objects (eg. picture.show() does something with a picture, no need to send back any data).
E. Why would you want to set a variable to null?
Again, I thank our TA here. Null is used with objects. We want to set a variable to null when we have not yet created an object to point to. An example is the linked lists we've been using–every node has a "next," even though there may be nothing there yet (end of list), so we use null.
Phew. Again, please give more answers–combined opinions work better!
Jim REMOVED, you are a genius
Jim is the new unofficial TA.
I love Jim.
About the static method, so static methods, like songPhrase, are things that when executed spit out the data they contain to become an object? In other words, if a static method creates a short musical phrase of notes. Could you just call it when writing another method by typing:
AShortPhrase = execute static method
But then how would you execute the static method if let's say its called phrase1()? Would it be:
AShortPhrase = phrase1()
For part A, I would also say in order to inherit all the mehtods in the sub-class to act on instances of the super-class.
Ack, there's no names from anybody! Ok, person who asked about static: close, as I understand it. But I should clarify.
Let's look at a regular class first:
The Phrase class, for example, has a bunch of methods for manipulating a musical phrase (stuff like addNote and addNoteList). All of its methods have a common sort of theme–they make a phrase work.
Now let's look at the SongPhrase class. It is designed to hold some large chunks of code that don't really have a specific object that they belong to. In other words, house() and riff3() and pattern1() and random() and so forth don't really have a common purpose that drives the SongPhrase object. In even more other words, there's not really a reason that you'd specifically want to create a SongPhrase object, since it doesn't really do a whole lot by itself.
But, somewhere along the way, someone thought "wouldn't it be useful if I could write some methods down somewhere in Java that I could use down the road instead of having to type in huge lists of notes every time I want to make a musical phrase." (Or something close to that).
The place to write stuff down in in objects, so someone made a logically named object. But wouldn't it be silly to have to create a SongPhrase object just to get that data?
But that's what we have to do with non-static stuff. Try telling the Dr. Java interface to scale(0.5) – it won't work. You have to make a new picture and then say "do this method on this picture," or myPic.scale(0.5); In the case of scale(), that's a good thing–you don't want to let people run scale if there's no picture to wok on.
But the methods in SongPhrase don't do anything to the object "this," and they're really just something you want to be able to use anywhere. In this case, it's actually desireable to let the user run a function without having to first create a new instance of the object that has that method. And that's exactly what static lets you do.
The syntax is:
import jm.music.data.*; //So that it knows what a phrase is
Phrase p = SongPhrase.riff1();
Notice how we never actually had to make a new instance of SongPhrase, just indicate that riff1() is a method that belongs to it. Since riff1() is static, it can be run this way. And riff1() returns a jm.music.data.Phrase object (a Phrase), so when we define our variable, remember to call it a phrase.
BTW, don't confuse this with the abstract classes we ran into on Wednesday! (not on the test).
So, that ended up being long... But I'm honestly trying to help! I must also point out that whatever I say is in no way guaranteed to be the definite answer–it's just what I would put on my own test, so I hope it works.
That's a good explanation about static method.
Link to this Page