Lecture Summary for 211 on 7/07/03 -- O'Hare
 
Review Questions
- Give three reasons why software engineers would want to
write static methods, other than the main method.
Answer: To avoid code duplication, for modularity, and to
facilitate code reuse.
- What does method invocation mean?
Answer: It means to call a method.
- How do you invoke a method with return type void?
Answer: Place the method name with any arguments in parentheses
on a line by itself.
- What does the header of a method contain?
Answer: It contains the visibility (public or private),
whether the method is static, the return type, the name of the
method, and any parameters.
- What is the difference between a method argument and
a method parameter?
Answer: An argument is specified at method invocation, but
a parameter is specified in the header of the method at definition.
- How do you specify the return value of a non-void method in
the method definition?
Answer: You put the return type in the header.
- How do you invoke a non-void method?
Answer: Use the method call in an expression. The return value
is used in the expression.
- Write a Java application that inputs a list of items consisting
of name, gender, and salary. Output the name of the woman with the
largest salary.
- True or False. A maximum of 20 objects can be created from
a given class.
Answer: False. Objects can be created as long as you have
enough memory, usually at least thousands.
- What does UML mean?
Answer: Unified Modeling Language
- What are the three sections of a UML class diagram?
Answer: Class name, instance variables, methods.
 
The DecimalFormat Object
- A DecimalFormat object is used to format a floating point number.
- To create a DecimalFormat object, pass a format string to the
DecimalFormat constructor.
- Here are some format strings and their meanings:
| Format String | Digits After Decimal Point? |
Always Show Leading Digit? | Commas? |
| "#0.0000" |
4 | Yes | No |
| "#.000" |
3 | No | No |
| "#,##0.00" |
2 | Yes | Yes |
| "#,###.0" |
1 | No | Yes |
- Here is Java code to create a DecimalFormat object df that
formats a floating point number with two digits behind the decimal
point, always includes a leading zero, and uses commas:
DecimalFormat decimalFormatObject;
decimalFormatObject = new DecimalFormat("#,##0.00");
- To actually format a floating point value, use the format
method of the DecimalFormat class.
- Here is Java code to format the value 34,435.584743:
double value = 34435.584743;
String formattedValue;
formattedValue = decimalFormatObject.format(34435.584743);
System.out.println("The formatted value is " + formattedValue + ".");
- Look at the DecimalFormat Example in DecimalFormat.zip or
DecimalFormat.txt.
 
UML
- UML means Unified Modeling Language.
- A UML class diagram shows the class name, instance variables,
and public methods.
- The symbol + means a member is public.
- The symbol - means a member is private.
- Here are some UML class diagram examples:
BasketballPlayer  
Coin  
VendingMachine  
Kid  
Student  
 
The Kid1 Example
- Look at the Kid1 Example in Classes1 (Classes1.zip or
Classes1.txt).
 
Accessing Members of an Object
- A member of a class is an instance variable or a method.
- Use the . operator to access a
member of a class or object.
- In the Kid1 Example,
x.name
accesses the name instance variable of the Kid object x.
- In the Kid1 Example,
x.display();
accesses the display method of the Kid object x.
- The toString method is supplied by the designed of a class
to return the information in an object for display purposes.
- The toString method of an object is automatically
invoked when an object is printed. For example, if x is an object,
System.out.println(x);
is short for
System.out.println(x.toString());
- If an instance variable is left uninitialized, it assumes a default
value according to the following table:
| Datatype | Default Value |
| int |
0 |
| double |
0.0 |
| char |
\0 (ascii code 0) |
| boolean |
false |
| Object |
null |
- The Object datatype includes objects of all types including
String objects.
 
The Kid2 Example
- Look at the Kid2 Example in Classes1 (Classes1.zip or
Classes1.txt).
 
Constructors
- A constructor is called by the new operator to create
a new object.
- No parameters are passed in to a noarg contstructor.
Example:
Kid x = new Kid();
- One or more parameters are passed in to a
parameterized constructor.
Example:
Kid y = new Kid("Jane", 'F', 29);
 
Public vs. Private Members
- A public member of a class
can be called from the Main class or from any other class.
- A private member of a class can only be accessed
by methods within that same class.
- A instance variables tend to be declared private.
- Methods tend to be declared public.
- Accessor methods provide read only access to instance
variables.
- Mutator methods can modify instance variables.
Data validation or error checking can be performed.
 
Overloaded Methods
- The signature of a method is the ordered list of
the types of its parameters.
- Two methods are different if their names differ, or if their
names are the same but their signatures are different. For example
f(5, "a"); and
f("a", 5); are different methods
because the signature (int, String) is different than
(String, int).
- See the Overloaded Example in Overloaded.zip or Overloaded.txt.
 
Public vs. Private Members
|   | Instance Vars |
Access Mode | Advantages | Disadvantages |
| Method 1 | public | Direct |
Easy | Not Safe |
| Method 2 | private |
Using Methods* | Safe |
Complicated |
*Methods to manipulate
instance variables are of two types:
- Accessor methods provide read only access to instance
variables.
- Mutator methods can modify instance variables.
Data validation or error checking can be performed.
 
The Student Example
- Look at the Student example in Classes1.
 
 
The Die Example
- Look at the Die example in Die.zip or Die.txt.
 
The
Trace3 Examples