Docsity

Object Oriented Programming - Final Exam Review | COP 3330, Exams of Computer Science

Toggle sidebar
bg1
COP 3330 Final Exam Review
DATE: April 26. 2007 (THURSDAY)
TIME: 4 – 7 PM
PLACE: CSB 101
I. The Basics (Chapters 2, 5, 6)
a. comments
b. identifiers, reserved words
c. white space
d. compilers vs. interpreters
e. syntax, semantics
f. errors
i. syntax
ii. run-time
iii. logic
g. primitive data types
h. arithmetic expressions
i. I/O – both standard and file
j. boolean expressions
k. control structures
II. Using Classes (Chapter 3 and Class Handouts)
a. How to create an object
b. How to call methods on objects
c. String class
d. Scanner class
e. Math class
f. Random class
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Object Oriented Programming - Final Exam Review | COP 3330 and more Exams Computer Science in PDF only on Docsity!

COP 3330 Final Exam Review DATE: April 26. 2007 (THURSDAY) TIME: 4 – 7 PM PLACE: CSB 101 I. The Basics ( Chapters 2, 5, 6 ) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time iii. logic g. primitive data types h. arithmetic expressions i. I/O – both standard and file j. boolean expressions k. control structures II. Using Classes ( Chapter 3 and Class Handouts ) a. How to create an object b. How to call methods on objects c. String class d. Scanner class e. Math class f. Random class

III. Arrays (Chapter 8: 8.1-8.5, 8.8) a. syntax b. dynamic allocation c. how to access an element d. use with a for loop e. two-dimensional arrays IV. Collections ( Chapter 8: 8.9-8.11 ) a. ArrayList b. Vector c. Collections methods V. Defining classes ( Chapter 7 ) a. Instance variables b. Constructors c. Instance methods d. Difference between static and instance e. private, public f. this g. difference between formal parameters&instance vars h. Scoping Rules h. all object names are REFERENCES!!! i. overloading methods j. overriding methods k. instanceof operator l. generic classes VI. Inheritance and Polymorphism ( Chapter 9 ) a. defining extra instance variables b. polymorphism c. overriding methods d. super (in constructors) e. super (for other methods) f. visibility modifiers g. late binding

How to study:

  1. Go through both of your programs. Make sure you understand how they work. I may ask questions based on your homework assignments.
  2. Look over all of the sample programs presented in class. For this semester, these contain a more thorough breakdown of the material covered in class than the notes.
  3. Read the class notes. Make sure you understand the ideas presented.
  4. Skim the chapters we have covered this semester.
  5. Peruse the problems at the end of the appropriate chapters in the text and plan out how you would attack them. Exam Format Here are the different types of questions I will have:
  6. Tracing: Either through code segments or of an entire program that uses a class.
  7. Writing: You may have to write a main method that uses a class, or write class methods.
  8. You may have to find mistakes in a piece of code.
  9. Short Answer: Some of the reading goes over general concepts in fairly non-technical manner.
  10. Design Question: I may give you the basic idea of a class to be written and then ask you general questions about the design of the class. I may ask you to draw an UML diagram.

Program Basics

All java code resides in classes. Only code from the main method of a class gets executed. The most basic java program resides in a single class that only contains a main method. Here is the layout: public class Test { public static void main(String[] args) { ... } } The name of the file storing a class should be the classnmame dot java. The above class should be stored in the Test.java file. In order to run a java file, one must first compile it. If the program contains no errors, the java compiler will create a corresponding class file, (in this case, Test.class.) In essence, this file contains JVM instructions, which are sort of like assembly language. In order to run a Java program, you must "interpret" a class file. On the command line you would type, "java Test", then your program will execute. In a simple program, here are some of the standard java statements:

  1. Declaration of variables
  2. Assignment Statements
  3. Creation of objects and calling predefined methods on them

