Summary for 6/18/03

Lecture Summary for 211 on 6/18/03 -- O'Hare

 

Review Questions

  1. How do you find the Java examples for this class?
    Answer: Under Java Examples in the table of contents on the left.

  2. When, where, and why was Java developed?
    Answer: Java was developed in the early 1990s at Sun Microsystems as a simplified version of C++. Originally it was designed as a language for programming microprocessors in appliances, but became more popular as an internet programming language.

  3. What is the difference between JDK1.3 and BlueJ?
    Answer: JDK1.3 is Sun's Java compiler. BlueJ is an editing and compiling environment running on top of JDK1.3. BlueJ was designed as an educational tool at Monash University.

  4. Why is Java hard for beginners?
    Answer: Because Java is object oriented from Day 1. However, Java's program structure is cleaner than that of C++ because of Java's strict object oriented design.

  5. What is "boiler plate" code?
    Answer: It is code that is for the most part copied, but modified in a few key places to achieve the desired output or result.

  6. Write a line of Java code that will print the message "Java is fun."   Answer:
    System.out.println("Java is fun.");

  7. What is an instance variable?
    Answer: A variable contained in an object. This variable contains data about the object useful to the application.

  8. What is a method?
    Answer: A procedure for doing something. A method can either be an object method (called from an object) or static (called directly from the class).

  9. What is a constructor?
    Answer: A special method that creates an object. A constructor is invoked with the new keyword.

  10. What is the difference between a class and an object?
    Answer: The class is the template or blueprint for creating objects. Many objects can be created from a class.

  11. What are the steps to creating a Java application in BlueJ?
      Answer:
    1. Invoke BlueJ.
    2. Select Project and New Project in the main menu to create a new project.
    3. Click on the New Class button to create a new class. The name of the class you choose and the name of the class in the line
      public class ClassName
      must be the same.
    4. Repeat Step c to create as many new classes as you need.
    5. Compile the application by clicking the Compile button.
    6. Run the application by right clicking on the class containing the main method. Then select void main(args) and click on OK.

 

Look at the Greeter2 Example in Input (Input.zip or Input.txt)

  1. Use the JOptionPane.showInputDialog method to display a an input dialog and obtain input from it.

  2. Use Control-Tab to find an input dialog hidden behind other windows.

 

The Three Java Styles for Comments

 

A Closer Look at the Greeter Examples

 

Command Line vs. User Interface Applications

 

Java Constants, Variables, and Datatypes

 

The Primitive Example

 

Practice Problems

  1. Create an integer variable and initialize it to 1.5 billion.   Answer:
    int population = 1500000000;

  2. Create a variable and initialize it to 6 billion.   Answer:
    double p = 6.0e9;

  3. Create a character variable and initialize it to the upper case W character.   Answer:
    char a = 'W';

  4. Create a boolean variable and initialize it to false.   Answer:
    boolean flag = false;

 

Java Operators

   

The parseInt Method

 

Static vs. Nonstatic Methods

 

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

 

Practice Problems

  1. 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.

  2. 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.