Lecture Summary for 211 on 1/14/04
 
Review Questions
- How do you find the Java examples for this class?
- When, where, and why was Java developed?
- What is the difference between JDK1.3 and BlueJ?
- Why is Java hard for beginners?
- What is "boiler plate" code?
- Write a line of Java code that will print the message
"Java is fun."
- What is an instance variable?
- What is a method?
- What is a constructor?
- What is the difference between a class and an object?
- Name three ways of specifying comments in Java?
- What are the steps to creating a Java application in BlueJ?
 
Command Line vs. User Interface Applications
- Applications that output with System.out.println are called
command line applications.
- Applications that create their own window and display output in
labels, textfields, etc. are called user interface applications.
- Look at the Temperature Example in Intro (Intro.zip or Intro.txt).
 
- Integer, floating point, character, and Boolean constants.
- Variables.
- int, double, char, and boolean datatypes.
- String objects.
- Go over the Variables Example in Primitive
(Primitive.zip and Primitive.txt).
 
The Primitive Example
- Look at the Variables Example in Primitive
(Primitive.zip and Primitive.txt).
 
Practice Problems
- Create an integer variable and initialize it to 1.5 billion.
- Create a variable and initialize it to 6 billion.
- Create a character variable and initialize it to the
upper case W character.
- Create a boolean variable and initialize it to false.
 
- Arithmetic Operators:  
+   -   *   /   %
- String Concatenation Operator:  +
- Comparison Operators:  
==   <   <=   >   >=   !=
- Logical Operators:  
&&   ||   !
- Assignment Operators:  
=   +=   -=   *=   /=   %=   ++   --
- Go over the Operators Example in Primitive
(Primitive.zip and Primitive.txt).
- Go over the Trace1 Examples  
(Trace1.txt).
 
The JOptionPane Class
- To use an input dialog in a class, use this statement at the top
of the class definition:
import javax.swing.JOptionPane;
- Look at the JOptionPane Example in Intro
(Intro.zip and Intro.txt).
- Sometimes the input dialog will not appear when the program
is run. You will have to find it behind other windows.
 
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 format a floating point value, use the format
method of the DecimalFormat class.
- The format method of DecimalFormat returns a string, not a double.
- 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
(DecimalFormat.zip or DecimalFormat.txt).
 
The parseInt Method
- Use the Integer.parseInt method call to convert a string representing
an integer into an int variable.
- The following Java code converts the string "234" into the int value
234:
String a = "234";
int x;
x = Integer.parseInt(a);
- If a nonnumeric string is passed to Integer.parseInt, the program will
crash. A NumberFormatException will be produced.
 
Static vs. Nonstatic Methods
- A static method is a method that may be called using
either the class directly or using an object of that class.
- For example, the method showInputDialog can either be called
using the JOptionPane class as
JOptionPane.showOptionPane(prompt);
or by using the JOptionPane object jop as
JOptionPane jop = new JOptionPane();
jop.showOptionPane(prompt);
- A nonstatic method can only be called using an object.
- For example, the VendingMachine method depositQuarter is called
using the VendingMachine object vm:
VendingMachine vm = new VendingMachine();
mv.depositQuarter();
 
Examples of Static and Nonstatic Methods
| Class | Method | Static? |
| Main | main | Yes |
| JOptionPane | showInputDialog | Yes |
| Math | random | Yes |
| Math | sqrt | Yes |
| VendingMachine | depositQuarter | No |
| VendingMachine | selectCandy | No |
| VendingMachine | printStatus | No |
| Coin | flip | No |
| DecimalFormat | format | No |
- println and print are NOT static methods because they are
called from the special class System.out.
 
Practice Problems
- Write a Java program that will input a temperature in
Celsius, change the input string into an int, convert the
temperature to Fahrenheit, and print the Fahrenheit temperature.
- Write a Java program that will input an integer, then output
a 0 if the input is even and a 1 if the input is odd.
Here are the
answers to the practice problems.