Boolean Expressions Java is different than C in that there is a boolean type. Thus boolean expressions included in ifs and whiles, etc. can only be of type boolean, so you can't have arithmetic expressions for boolean expressions in any control structure. Make sure you know how && and || work, and how to parenthesize boolean expressions. Arithmetic Expressions They are the same as it C. The two that are somewhat tricky are integer division and mod(%) Control Structures Know how to trace through each of the following control structures:

  1. if and all variations
  2. switch
  3. while
  4. for
  5. do

Defining Classes

Remember when you are defining a class, you need to define two parts:

  1. instance variables (these dictate the structure of an object of the class.)
  2. instance methods (these dictate what operations are allowed on an object of the class.) Generally instance variables are private, and all methods you want others to use are public. Other methods, such as the totalminutes method in the Time class need not be public. Remember that there are three types of variables you could have when dealing with instance methods:
  3. instance variables
  4. formal parameters
  5. local variables Learn the difference between the three and do not call different types of variables with the same name.

Remember that anytime you refer to an instance variable

in an instance method, you are referring to the instance

variable OF the object the method was called upon.

Formal parameters are the information the method needs to complete it's task. Local variables are declared as necessary, such as loop indexes.

Thus, the Time class, which we defined in class, automatically inherits all of the characteristics of the Object class. For example, the Object class has a toString method. If there is no toString method declared in the Time class, then anytime the toString method is called on a Time object, it will actually run the toString method defined in the Object class. In essence, the Time class has inherited the toString method in this example. The other option would be to write a toString method in the Time class. Once we do this, if we call the toString method on a Time object, it will call the one defined in the Time class, overriding the toString method in the Object class. (The rule is that the method in the “nearest” class overrides all similar methods in previous parent classes.) It would be a good idea to take some of the examples shown in class and test out what occurs when a method is overridden and when it is not. toString The following method is defined in the Object class: public String toString(); It is frequently overridden in user-defined classes. The reason for this is as follows: Anytime someone tries to print out an Object, what java automatically does is call toString() on that Object and just prints out what that method returns. Thus, if t is a time object and you run System.out.println(t); // You are implicitly running System.out.println(t.toString());

compareTo This method is necessary to implement the Comparable interface. We will talk about interfaces later, but the basic idea is that to implement an interface, you must write certain methods in your class. To implement Comparable, you must write a method with the following signature: public int compareTo(Object o); Furthermore, the method should work as follows: It should return a negative integer if the current object comes before o. It should return 0 if the two objects are equal and it should return a positive integer if the current object comes after o. Notice that the method takes in an Object. Thus, you must cast o to the appropriate type in order to access any components of the actual object that gets passed into the method. static vs. instance static means, “belongs to the class.” instance means, “belongs to the object.” An instance method MUST BE called on an object of that class. A static method is always called CLASSNAME.method(); because it just belongs to the class and does NOT operate on any object inherently. An instance variable belongs to the object, whereas only one copy of a static variable ever exists, regardless of how many objects of a class are instantiated. Visibility modifiers

An inherited class is defined as follows, using the key word extends: public class ThreeDimensionalPoint extends Point { … } In the example above, Point is known as the superclass, and ThreeDimensionalPoint is Point's subclass. (Alternatively, Point is known as the base class from which ThreeDimensionalPoint inherits. Also, there is an IS-A relationship here – a ThreeDimensionalPoint object IS A Point object as well.) A subclass inherits all the characteristics of the superclass. Thus, all public methods in the superclass are valid to be run on objects of the subclass. In the typical case, the subclass adds at least one instance variable, separate from those it inherited from the superclass. In the example in the text, ThreeDimensionalPoint has its own instance variable z, and inherits x and y from Point. In the typical case, a subclass just uses some of the methods in its superclass, but overrides other methods. To override a method, you must define a method with the same signature as one in the superclass in the subclass. The purpose of this is so that if you have a ThreeDimensionalPoint object, you end up running a different method on it than if you just had a Point object. The method call super()

