Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm exam 1 review Sp2005: Short Essay

Comments? Questions? Answers? Questions on Answers? Comments on Answers?



can a public method in a certain class be accessed by any stance of another class?

Yup! ("Instance" not "stance.") Think about what's happening when we say pixel.setRed() in the main method of class MyPicture. That's one class (MyPicture) accessing the method setRed of another class. That only works because setRed is declared to be public. Mark Guzdial


thank you for your responce. what are the limits to using methods on classes that aren't remotely related. like using setRed() on the primitive "String." you can't say someString.setRed().

You can only use a method on instances of the class where it's defined or instances of subclasses of that class. For example, Turtle instances understand turn, because turn is defined by SimpleTurtle. Strings are completely unrelated to Turtles, so you can't say "abc".turn(90). Mark Guzdial


A. Why would someone want to extend another class, in the way that Picture extends SimplePicture?

Sometimes it is useful to have a generic class that has certain properties, and then to extend it so that its children have different qualities for purposes of polymorphism. For instance, a dog and a cat do not share all the same traits, so it would make sense to have one animal class, and to have dog and cat both extend animal.

B. What is the difference between public and private?

A public method or instance variable can be accessed by any method or object. Private methods and variables however, can only be accessed by the instance of an object to which they belong.

C. What is a static method and why would you ever want one?

A static method is a method which must be implemented in all classes that extend the class which originally contains the static method. This is useful when creating interfaces, and insuring that the same features of a class are inherited by its children. For instance, the animal class might have a static "die" method that both the dog and cat must somehow implement.

#4: This isn't quite true. A static method is one that can be called without needing an instance of a class. Here's an example:

public class Greeter {
public void sayHello() {
System.out.println("Hello world");
}
public static void sayGoodbye() {
System.out.println("Goodbye world");
}
}
public class Example {
public static void main (String args) {
Greeter myGreeter = new Greeter();
//I need an instance of Greeter in order to call sayHello
myGreeter.sayHello();
//I don't need an instance of Greeter because sayGoodbye is static
Greeter.sayGoodbye();
}
}

D. What in the world is a void method?

A method which does not return a reference to any object, nor any value.

E. Why would you want to set a variable to null?

So that you do not immediately tie yourself to referencing a particular object, especially if it has not yet been instantiated.

Student36
On E. – I was actually looking for a bit of an explanation of what null is. Mark Guzdial


want some feed back on this one:

C. What is a static method and why would you ever want one?

static defines a method as a class method.
Class methods are invoked by the class instead of a specific instance, and can only operate on class variables.

Is that last part important? That it can only operate on class variables? What does that mean? You still don't explain why you'd ever want one. What class methods have we used? Mark Guzdial


will somebody please post a suggestion on why you'd want to use a static method. my best guess is: it allows you to call a method without an instance of a class. you can also call it in other classes.

The first reason is an excellent one! Hints: Think about FileChooser and SongPhrase. Mark Guzdial


yeah. when i formulated that i was looking at picture.java. all the static methods can operate independantly from an instance by taking in a class of something in the ( )

Picture.java has a bunch of static methods in it that are there as examples. (See http://coweb.cc.gatech.edu/mediaComp-plan/101) Those aren't good examples of static methods. Better are the static methods that we use everyday. Mark Guzdial


null is used not to attribute a value. it's like void...but not. how specific do we need to be on this one?

A static method example is pickAFile()

That is an excellent example of a static method. "null is used not to attribute a value"? What does that mean? Mark Guzdial


i...guess i don't know? that's why i posted..ha

I thought that "null" meant "without value." It's useful to declare "null" variables that have changing values so we don't have to name each and every one. REMOVEDke "Pixel pixel = null;" - we declare this variable "null" at the beginning so that we can let it refer to each pixel every time that Dr. Java goes through the loop. This way, we don't have to name every single pixel in the picture, and Dr. Java doesn't complain about redefining variables. Or... something like that? Maybe? ;)

Absolutely! null means "no value." But it isn't "undefined." Think about our linked lists. We set up next to point to null. That means "This node doesn't have a next, yes." REMOVEDterally, "The next here has no value." Mark Guzdial




Link to this Page