PIC 20A
JAVA and Internet Programming, Computation & Visualization
Midterm Study Guide, Mon., July, 22, 2002, 10:30-11:20AM, MS
6229
I. Theoretical Component
- Compiled vs. interpreted languages, pros & cons.
- Exec-Speed, portability, readability, language complexity, efficiency.
- Structural/Procedural vs. Object-Oriented programming differences
& similarities.
- Similarities: both use variables and functions, one can use
procedural-style of programming in OOP languages;
- Differences: Focus is on functionality vs. objects. Reusability,
efficiency of programming. Procedural programming describes mainly
action behaviors, where as OOP uses state/behavior of objects to provide
functionality.
- Applications (stand alone applications, can run by themselves,
Complete control over local data) vs. Applets (need to be run by another
application like a browser or appletviewer, Internet ready, but restricted
access to local file system).
- Illustrate in a figure the process of generating and running Java
programs (compiles & interpreter, ASCII vs. bytecode representation of
a program)
- Write ASCII source file --> Compile (javac) --> Invoke JVM
on the bytecode (*.class) to translate the java bytecode to chip-specific
machine language commands runnable by your machine.
- What do these acronyms stand for: JDK (Java Development Kit), API
(Java Application Programming Interface), JRE (Java Runtime Environment),
JVM (Java VIrtual Machine), OOP( Obj. Oriented Prog.)
- A class is the basic building block of an object-oriented
language, what is the difference between a class and an object
(give some examples).
- A class is the blueprint that describes the state and the behavior
associated with instances of that class.
- When you instantiate a class, you create an object that has the
same states and behaviors as other instances of the same class.
- Think of Bicycle class and its two instances: myFirstBike
= new Bicycle(); mySecondBike = new Bicycle();
- What is the program-entry-point for a Java application? [main()]
How about for a Java applet? [ init(), start() ]
- Differentiate between class and instance
members! Can we do away with one of these types?
- class members are common for all instances of a class. All instances
share these (e.g., main(), int numberOfInstances, etc.)
- Both are necessary
- What is a message? What are messages used for?
- Objects communicate & interact by using messages.
- An object’s behavior is expressed through its methods, so message
passing supports all possible interactions between objects.
- Objects don’t need to be in the same process or even on the same
machine to send and receive messages back and forth to each other.
- What is a state and what is a behavior of an object?
- Software objects have state and behavior, like any real world object.
- The state in represented by one or more variables. Its behavior
is controlled with methods.
- Can a Java class extend more than 1 class? [No!] How many? [Max
1!] Are there differences from C++?
- C++ class can extend multiple (inheritance) classes.
- What are interfaces?
- An interface is a device that unrelated objects use to interact
with each other. An object can implement multiple interfaces.
- Interface protocol of communication comes in the form of a set
of constant and method definitions contained within an interface prototype.
- Ex: The inventory interface would define, but not implement, methods
that set and get the retail price, assign a tracking number, and so on.
- What is inheritance? Is inheritance specific to OOP languages?
- Subclasses are not limited to the state and behaviors provided
to them by their superclass.
- The inheritance tree, or class hierarchy, can be as deep as you
want it to be.
- How can a Java object inherit functionality from multiple parents?
- If needed, via interface implementations
- What types of members can an object have?
- Static/Instance
- Variables/Methods/OtherObjects
- Primitive/Reference
- What are the Java primitive data-types? [byte, short, long, int,
float, double, char, boolean]
- How is variable declaration different from its instantiation
or initialization?
- Declaration: double myDouble;
MyClass newMemberOfMyClass;
- Initialization: myDouble = 5.5;
- Instantiation: newMemberOfMyClass = new MyClass(myArgList);
- Are object constructors different from other member methods? [
no return type ]
- Variable identifiers: type, scope, value, name.
- What are the conventions for naming your members (variables, constants,
methods)?
- Members start with lower-case characters, Name must be descriptive
of state/behavior it represents
- CONSTANTS all caps
- What type of iterative statements are allowed in Java?
- What type of conditional statements are allowed in Java?
- What are the differences between Character, String and
StringBuffer objects?
- Character contains one of the 256 terminal and non terminal characters.
- String is a non-modifiable collection of Chars
- StringBuffer's are a modifiable Strings
- What are the basic sub-classes of the abstract class
Number ?
- Byte Double Float Integer Long Short BigInteger BigDecimal
- What are the basic access-specifiers for classes/members?
- Private, protected, package, public
- What is this a reference to? Why is it necessary?
- Yes, it is a reference to the current object inside of itself.
- Ex. You may have a method that has a local variable with name exactly
like another variable global for the class. If you want to reset the global
variable from within your method you need this.var=0.0;
- Why do we want to have member variables be accessible only through
variable-access-methods provided by the class desinger/programmer?
- To prevent inappropriate setting/usage of state members by
outside users
- Nested classes! Implicit Objects!
- class myOutterClass {
- class myInnerClass{
- ...
- }
- }
- JPanel myPanel = new JPanel ( new BorderLayout() );
II. Applications Component
- Write a HelloWorldApp application that shows a window
and displays the string "Hello World".
- Write the command-lines to compile and run the application
- Convert the previous example to an applet. Include the necessary
HTML file. Why is this file needed? Can it be used to alter the behaviour/appearance
of the applet?
- Give 2 examples of defining, instantiation and usage of a
class and instance variables.
- Give examples of using messages (e.g., function calls).
- List 10 different members for a class
Rectangle, which would be necessary to provide the means
to alter the state and behavior of any object of type
Rectangle .
- drawRectangle(Graphics); setWidth(int); setHeight(int); getWidth();
getHeight(); boolean filledTrue;
- int borderThickness; Color fillColor; Color borderColor; int getArea();
int getCircumference(); etc.
- Give some examples (pseudocode) of inheritance and its importance.
- Shape --> Point, Line, Curve; Point --> Circle(center=Point)
--> XYBased_Cylinder(Circle + Height)
- Give some examples of declearing variables that are constants,
private to the current class or accessible to all classes within the package.
- Declare and instantiate an array of integers of 15 elements
and set each element of the array equal to the index of the element. Print
out the array indices and their values one element per line?
- Define a class Circle and include
some member variables and member methods and constructors, specific to
this class, leaving the bodies of the methods empty.
- Write a complete method that takes in a Vector object myData
and computes the average of the integer values in myData
.
- public float myAverage(Vector vec)
- { float avg = 0;
- for (int i = 0; i< vec.size(); i++)
- avg += (Integer)vec.elementAt(i)).getValue();
- if (vec.size()>0) avg /= vec.size();
- return avg;
- }
- Write the negation of the boolean statement
if ( (!p) && (q || r) ), where p, q, r are
initialized boolean variables.
- What is the binary expression 1100 & 1010 equal to?
- Write an application that reads a number as command line argument
and prints the name of the corresponding month, if the number is between
1 and 12, or prints No such month!, otherwise.
- Give 5 examples of calling methods from some commonly used objects.
How do we go about accessing object member variables?
- Object.toString(); Math.sqrt(); JPanel.repaint(); JApplet.start();
JTextField.setText("this text");
- List 5 methods that the String class should absolutely have.
- length(), toCharArray(), charAt(int); append(String), toLowerCase(),
substring(String, start, end);
- Write a program that reads a string as a command line argument
and prints out (on the standard output stream) the string in reversed order.
- What is the result of: 145/12.5? How about 145 %30?
- 11 and 4 (25), respectively.
- Give some examples of converting numbers to strings and casting
strings as numbers.
- Integer.parseInt(String); myNewString = String.valueOf(float);
- Declare and instantiate a JTextField variable. Set the text
to "123". Extract the text and cast it as an integer, multiply
by 2 and write it in the JTextField .
- Declare and instantiate a JButton object. Make your class
ActionListener and define the body of the actionPerformed method
so that a result of the string in the JTextField above is printed twice
in the text field.
- Define a class ComplexNumber with 2 private member variables
rE and iM (for the rEal and iMaginary part
of the complex number) and provide setRe(double newRe), setIm
(double newRe), getRe(), getIm(), public member
methods for establishing controlled access to these variables.
Last modified on
by
.
\Ivo D. Dinov, Ph.D., Departments of Statistics, Neurology &
Program in Computing, UCLA School of Medicine/