When overriding a method in the superclass, you have the option of calling the corresponding method from the superclass. To do this, instead of using "this" just use the name "super". For all methods except for constructor, you would call, super.methodname(). Pass the parameters you need/want to pass to the corresponding method in the superclass. Constructors All constructors in the subclass will automatically call the default constructor in the base class if there is NO EXPLICIT call to the base class constructor. The other option is to make an explicit call to the superclass constructor using "super". In this case, you control which of the constructors in the superclass gets called. (Here, the method call is just super, not super.methodname()) The rationale behind this is that you MUST build the base object first before building any of the rest of it to create the subclass specialization. Reasons to use Inheritance, instead of just adding to one class Putting all the functionality into one class may make it difficult to understand, or more difficult to use for simple things. (For example, sometimes you want to use points in 2 dimensions, sometimes in three. By incorporating inheritance, you make the use of both seamless. If you didn't, when wanting a 2-D point, you'd have to always just set z=0 and deal with that extra component.) Polymorphism

Visibility Modifier Rules Member Restriction this Subclass Package General public X X X x protected X X X default X x private X instanceof operator The instanceof operator is used in Boolean expressions as follows: reference instanceof Class This expression is true if the reference to the left of the operator is referring to an object that belongs in the class designated to the right of the operator. It doesn't have to be the exact class. Thus, if the variable pt is a Point reference, and it was referring to a ThreeDColoredPoint object, then the expressions pt instance of Point pt instance of ColoredPoint pt instance of ThreeDColoredPoint would all evaluate to true. But if pt referred to a ColoredPoint object, then the first two would be true and the last one would be false. Late Binding

I don't think this is any different than the regular idea of polymorphism, but it's possible for two different references to be used to call the same method initially, but have that SAME method call work differently. Imagine that a class M inherits from L. A method f is declared only in L. A method g is declared in both L and M. The method f makes a call to the method g. Let l and m be references to objects from classes L and M, respectively. The method call l.f(), will eventually invoke the method g written in the class L. BUT, the method call m.f() which calls the same method f as the previous call, will invoke the method g written in the class M, since Java realizes that the original object that the method f was called upon was of class M. Final class You can not inherit from a final class. Interfaces Not a real class. You can only specify constants and method signatures. A real class can be created that "implements" an interface. You can do this as follows: public class X implements Comparable { … } (Note: Any interface can take the place of Comparable.) Abstract Base Classes

Find the first one that matches, and execute the code in that catch block. If there is a finally block, that code always gets executed, regardless of whether there was an exception or not. (The exception if is an return statement is hit before it.) Finally, you can create your own exceptions that inherit from Exception. Then you would have to throw these exceptions and try to catch them elsewhere. Enums These should be used when you are storing some discrete information that isn't necessarily numerical, for example the suits in a deck of cards or the set of months. In essence, you could think of each of these as a type without associated methods. Thus, Suit is a type which can have four values: CLUBS, DIAMONDS, HEARTS and CLUBS. In this case, we can define this "type" as an enum as follows: public enum Suit { CLUBS, DIAMONDS, HEARTS, CLUBS } Now, if you want to declare a variable of this enum, you do as follows: suit mycardsuit; We can set this to a value as follows: mycardsuit = Suit.CLUBS; These are preferable to using integer constants because integer constants can be meaningless (Why is CLUBS equal to 0?) in some contexts and if an "invalid" integer is used to represent

an enum, the compiler will not catch it and a run-time error or logical error could occur. (For example, if we passed in 5 into a method that expected a suit, but only 0, 1, 2 and 3 were well- defined suits, then the behavior of that method is not guaranteed.) UML Class Diagrams These outline the design of a class as well as the relationship between classes in an application. Codes for visibility: -(private), +(public), # (protected) Syntax: types are listed in a different place in an UML diagram than in regular Java syntax Inheritance relationship: denoted by a line and arrow with the arrow(triangle) pointing to the superclass. Implementation is a broken line. HAS-A relationship: denoted by a line and a diamond pointing to the more complex of the two classes.