Section 13.5. Concepts Summary


[Page 477 (continued)]

13.5. Concepts Summary

In this chapter we introduced HTML, and writing programs to generate HTML. We broke a long method into several smaller helper methods. We showed how a method can throw an exception. We talked about the "unnamed" package. We introduced another of the collection classes: HashMap. We introduced generics which allow us to add types to collection classes. We introduced using an Iterator to loop through a collection. We also used JDBC and SQL to query a relational database.

13.5.1. HTML

HTML is the HyperText Markup Language. It consists of tags that you add to text to control how it is displayed in a browser. HTML tags often come in pairs of opening and closing tags like <title>...</title>. Tags that do not have a opening and closing tag look a bit different: <img src='/books/1/79/1/html/2/file' height='100' />.


[Page 478]

13.5.2. Helper Methods

When a method gets long and hard to read, it is best to break it up into several smaller methods. These are often called helper methods because they help another method accomplish a task. Helper methods are often declared with a visibility of private, but they don't have to be. If a helper method is something that another class would like to have access to, it can be public. Breaking a method up into smaller methods is also called procedural abstraction.

13.5.3. Throwing an Exception

A method can throw an exception instead of catching it by adding throws ExceptionName after the parameter list in the method declaration. Here is an example:

private void writeBody(BufferedWriter writer, String body)   throws IOException


Then, if the exception occurs during the execution of the method, the runtime will look to see whether the method that called the method that throws the exception handles it.

13.5.4. The "Unnamed" Package

All classes that you create will be put into a package. If you don't include a package statement in your file as the first line of executable code in the file, then your class is put in the "unnamed" package and can use other classes that are also in that package. A package statement looks like:

package edu.gatech.intro; public class Student


13.5.5. HashMap

A HashMap is a class that implements the Map interface. It is one of the collection classes. It is in the package java.util. It allows you to store an object (called the value) which can be retrieved by using another object (the key). There cannot be duplicate keys in a map, and each key maps to one value. You can put values into the map for a key using the method put(Object key, Object value). You can get a value out of the map for a key using the method get(Object key).

13.5.6. Generics

You can use generics to provide type information for collection classes. To do so, just specify the type of the collection class when you create it and also in variables that refer to it. This eliminates the need to downcast an object that you get from a collection back to the original class.

private Map<String,String> phoneMap =     new HashMap<String,String>();



[Page 479]

13.5.7. Iterators

If you are using Java 5.0 (1.5) you can use the for-each loop to walk through the elements in a collection. You can also use an Iterator from package java.util. Iterator is an interface that provides methods for checking if there are still items in the collection to process (hasNext()) and for getting the next item (next()). If you assign the item from the collection to an object reference of a type other than Object, you will need to downcast (cast to a subclass).

// loop through the keys Iterator iterator = keySet.iterator(); while (iterator.hasNext()) {   key = (String) iterator.next();


13.5.8. JDBC and SQL

JDBC is another name for the classes in the java.sql package that let Java programs communicate with databases. We used classes in this package to create a connection to a database (Connection), create an object that handles executing SQL (Statement), and retrieve the results from a SQL query (ResultSet).

SQL (Structured Query Language) is a standard language for manipulating and querying databases. We used it to select data that we wanted from a relational database. It can also be used for creating databases and tables, and for removing or updating data in the tables.



Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net